rust-prometheus icon indicating copy to clipboard operation
rust-prometheus copied to clipboard

How to properly set timestamp?

Open svenstaro opened this issue 4 years ago • 4 comments

I've been trying to set a timestamp on my metrics but I don't see an obvious way. What I ended up doing was

let gauge = MONITOR_LATENCY_SECONDS_GAUGE.with_label_values(&["blah]);
gauge.set(attribute_value);
gauge.metric().set_timestamp_ms(1234));

but that didn't really seem to set anything in the output. The API for this is obviously there but I can't find any examples or docs about this.

svenstaro avatar Jan 07 '21 20:01 svenstaro

gauge.metrics() will return a snapshot of the metric, gauge.metric().set_timestamp_ms(1234)); sets the timestamp on that snapshot, not on the metric observation.

Long story short, timestamp functionality is part of the data model, but not supported by rust-prometheus today.

Following your issue linked here (https://github.com/svenstaro/site24x7_exporter/issues/75), I would recommend scraping your monitored target whenever Prometheus scrapes your exporter. See https://prometheus.io/docs/instrumenting/writing_exporters/#scheduling for details.

Would that be an option for you?

mxinden avatar Jan 08 '21 10:01 mxinden

Thanks for your explanation of why my approach doesn't work, that makes sense. I suspected as much.

Sadly that's not an option as the API backend asynchronously does those monitoring checks whenever it feels like it and a run might take some minutes. I can only poll and hope I don't miss any data. Works pretty well the way I have it actually so I'd just need manual timestamps.

svenstaro avatar Jan 10 '21 01:01 svenstaro

For the record, I wouldn't be opposed to a pull request that would add support for timestamps.

mxinden avatar Jan 14 '21 10:01 mxinden

Thanks for filing this, this confirms my suspicion that the timestamp-related code wasn't plumbed all the way through yet. :)

I'd love to have this, my main use for it is when writing exporters that pass through from elsewhere (for example wireless sensors). It's good to be able to timestamp all datapoints since at /metrics scrape time they're already known to be stale.

(I'd send a PR but this is my first experimental Rust project, give it time. :-)

Wilm0r avatar Mar 07 '21 21:03 Wilm0r