Skip to content

feat(metrics): expose the OpenMetrics Created timestamp - #395

Merged
krisztianfekete merged 2 commits into
prometheus:masterfrom
onematchfox:feat/created-timestamps
Dec 4, 2026
Merged

krisztianfekete merged 2 commits into
prometheus:masterfrom
onematchfox:feat/created-timestamps

Conversation

@onematchfox

@onematchfox onematchfox commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Add opt-in support for the Created timestamp on Counter, ConstCounter and Histogram via with_created, in line with the OpenMetrics specification. Resolves open "Spec Compliance" note in README.md.

Metrics are encoded as _created in OpenMetrics text and as the created/start timestamp in protobuf. Metrics that don't opt in encode as before.

Comment thread src/metrics/counter.rs
#[derive(Debug)]
pub struct Counter<N = u64, A = AtomicU64> {
value: Arc<A>,
created: Option<SystemTime>,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 krisztianfekete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Two main things:

  1. 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.
  2. Keep the native-histogram reset timing separate from the created time (see inline).

Comment thread src/metrics/counter.rs
#[derive(Debug)]
pub struct Counter<N = u64, A = AtomicU64> {
value: Arc<A>,
created: Option<SystemTime>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/metrics/histogram.rs Outdated
inner.created = Some(created);
// One value, so `_created` and `start_timestamp` agree.
if let Some(native) = &mut inner.native {
native.created = created;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will delay this decision depending on feedback to #395 (comment) 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Staying opt-in, so please go ahead with the last_reset split.

Comment thread src/encoding.rs Outdated
/// 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>(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, inner.created.or(Some(last_reset)) works.

@onematchfox

Copy link
Copy Markdown
Contributor Author
  1. 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.

OK, I'm good with this and will adjust. Since you mention client_golang though, it records the created time at construction for every counter, histogram and summary, whereas I made it opt-in through with_created. Should we match that behaviour?

@onematchfox
onematchfox force-pushed the feat/created-timestamps branch from 7861962 to 556f96a Compare December 3, 2026 17:18
@krisztianfekete

Copy link
Copy Markdown
Member
  1. 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.

OK, I'm good with this and will adjust. Since you mention client_golang though, it records the created time at construction for every counter, histogram and summary, whereas I made it opt-in through with_created. Should we match that behaviour?

Let's keep it opt-in. SystemTime::now() panics on wasm32-unknown-unknown, which we target, so Counter::default() can't call it. It would also add start_timestamp to every counter's protobuf output.

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
@onematchfox
onematchfox force-pushed the feat/created-timestamps branch from 556f96a to 3468037 Compare December 4, 2026 13:03
@onematchfox

Copy link
Copy Markdown
Contributor Author

All requested changes made. LMKWYT.

@krisztianfekete krisztianfekete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both main points are addressed. A few small things left, mainly the with_created docs, then this looks good to me.

Comment thread src/metrics/counter.rs Outdated
impl<N, A> Counter<N, A> {
/// Expose `created` as the time this [`Counter`] started counting.
///
/// OpenMetrics text encodes it as a `_created` sample, OpenMetrics

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/encoding/prometheus_text.rs Outdated
&mut self,
v: &CounterValue,
_exemplar: Option<&Exemplar<S, ExemplarValue>>,
// The Prometheus text format has no created sample at present - must be

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo ("extrra"), and the format has no created sample at all. Could this go back to the one-line version?

Comment thread CHANGELOG.md Outdated
- 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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].

Comment thread README.md

- Gauge histogram metric.

- Allow "A MetricPoint in a Metric with the type [Counter, Histogram] SHOULD have a Timestamp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread src/metrics/counter.rs Outdated
}

/// The time this [`Counter`] started counting, if it exposes one.
pub fn created(&self) -> Option<SystemTime> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped

Comment thread src/metrics/histogram.rs Outdated
// TODO: Consider being generic over the bucket length.
buckets: Vec<(f64, u64)>,
native: Option<NativeHistogramState>,
// Encoded only when set through `with_created`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this comment just repeats the type, can it go?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if Option< is explicit enough for you then I'll remove.

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
@onematchfox

Copy link
Copy Markdown
Contributor Author

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é 🙏

@krisztianfekete

Copy link
Copy Markdown
Member

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?

@onematchfox

Copy link
Copy Markdown
Contributor Author

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

@krisztianfekete
krisztianfekete added this pull request to the merge queue Dec 4, 2026
Merged via the queue into prometheus:master with commit 9fbb755 Dec 4, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants