Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzv committed Nov 4, 2023
0 parents commit 945ddf3
Show file tree
Hide file tree
Showing 10 changed files with 810 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.21.1

RUN mkdir -p /app

WORKDIR /app

ADD . /app

RUN go build ./p2c.go

CMD ["./p2c"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
145 changes: 145 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <Prometheus query> --time-range <time range> --address <Prometheus address> [--upload-s3 <S3 destination>] [--region <AWS region>] [--filename <Destination file name>]
```


Options


```
--query: (Required) The Prometheus query to fetch data.
--time-range: (Required) The time range for the query (e.g., "29d" for 29 days).
--address: (Required) The address of the Prometheus server.
--upload-s3: (Optional) The S3 destination in the format "s3://bucket-name/path/to/folder/". If provided, the CSV file will be uploaded to this S3 location.
--region: (Optional) The AWS region for S3. Required if --upload-s3 is specified.
--filename: (Optional) The name of the destination CSV file. If not provided, a default file name will be used.
--debug: (Optional) Enable debug output
```
All the following options can be used also via enviroment variables as you can see in the k8s/cronjob.tf example, the variables have the prefix `P2C_NAME` , here are some examples using environment variables:

```
P2C_QUERY='up' P2C_TIMERANGE='29d' P2C_ADDRESS='http://prometheus.ingress' P2C_UPLOAD_S3='s3://your-s3-buckets/metrics/' P2C_REGION='eu-west-1' ./p2c
```

## Examples



Export data from Prometheus to a local CSV file:


`./p2c --query "up" --time-range "7d" --address "http://prometheus.example.com:9090"`



Export data from Prometheus and upload it to an AWS S3 bucket:

`./p2c --query "up" --time-range "7d" --address "http://prometheus.example.com:9090" --upload-s3 "s3://my-s3-bucket/folder/" --region "us-west-1"`



Specify a custom destination file name:

`./p2c --query "up" --time-range "7d" --address "http://prometheus.example.com:9090" --filename "custom_data.csv"`



## Contributing


Contributions to this project are welcome! If you have any ideas, improvements, or bug fixes, feel free to create an issue or submit a pull request.

## License


This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to customize the information as needed for your specific project.
35 changes: 35 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module p2c

go 1.18

require (
github.com/aws/aws-sdk-go v1.46.1
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/common v0.45.0
golang.org/x/oauth2 v0.13.0
google.golang.org/api v0.148.0
)

require (
cloud.google.com/go/compute v1.23.2 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading

0 comments on commit 945ddf3

Please sign in to comment.