endclothing / prometheus_client_php

Prometheus instrumentation library for PHP applications
https://prometheus.io/docs/concepts/metric_types/
Apache License 2.0
150 stars 77 forks source link

Pushing multiple values for the same metric sequentially #53

Open pavlepredic opened 4 years ago

pavlepredic commented 4 years ago

Perhaps I'm misunderstanding some fundamental concept here, but it seems to me there is a bug in the way metrics are handled by provided adapters. I would expect that setting the value of the gauge metric to two different values in sequence would result in both those values being present in the MetricFamilySamples object. Instead, the second value overwrites the first one. Also, no timestamp seems to be included with any of the metrics, so I'm guessing Prometheus will assign the timestamp when scraping. This all means that, in order to have accurate metrics, you need to configure Prometheus to scrape your data once every second, which does not seem very efficient.

Here's a test case I wrote to demonstrate what I'm saying: https://github.com/pavlepredic/prometheus_client_php_test/blob/master/src/GaugeTest.php. In the test, I use InMemory adapter (because it's easier to set up), but I'm getting the same behaviour with Redis adapter.

Am I missing something or is there a design flaw in the library?

skydiablo commented 4 years ago

try to add an label with current datetime. you will get new entry for any new combination of label-values...

pavlepredic commented 4 years ago

try to add an label with current datetime. you will get new entry for any new combination of label-values...

But that label will not act as the metric timestamp, right? Prometheus will still assign the same timestamp to all those metrics based on the time of scraping. Or am I wrong?

skydiablo commented 4 years ago

yes, prometheus use the scraping timestamp. but i have also an PR (https://github.com/endclothing/prometheus_client_php/pull/54) to handle this, so you can set your own TS.

on the other hand, you have to strip this datetime-label so that prometheus/grafana can better handle this samples and recognize that this is one stream:

scrape_configs:
  - job_name: 'XXXXXXXX'
# ...
    metric_relabel_configs:
      - regex: 'datetime' 
        action: labeldrop