blee
blee

Reputation: 596

Prometheus json metrics

The application I want to monitor provides an api endpoint for health checks that responds with metrics in json. As an example:

$ curl  https://example.com/api/stats
{"status":"success","code":0,"data":{"UserCount":140,"UserCountActive":23}}

I have setup the Prometheus blackbox_exporter to monitor that this endpoint returns 200 Ok however I'd ideally like to get those metrics too. I understand instrumentation exports this data directly from the application. But since the application is already exporting what I want in a json object, I would prefer the convenience of not maintaining my own fork of this software to include the Prometheus libraries necessary for instrumentation. How should I consume metrics that are in json?

Upvotes: 5

Views: 30038

Answers (2)

Gleb
Gleb

Reputation: 89

You can use Prometheus JSON Exporter (https://github.com/prometheus-community/json_exporter) to call your service and extract values from JSON

Deploy Prometheus JSON Exporter where it can be pulled by Prometheus and where Exporter can hit your URL

For your JSON example config.xml for JSON Exporter will be like

---
metrics:
  - name: user_count
    path: "{$.data.UserCount}"
    type: value
    help: UserCount value
  - name: user_count_active
    path: "{$.data.UserCountActive}"
    type: value
    help: UserCountActive value

and scrape config in Prometheus (prometheus.yml):

    ## gather the metrics from third party json sources, via the json exporter
  - job_name: json_user_stat
    metrics_path: /probe
    static_configs:
      - targets:
          # URL of each API for json exporter
          - https://example.com/api/stats
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        # Location of the json exporter's real <hostname>:<port> from Prometheus
        replacement: json_exporter:7979

Test first your Exporter by hitting URL (please encode "target" value if you like to use outside of your browser, browser will encode automatically) http://json_exporter:7979/probe?target=https://example.com/api/stats and check output

# HELP UserCount value
# TYPE logstash_audit_events_in untyped
user_count{} 140
# HELP lUserCountActive value
# TYPE logstash_audit_events_out untyped
user_count_active{} 23

If you get it - configure scape in Prometheus and enjoy your metrics

Upvotes: 6

Michael Doubez
Michael Doubez

Reputation: 6863

There is currently no official exporter to scrape JSON endpoint. Maybe because it is easy to write one from scratch and any general solution must use some default behaviors like building the name of the metric from the path to the data which doesn't take into account the type of the metric ; or any relevant label to apply or parse date to name a few.

You will easily find available JSON exporters with your preferred search engine. They can readily replace the blackbox_exporter. And they should be a good fit given the sample provided.

One solution, I would like to mention is the exporter_exporter because I have found it useful for implementing rapidly an exporter while waiting for an adhoc one. It can be used to execute scripts that produce prometheus metrics. In your case, it is quite easy to write a python script that scrape a Json endpoint and use it to write the corresponding prometheus format in standard output.

Upvotes: 3

Related Questions