Skip to main content

A script to export Prometheus metrics as CSV files

Project description

prom2csv: Export prometheus metrics into CSV

A hi-performance tool to export prometheus metrics in a CSV file.

It will help you fetching the last value of metrics extracted from prometheus, including computed values and export those results as CSV files. Which make it efficient to export those data to other tools.

It also supports generating IDs based on labels of metrics (see --row-id-expression), so tools can compare values or override existing ones easily => one metric is always named the same way.

Requirements

It just requires requests => pip install requests

You can install it with pip install prom2csv in any python environment (Python 3.9+)

Usage

usage: prom2csv [-h] [--prometheus-url PROMETHEUS_URL] [--column-name-mapping COLUMN_NAME_MAPPING COLUMN_NAME_MAPPING]
                [--row-id-expression ROW_ID_EXPRESSION] [--remove-column REMOVE_COLUMN] [--skip-csv-header] [-o OUTPUT_CSV] [-v]
                query

Export Prometheus time-serie to CSV

positional arguments:
  query                 Prometheus query to perform

options:
  -h, --help            show this help message and exit
  --prometheus-url PROMETHEUS_URL
                        Connect to the given prometheus host (default=$PROMETHEUS_URL or http://localhost:9090)
  --column-name-mapping COLUMN_NAME_MAPPING COLUMN_NAME_MAPPING
                        map a column into a specific name (id, timestamp, value + labels) into another name: --column-name-mapping id my_id (can be
                        repeated)
  --row-id-expression ROW_ID_EXPRESSION
                        First column value (default=__name__'). You can use python expression using labels: --name 'f"{__name__}.{__job__}"
  --remove-column REMOVE_COLUMN
                        Remove a column, can be specified multiple times
  --skip-csv-header     Do not create CSV header
  -o OUTPUT_CSV, --output-csv OUTPUT_CSV
                        File to perform the output to, defaults to stdout
  -v, --version         show program's version number and exit

Examples

Get the size of the table user on all Postgresql instance in env=prod

./prom2csv.py 'pg_stat_user_tables_n_live_tup{relname="user",env="prod"}'    
id,timestamp,value,datname,duty,env,hostname,instance,job,relname,schemaname,tenant_id,type
pg_stat_user_tables_n_live_tup,2024-06-05 09:54:09.274000+00:00,180,pgdb,always,prod,prod1005-01,prod1005-01.example.org:39187,postgres,user,public,1005,app
pg_stat_user_tables_n_live_tup,2024-06-05 09:54:09.274000+00:00,107,pgdb,always,prod,prod1006-01,prod1006-01.example.org:39187,postgres,user,public,1006,app

Same, but set the name of instance as first row and hide some rows

./prom2csv.py 'pg_stat_user_tables_n_live_tup{relname="user",env="prod"}' \
  --column-name-mapping id my_instance \
  --row-id-expression 'instance' \
  --column-name-mapping tenant_id my_customer \
  --remove-column timestamp \
  --remove-column type \
  --remove-column duty
my_instance,value,datname,env,hostname,instance,job,relname,schemaname,my_customer
prod1005-01.example.org:39187,180,pgdb,prod,prod1005-01,prod1005-01.example.org:39187,postgres,user,public,1005
prod1006-01.example.org:39187,107,pgdb,prod,prod1006-01,prod1006-01.example.org:39187,postgres,user,public,1006

List all machines/partitions in dev or prod with partition usage > 50%

./prom2csv.py --column-name-mapping id instance --row-id-expression 'instance' '(node_filesystem_free_bytes{env=~"dev|prod",device!~"rootfs",fstype!="tmpfs"} / node_filesystem_size_bytes{env=~"dev|prod",device!~"rootfs",fstype!="tmpfs"}) < 0.5'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prom2csv-0.9.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

prom2csv-0.9.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file prom2csv-0.9.1.tar.gz.

File metadata

  • Download URL: prom2csv-0.9.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for prom2csv-0.9.1.tar.gz
Algorithm Hash digest
SHA256 8ee3435136d31ed4e7ca7c22769643df29a7cdabb22e153fe5a818933b5173f1
MD5 96048142891f92686977b51b8a8f884d
BLAKE2b-256 38ad0c94343e8d5be27fffdb41c3aefe8f21f5b24099fd498123490c77bd2064

See more details on using hashes here.

File details

Details for the file prom2csv-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: prom2csv-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for prom2csv-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ec7b0dc48d73343e708c7818f49ac37a92e0ebf1189f0dc43894884bf11425b
MD5 6e9478d6ab6b5f4cd1cbb2aa4422b516
BLAKE2b-256 1dd5aa5d553579f6e89864b360951b4c15074e4f1f4ef77687860687a5148e04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page