How can I continuously poll JSON data in Prometheus?

Viewed 1306

I am new to prometheus so did some researched and found that it is helpful for monitoring application there are lot of examples of increasing counter to check api calls. My use case is somewhat different, I have a Rest API which is connecting to database executing query and returning data in JSON format. For example:

{
    "dskreads": 26815,
    "bufreads": 14451041,
    "dskwrites": 52471,
    "bufwrites": 569468,
    "isamtot": 18439806,
    "isopens": 432586,
    "isstarts": 408604,
    "isreads": 6962799,
    "iswrites": 80438,
    "isrewrites": 54535,
    "isdeletes": 54469,
    "iscommits": 61673,
    "isrollbacks": 0,
    "latchwts": 190,
    "buffwts": 192,
    "lockreqs": 13921801,
    "lockwts": 0,
    "ckptwts": 7,
    "deadlks": 0,
    "lktouts": 0,
    "numckpts": 106,
    "plgpagewrites": 11106,
    "plgwrites": 460,
    "llgrecs": 552829,
    "llgpagewrites": 49076,
    "llgwrites": 43672,
    "pagreads": 76446,
    "pagwrites": 68495,
    "flushes": 107,
    "compress": 15039,
    "fgwrites": 0,
    "lruwrites": 0,
    "chunkwrites": 8195,
    "btraidx": 1627,
    "dpra": 3933,
    "rapgs_used": 4755,
    "seqscans": 16008,
    "totalsorts": 6691,
    "memsorts": 4612,
    "disksorts": 2079,
    "maxsortspace": 224
}

Now I want to register my API to Prometheus so that it continuously polls the above data and shows it in graphical format.

1 Answers

I think what you're saying is that, the JSON document you referenced is the result of an API call and that you'd like each of its properties (e.g. dskreads) to be treated as a metric by Prometheus so that you may query|graph these?

If so, you will need to write an exporter that:

  1. GET's your API

  2. Parses the JSON response

  3. Iterates over the properties

    • Generating a Prometheus metric for each
  4. Render the results as an HTTP (/metrics) page

  5. Scrape this page from a Prometheus server

See Writing Exporters

One thing to be mindful of is that, Prometheus expects Counters to always increment (and you may not be able to set an initial value, e.g. for a Counter called perhaps total_disk_reads (derived from dskreads), you may not be able to initialize the Counter's value to 26815.

For this reason and, if you think you'll need to reduce a measured value, you may want to ensure you wish to use Counters rather than Gauges. See Metric Types

Related