6

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.

Shubham kapoor
  • 394
  • 3
  • 18
  • That's s sample JSON, I will edit the same. The problem is how to bind JSON to counter metric of Prometheus. I have seen example of counter increment when the api is called but here I want to bind json with it. – Shubham kapoor Jun 02 '21 at 12:20

1 Answers1

1

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

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • yes, this is what exactly I am looking for. so you mean to say each property of JSON will become a metric in Prometheus? Let suppose if I want to see the graph of "dskreads" and "bufreads" so I need to create two metric one for each? Also if u have some examples of an already written exporter which matches my use case please share. – Shubham kapoor Jun 03 '21 at 06:11
  • Yes to both the questions. The Prometheus Java client contains examples: https://github.com/prometheus/client_java – DazWilkin Jun 03 '21 at 12:41
  • In the meantime what i did I ran a scheduler which queries the data and post it in "/actuator/prometheus". So as soon as after running a query and getting the JSON I am doing like below @Autowired private MeterRegistry meterRegistry; and inside my cron function meterRegistry.gauge("requests_mem_test",json.getInt("dskreads")); I am successfully able to see the metric but as soon as my value changes I am getting NAN – Shubham kapoor Jun 03 '21 at 12:55