From 945ddf3d7d651a0718277f1bc76167cdcc34f6f7 Mon Sep 17 00:00:00 2001 From: fnzv Date: Sat, 4 Nov 2023 21:18:55 +0100 Subject: [PATCH] initial commit --- .github/workflows/docker-image.yml | 22 +++ .gitignore | 21 +++ Dockerfile | 11 ++ LICENSE | 21 +++ README.md | 145 +++++++++++++++ go.mod | 35 ++++ go.sum | 209 +++++++++++++++++++++ img/car-report-sheet.PNG | Bin 0 -> 451037 bytes k8s/cronjob.tf | 58 ++++++ p2c.go | 288 +++++++++++++++++++++++++++++ 10 files changed, 810 insertions(+) create mode 100644 .github/workflows/docker-image.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 img/car-report-sheet.PNG create mode 100644 k8s/cronjob.tf create mode 100644 p2c.go diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..5df0abb --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,22 @@ +name: Docker Image CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag fnzv/p2c + - name: Registry login + run: echo ${{secrets.GH_DOCKERHUB}} | docker login -u fnzv --password-stdin + - name: Push image + run: docker push fnzv/p2c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b735ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6fea487 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.21.1 + +RUN mkdir -p /app + +WORKDIR /app + +ADD . /app + +RUN go build ./p2c.go + +CMD ["./p2c"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c5d1881 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Sami + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4520477 --- /dev/null +++ b/README.md @@ -0,0 +1,145 @@ + +# Prometheus 2 CSV Exporter + + ![Project Logo](/img/car-report-sheet.PNG) + +This is a Go application that exports data from Prometheus as a CSV file and optionally uploads it to an AWS S3 bucket. This README will guide you through setting up and running the application. + +Related blog post: +- https://blog.sami.pw/2023/11/exporting-prometheus-metrics-into.html + + +## Table of Contents + + +- [Prometheus 2 CSV Exporter](#prometheus-2-csv-exporter) + - [Table of Contents](#table-of-contents) + - [Prerequisites](#prerequisites) + - [Demo](#demo) + - [Installation](#installation) + - [Build](#build) + - [Examples](#examples) + - [Contributing](#contributing) + - [License](#license) + + + + + +## Prerequisites + + + +Before using this exporter, you should have the following prerequisites installed and configured: + +- Go (tested with Golang 1.21.1): Make sure you have Go installed on your system, you can download and install Go from the [official Go website](https://golang.org/doc/install). + +- AWS S3 bucket (Optional): If you plan to upload the CSV file to an AWS S3 bucket, make sure you have the AWS credentials configured, the Go script will automatically load the default AWS credentials or take them from enviroment variables `AWS_SECRET_ACCESS_KEY` and `AWS_ACCESS_KEY_ID` + +- Prometheus: You must have access to a Prometheus server with a valid address to query data. + + +## Demo + +Tested on Ubuntu 22.04 and Golang 1.18, 1.21 + +[![asciicast](https://asciinema.org/a/dnnGv3ZYarAeXER37uYTKYdZt.png)](https://asciinema.org/a/dnnGv3ZYarAeXER37uYTKYdZt) + + + +## Installation + + + +1. Clone this repository to your local machine: + + +`git clone https://github.com/fnzv/p2c.git` + + + +Change your working directory to the project folder: + + + +`cd p2c/` + +## Build + + +`go build p2c.go` + + +The exporter binary will be created in the project folder. + +`./p2c` + + +You can run the Prometheus to CSV exporter using the following command: + + + + +``` +./p2c --query --time-range