Repository navigation
feat(metrics): expose the OpenMetrics Created timestamp - #395
Conversation
| #[derive(Debug)] | ||
| pub struct Counter<N = u64, A = AtomicU64> { | ||
| value: Arc<A>, | ||
| created: Option<SystemTime>, |
There was a problem hiding this comment.
Storing this per handle increases the size of a Counter handle from 8 to 24 bytes, and clones taken before with_created don't carry it. An alternative is something like Arc<(A, Option<SystemTime>)> or an inner struct, which keeps the handle at 8 bytes and makes clones share it, but then with_created must rebuild the Arc. inc doesn't touch it either way, so I'm happy with either.
There was a problem hiding this comment.
Let's keep the field. Rebuilding the Arc in with_created would split the value between existing clones, which is worse than 16 extra bytes per handle.
krisztianfekete
left a comment
There was a problem hiding this comment.
Thanks!
Two main things:
- Let's emit the created timestamp in protobuf only for now. In OpenMetrics text, Prometheus ingests each _created line as an extra series unless a feature flag is on, so client_golang keeps those lines off by default. We can add them later behind an option.
- Keep the native-histogram reset timing separate from the created time (see inline).
| #[derive(Debug)] | ||
| pub struct Counter<N = u64, A = AtomicU64> { | ||
| value: Arc<A>, | ||
| created: Option<SystemTime>, |
There was a problem hiding this comment.
Let's keep the field. Rebuilding the Arc in with_created would split the value between existing clones, which is worse than 16 extra bytes per handle.
| inner.created = Some(created); | ||
| // One value, so `_created` and `start_timestamp` agree. | ||
| if let Some(native) = &mut inner.native { | ||
| native.created = created; |
There was a problem hiding this comment.
native.created is also where min_reset_duration starts (enforce_bucket_limit), so a past created resets on the first overflow and a future one never resets. Please keep a private last_reset for that and store the exposed value only in inner.created.
There was a problem hiding this comment.
Will delay this decision depending on feedback to #395 (comment) 😄
There was a problem hiding this comment.
Staying opt-in, so please go ahead with the last_reset split.
| /// Prometheus protobuf prefers [`NativeHistogram::created`] as the start | ||
| /// timestamp; a [`Histogram`](crate::metrics::histogram::Histogram) sets | ||
| /// both to the same value. | ||
| pub fn encode_histogram_with_native_and_created<S: EncodeLabelSet>( |
There was a problem hiding this comment.
NativeHistogram::created already has this. Could we drop this method and the .or(created) in prometheus_protobuf.rs, and have Histogram pass inner.created through NativeHistogram?
There was a problem hiding this comment.
Passing inner.created alone would drop start_timestamp from native histograms that don't call with_created - which master emits today. If we keep the opt-in, I could pass inner.created.or(Some(last_reset)) so existing output is unchanged - does that work for you? Alternatively, if we match client_golang instead (see my other comment), the created time is recorded at construction (only ConstCounter takes one) so inner.created is always set and this question goes away. WDYT?
There was a problem hiding this comment.
Yes, inner.created.or(Some(last_reset)) works.
OK, I'm good with this and will adjust. Since you mention |
7861962 to
556f96a
Compare
Let's keep it opt-in. |
Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
556f96a to
3468037
Compare
|
All requested changes made. LMKWYT. |
krisztianfekete
left a comment
There was a problem hiding this comment.
Thanks, both main points are addressed. A few small things left, mainly the with_created docs, then this looks good to me.
| impl<N, A> Counter<N, A> { | ||
| /// Expose `created` as the time this [`Counter`] started counting. | ||
| /// | ||
| /// OpenMetrics text encodes it as a `_created` sample, OpenMetrics |
There was a problem hiding this comment.
This is out of date now that text doesn't emit it. Also, "Encoding fails for a time before the Unix epoch" is only true for Prometheus protobuf: OpenMetrics protobuf encodes negative seconds.
| &mut self, | ||
| v: &CounterValue, | ||
| _exemplar: Option<&Exemplar<S, ExemplarValue>>, | ||
| // The Prometheus text format has no created sample at present - must be |
There was a problem hiding this comment.
Typo ("extrra"), and the format has no created sample at all. Could this go back to the one-line version?
| - Added `encoding::negotiate::negotiate` to choose the exposition format of a response from the `Accept` header of an HTTP request. See [PR 381]. | ||
| - Added `encoding::negotiate::MediaType::encode` to encode a `Registry` into an `io::Write` in the negotiated format. See [PR 384]. | ||
| - Added `http::ScrapeHandler` to answer scrapes over HTTP: it negotiates the format from the `Accept` header, encodes a `Registry` and returns the status, headers and body to copy into the response of any HTTP framework. See [PR 387]. | ||
| - Added `Counter::with_created`, `ConstCounter::with_created` and `Histogram::with_created` to expose the OpenMetrics Created timestamp in the protobuf formats, so ingestors can count the first increments of a new series. Metrics without it encode as before. |
There was a problem hiding this comment.
Native histograms now also send created (the last reset) in OpenMetrics protobuf, so "Metrics without it encode as before" isn't quite true. Please mention that and add See [PR 395].
|
|
||
| - Gauge histogram metric. | ||
|
|
||
| - Allow "A MetricPoint in a Metric with the type [Counter, Histogram] SHOULD have a Timestamp |
There was a problem hiding this comment.
OpenMetrics text still doesn't emit it, so could we reword this line (e.g. "Created timestamps in the OpenMetrics text format") instead of deleting it?
| } | ||
|
|
||
| /// The time this [`Counter`] started counting, if it exposes one. | ||
| pub fn created(&self) -> Option<SystemTime> { |
There was a problem hiding this comment.
Only family_series_get_their_created_time_when_first_used uses this, and that test checks the closure rather than the library. Drop both, or add the getter to Histogram and ConstCounter too?
| // TODO: Consider being generic over the bucket length. | ||
| buckets: Vec<(f64, u64)>, | ||
| native: Option<NativeHistogramState>, | ||
| // Encoded only when set through `with_created`. |
There was a problem hiding this comment.
Nit: this comment just repeats the type, can it go?
There was a problem hiding this comment.
Sure, if Option< is explicit enough for you then I'll remove.
Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
|
All comments addressed as part of f4e4f31 (assuming you will squash the PR at the end). Thank you for your patience in iterating on this - Rust is not my forté 🙏 |
No worries, thanks for iterating on it! Out of curiosity: are you using client_rust in one of your own projects, or found this gap via other OSS projects where you hit this? |
Found via agentgateway metrics |
Add opt-in support for the Created timestamp on
Counter,ConstCounterandHistogramviawith_created, in line with the OpenMetrics specification. Resolves open "Spec Compliance" note inREADME.md.Metrics are encoded
asas the created/start timestamp in protobuf. Metrics that don't opt in encode as before._createdin OpenMetrics text and