Skip to content

Commit 0e1bb0e

Browse files
committed
Pass exemplar timestamp explicitly
This is a bit more expensive when you don't pass it: ``` histogram without exemplars time: [3.1824 ns 3.2017 ns 3.2235 ns] change: [-3.6414% +0.1893% +3.1022%] (p = 0.92 > 0.05) No change in performance detected. histogram with exemplars (no exemplar passed) time: [6.2238 ns 6.2487 ns 6.2770 ns] change: [+7.5129% +8.3245% +9.0265%] (p = 0.00 < 0.05) Performance has regressed. histogram with exemplars (some exemplar passed) time: [70.907 ns 71.667 ns 72.493 ns] change: [+1.4206% +2.3509% +3.3463%] (p = 0.00 < 0.05) Performance has regressed. ``` It should amortize as one passes the same timestamp in multiple observations. Signed-off-by: Ivan Babrou <[email protected]>
1 parent cd5cdf7 commit 0e1bb0e

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

benches/exemplars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn exemplars(c: &mut Criterion) {
1919
let histogram = HistogramWithExemplars::<Exemplar>::new(BUCKETS.iter().copied());
2020

2121
b.iter(|| {
22-
histogram.observe(1.0, None);
22+
histogram.observe(1.0, None, None);
2323
});
2424
});
2525

@@ -28,7 +28,7 @@ pub fn exemplars(c: &mut Criterion) {
2828
let exemplar = vec![("TraceID".to_owned(), "deadfeed".to_owned())];
2929

3030
b.iter(|| {
31-
histogram.observe(1.0, Some(exemplar.clone()));
31+
histogram.observe(1.0, Some(exemplar.clone()), None);
3232
});
3333
});
3434
}

src/encoding/protobuf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ mod tests {
806806
let mut registry = Registry::default();
807807
let histogram = HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10));
808808
registry.register("my_histogram", "My histogram", histogram.clone());
809-
histogram.observe(1.0, Some(vec![("user_id".to_string(), 42u64)]));
809+
histogram.observe(1.0, Some(vec![("user_id".to_string(), 42u64)]), None);
810810

811811
histogram
812812
.inner

src/encoding/text.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ mod tests {
978978
let mut registry = Registry::default();
979979
let histogram = HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10));
980980
registry.register("my_histogram", "My histogram", histogram.clone());
981-
histogram.observe(1.0, Some([("user_id".to_string(), 42u64)]));
981+
histogram.observe(1.0, Some([("user_id".to_string(), 42u64)]), None);
982982

983983
histogram
984984
.inner

src/metrics/exemplar.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ where
178178
/// # use prometheus_client::metrics::exemplar::HistogramWithExemplars;
179179
/// # use prometheus_client::metrics::histogram::exponential_buckets;
180180
/// let histogram = HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10));
181-
/// histogram.observe(4.2, Some(vec![("user_id".to_string(), "42".to_string())]));
181+
/// histogram.observe(4.2, Some(vec![("user_id".to_string(), "42".to_string())]), None);
182182
/// ```
183183
/// You can also use exemplars with families. Just wrap the exemplar in a Family.
184184
/// ```
@@ -210,6 +210,7 @@ where
210210
/// Some(TraceLabel {
211211
/// trace_id: "3a2f90c9f80b894f".to_owned(),
212212
/// }),
213+
/// None,
213214
/// );
214215
/// ```
215216
#[derive(Debug)]
@@ -250,7 +251,7 @@ impl<S> HistogramWithExemplars<S> {
250251

251252
/// Observe the given value, optionally providing a label set and thus
252253
/// setting the [`Exemplar`] value.
253-
pub fn observe(&self, v: f64, label_set: Option<S>) {
254+
pub fn observe(&self, v: f64, label_set: Option<S>, timestamp: Option<SystemTime>) {
254255
let mut inner = self.inner.write();
255256
let bucket = inner.histogram.observe_and_bucket(v);
256257
if let (Some(bucket), Some(label_set)) = (bucket, label_set) {
@@ -259,7 +260,7 @@ impl<S> HistogramWithExemplars<S> {
259260
Exemplar {
260261
label_set,
261262
value: v,
262-
time: SystemTime::now(),
263+
time: timestamp.unwrap_or_else(SystemTime::now),
263264
},
264265
);
265266
}

0 commit comments

Comments
 (0)