Description
Background
The Prometheus exposition format (https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#comments-help-text-and-type-information) allows exporters to specify an optional timestamp for each sample. If this is unset, the collector uses the timestamp that the sample is collected.
This timestamp is useful in a certain situation. My concrete use case is to expose GitHub API rate limit as a metric. This API rate limit is a value that decreases over time, and periodically it resets to the maximum value. We can get this API rate limit value as a HTTP response header when calling GitHub API.
Imagine a situation where there are two (physical) servers making this GitHub API call, and for the sake of illustration, let's assume that one server issues more requests and the other one does less.
Server1 (frequently issue requests) | Server2 (somehow less requests) | |
---|---|---|
Observed API limit | 100 | 4900 |
(Last time a server made a GH API call) | 3 minutes ago | 50 minutes ago |
From human's point of view, because server1 made an API call more recently than server2, we can tell that server1's observed API limit is the current value. However, without exposing the sample timestamp, Prometheus cannot distinguish which one is the most recent value. This is because when Prometheus collects sample from these two servers, when there's no timestamp specified, it treats these samples as "the samples collected now".
By exposing the sample timestamp, Prometheus should be able to treat these two samples correctly, and it can tell that server1' s value is the most recent value.
Request
With #967, client_python should learn timestamps for Gauge metrics in the multiprocessing mode. This issue is asking for an option to expose those timestamps via Prometheus format.
Code pointers
Sample already can take a timestamp (
), and so as the Prometheus exposition formatter (client_python/prometheus_client/exposition.py
Lines 191 to 194 in 249490e
For the multiprocessing mode, changing the metrics aggregation logic to propagate the timestamps (
) would suffice (based on a flag or something).For the non-multiprocessing mode, need to change this _child_samples (
client_python/prometheus_client/metrics.py
Lines 433 to 434 in 249490e