• Stars
    star
    364
  • Rank 117,101 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool to scrape a Prometheus client and dump the result as JSON.

prom2json

A tool to scrape a Prometheus client and dump the result as JSON.

Background

(Pre-)historically, Prometheus clients were able to expose metrics as JSON. For various reasons, the JSON exposition format was deprecated.

Usually, scraping of a Prometheus client is done by the Prometheus server, which preferably happens with the protocol buffer format. Sometimes, a human being needs to inspect what a Prometheus clients exposes. In that case, the text format is used (which is otherwise meant to allow simplistic clients like shell scripts to expose metrics to a Prometheus server).

However, some users wish to scrape Prometheus clients with programs other than the Prometheus server. Those programs would usually use the protocol buffer format, but for small ad hoc programs, that is too much of an (programming) overhead. JSON comes in handy for these use-cases, as many languages offer tooling for JSON parsing.

To avoid maintaining a JSON format in all client libraries, the prom2json tool has been created, which scrapes a Prometheus client in protocol buffer or text format and dumps the result as JSON to stdout.

Usage

Installing and building:

$ GO111MODULE=on go install github.com/prometheus/prom2json/cmd/prom2json@latest

Running:

$ prom2json http://my-prometheus-client.example.org:8080/metrics
$ curl http://my-prometheus-client.example.org:8080/metrics | prom2json
$ prom2json /tmp/metrics.prom

Running with TLS client authentication:

$ prom2json --cert=/path/to/certificate --key=/path/to/key http://my-prometheus-client.example.org:8080/metrics

Running without TLS validation (insecure, do not use in production!):

$ prom2json --accept-invalid-cert https://my-prometheus-client.example.org:8080/metrics

Advanced HTTP through curl:

$ curl -XPOST -H 'X-CSRFToken: 1234567890abcdef' --connect-timeout 60 'https://username:[email protected]:8080/metrics' | prom2json

This will dump the JSON to stdout. Note that the dumped JSON is not using the deprecated JSON format as specified in the Prometheus exposition format reference. The created JSON uses a format much closer in structure to the protocol buffer format. It is only used by the prom2json tool and has no significance elsewhere. See below for a description.

A typical use-case is to pipe the JSON format into a tool like jq to run a query over it. That looked like the following when the clients still supported the deprecated JSON format:

$ curl http://my-prometheus-client.example.org:8080/metrics | jq .

Now simply use prom2json instead of curl (and change the query syntax according to the changed JSON format generated by prom2json):

$ prom2json http://my-prometheus-client.example.org:8080/metrics | jq .

Example query to retrieve the number of metrics in the http_requests_total metric family (only works with the new format):

$ prom2json http://my-prometheus-client.example.org:8080/metrics | jq '.[]|select(.name=="http_requests_total")|.metrics|length'

JSON format

Note that all numbers are encoded as strings. Some parsers want it that way. Also, Prometheus allows sample values like NaN or +Inf, which cannot be encoded as JSON numbers.

[
  {
    "name": "http_request_duration_microseconds",
    "help": "The HTTP request latencies in microseconds.",
    "type": "SUMMARY",
    "metrics": [
      {
        "labels": {
          "method": "get",
          "handler": "prometheus",
          "code": "200"
        },
        "quantiles": {
          "0.99": "67542.292",
          "0.9": "23902.678",
          "0.5": "6865.718"
        },
        "count": "743",
        "sum": "6936936.447000001"
      },
      {
        "labels": {
          "method": "get",
          "handler": "prometheus",
          "code": "400"
        },
        "quantiles": {
          "0.99": "3542.9",
          "0.9": "1202.3",
          "0.5": "1002.8"
        },
        "count": "4",
        "sum": "345.01"
      }
    ]
  },
  {
    "name": "roshi_select_call_count",
    "help": "How many select calls have been made.",
    "type": "COUNTER",
    "metrics": [
      {
        "value": "1063110"
      }
    ]
  }
]

Using Docker

You can deploy this tool using the prom/prom2json Docker image.

For example:

docker pull prom/prom2json

docker run --rm -ti prom/prom2json http://my-prometheus-client.example.org:8080/metrics

More Repositories

1

prometheus

The Prometheus monitoring system and time series database.
Go
54,496
star
2

node_exporter

Exporter for machine metrics
Go
10,870
star
3

alertmanager

Prometheus Alertmanager
Go
6,540
star
4

client_golang

Prometheus instrumentation library for Go applications
Go
5,367
star
5

blackbox_exporter

Blackbox prober exporter
Go
4,532
star
6

client_python

Prometheus instrumentation library for Python applications
Python
3,914
star
7

jmx_exporter

A process for exposing JMX Beans via HTTP for Prometheus consumption
Java
3,005
star
8

pushgateway

Push acceptor for ephemeral and batch jobs.
Go
2,969
star
9

client_java

Prometheus instrumentation library for JVM applications
Java
2,166
star
10

mysqld_exporter

Exporter for MySQL server metrics
Go
2,097
star
11

snmp_exporter

SNMP Exporter for Prometheus
Go
1,634
star
12

statsd_exporter

StatsD to Prometheus metrics exporter
Go
912
star
13

cloudwatch_exporter

Metrics exporter for Amazon AWS CloudWatch
Java
892
star
14

procfs

procfs provides functions to retrieve system, kernel and process metrics from the pseudo-filesystem proc.
Go
767
star
15

docs

Prometheus documentation: content and static site generator
SCSS
645
star
16

haproxy_exporter

Simple server that scrapes HAProxy stats and exports them via HTTP for Prometheus consumption
Go
615
star
17

promlens

PromLens – The query builder, analyzer, and explainer for PromQL
TypeScript
552
star
18

client_ruby

Prometheus instrumentation library for Ruby applications
Ruby
510
star
19

client_rust

Prometheus / OpenMetrics client library in Rust
Rust
462
star
20

consul_exporter

Exporter for Consul metrics
Go
436
star
21

graphite_exporter

Server that accepts metrics via the Graphite protocol and exports them as Prometheus metrics
Go
350
star
22

promu

Prometheus Utility Tool
Go
268
star
23

influxdb_exporter

A server that accepts InfluxDB metrics via the HTTP API and exports them via HTTP for Prometheus consumption
Go
261
star
24

exporter-toolkit

Utility package to build exporters
Go
261
star
25

common

Go libraries shared across Prometheus components and libraries.
Go
261
star
26

collectd_exporter

A server that accepts collectd stats via HTTP POST and exports them via HTTP for Prometheus consumption
Go
255
star
27

memcached_exporter

Exports metrics from memcached servers for consumption by Prometheus.
Go
182
star
28

test-infra

Prometheus E2E benchmarking tool
Go
153
star
29

compliance

A set of tests to check compliance with various Prometheus interfaces
Go
127
star
30

nagios_plugins

Nagios plugins for alerting on Prometheus query results
Shell
103
star
31

demo-site

Demo site auto-deployed with Ansible and Travis CI.
HTML
96
star
32

client_model

Data model artifacts for Prometheus.
Makefile
74
star
33

golang-builder

Prometheus Golang builder Docker images
Shell
69
star
34

codemirror-promql

PromQL support for the CodeMirror code editor
TypeScript
39
star
35

busybox

Prometheus Busybox Docker base images
Makefile
37
star
36

prometheus_api_client_ruby

A Ruby library for reading metrics stored on a Prometheus server
Ruby
36
star
37

talks

Track Prometheus talks
20
star
38

lezer-promql

A lezer-based PromQL grammar
JavaScript
12
star
39

proposals

Design documents for Prometheus Ecosystem
Makefile
9
star
40

host_exporter

See the "node_exporter" repository instead!
8
star
41

circleci

7
star
42

snmp_exporter_mibs

4
star
43

promci

GitHub Actions repository
4
star
44

kube-demo-site

Kubernetes Demo Site
Go
1
star
45

client_java-benchmarks

1
star
46

sigv4

A http.RoundTripper that will sign requests using Amazon's Signature Verification V4 signing procedure
1
star