Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 963
blee Avatar asked Sep 08 '26 00:09

blee


1 Answers

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

like image 191
Gleb Avatar answered Sep 11 '26 06:09

Gleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!