Skip to content

Commit 833c201

Browse files
authored
chore(layer/fastmetrics): upgrade fastmetrics to v0.7.0 (#7227)
1 parent d37828e commit 833c201

File tree

3 files changed

+48
-68
lines changed

3 files changed

+48
-68
lines changed

core/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/layers/fastmetrics/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ version = { workspace = true }
3131
all-features = true
3232

3333
[dependencies]
34-
fastmetrics = "0.6"
34+
fastmetrics = "0.7"
3535
opendal-core = { path = "../../core", version = "0.55.0", default-features = false }
3636
opendal-layer-observe-metrics-common = { path = "../observe-metrics-common", version = "0.55.0", default-features = false }
3737

core/layers/fastmetrics/src/lib.rs

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use fastmetrics::encoder::EncodeLabelSet;
2424
use fastmetrics::encoder::LabelSetEncoder;
2525
use fastmetrics::metrics::counter::Counter;
2626
use fastmetrics::metrics::family::Family;
27-
use fastmetrics::metrics::family::MetricFactory;
2827
use fastmetrics::metrics::gauge::Gauge;
2928
use fastmetrics::metrics::histogram::Histogram;
3029
use fastmetrics::raw::LabelSetSchema;
@@ -42,7 +41,8 @@ use opendal_layer_observe_metrics_common as observe;
4241
/// ## Basic Usage
4342
///
4443
/// ```no_run
45-
/// # use fastmetrics::format::text;
44+
/// # use fastmetrics::format::text::encode;
45+
/// # use fastmetrics::format::text::TextProfile;
4646
/// # use log::info;
4747
/// # use opendal_core::services;
4848
/// # use opendal_core::Operator;
@@ -69,7 +69,7 @@ use opendal_layer_observe_metrics_common as observe;
6969
///
7070
/// // Export prometheus metrics.
7171
/// let mut output = String::new();
72-
/// text::encode(&mut output, &registry).unwrap();
72+
/// encode(&mut output, &registry, TextProfile::PrometheusV0_0_4).unwrap();
7373
/// println!("{}", output);
7474
/// # Ok(())
7575
/// # }
@@ -87,7 +87,8 @@ use opendal_layer_observe_metrics_common as observe;
8787
/// ```no_run
8888
/// # use std::sync::OnceLock;
8989
/// #
90-
/// # use fastmetrics::format::text;
90+
/// # use fastmetrics::format::text::encode;
91+
/// # use fastmetrics::format::text::TextProfile;
9192
/// # use fastmetrics::registry::with_global_registry;
9293
/// # use log::info;
9394
/// # use opendal_core::services;
@@ -123,7 +124,7 @@ use opendal_layer_observe_metrics_common as observe;
123124
///
124125
/// // Export prometheus metrics.
125126
/// let mut output = String::new();
126-
/// with_global_registry(|registry| text::encode(&mut output, &registry).unwrap());
127+
/// with_global_registry(|reg| encode(&mut output, &reg, TextProfile::PrometheusV0_0_4).unwrap());
127128
/// println!("{}", output);
128129
/// # Ok(())
129130
/// # }
@@ -250,46 +251,36 @@ impl FastmetricsLayerBuilder {
250251
/// # }
251252
/// ```
252253
pub fn register(self, registry: &mut Registry) -> Result<FastmetricsLayer> {
253-
let operation_bytes = Family::new(HistogramFactory {
254-
buckets: self.bytes_buckets.clone(),
255-
});
256-
let operation_bytes_rate = Family::new(HistogramFactory {
257-
buckets: self.bytes_rate_buckets.clone(),
258-
});
259-
let operation_entries = Family::new(HistogramFactory {
260-
buckets: self.entries_buckets.clone(),
261-
});
262-
let operation_entries_rate = Family::new(HistogramFactory {
263-
buckets: self.entries_rate_buckets.clone(),
264-
});
265-
let operation_duration_seconds = Family::new(HistogramFactory {
266-
buckets: self.duration_seconds_buckets.clone(),
267-
});
254+
let Self {
255+
bytes_buckets,
256+
bytes_rate_buckets,
257+
entries_buckets,
258+
entries_rate_buckets,
259+
duration_seconds_buckets,
260+
ttfb_buckets,
261+
disable_label_root,
262+
} = self;
263+
264+
let new_hist_family = |buckets: Vec<f64>| -> Family<OperationLabels, Histogram> {
265+
Family::new(move || Histogram::new(buckets.iter().copied()))
266+
};
267+
268+
let operation_bytes = new_hist_family(bytes_buckets.clone());
269+
let operation_bytes_rate = new_hist_family(bytes_rate_buckets.clone());
270+
let operation_entries = new_hist_family(entries_buckets);
271+
let operation_entries_rate = new_hist_family(entries_rate_buckets);
272+
let operation_duration_seconds = new_hist_family(duration_seconds_buckets.clone());
268273
let operation_errors_total = Family::default();
269274
let operation_executing = Family::default();
270-
let operation_ttfb_seconds = Family::new(HistogramFactory {
271-
buckets: self.ttfb_buckets.clone(),
272-
});
275+
let operation_ttfb_seconds = new_hist_family(ttfb_buckets);
273276

274277
let http_executing = Family::default();
275-
let http_request_bytes = Family::new(HistogramFactory {
276-
buckets: self.bytes_buckets.clone(),
277-
});
278-
let http_request_bytes_rate = Family::new(HistogramFactory {
279-
buckets: self.bytes_rate_buckets.clone(),
280-
});
281-
let http_request_duration_seconds = Family::new(HistogramFactory {
282-
buckets: self.duration_seconds_buckets.clone(),
283-
});
284-
let http_response_bytes = Family::new(HistogramFactory {
285-
buckets: self.bytes_buckets.clone(),
286-
});
287-
let http_response_bytes_rate = Family::new(HistogramFactory {
288-
buckets: self.bytes_rate_buckets.clone(),
289-
});
290-
let http_response_duration_seconds = Family::new(HistogramFactory {
291-
buckets: self.duration_seconds_buckets.clone(),
292-
});
278+
let http_request_bytes = new_hist_family(bytes_buckets.clone());
279+
let http_request_bytes_rate = new_hist_family(bytes_rate_buckets.clone());
280+
let http_request_duration_seconds = new_hist_family(duration_seconds_buckets.clone());
281+
let http_response_bytes = new_hist_family(bytes_buckets);
282+
let http_response_bytes_rate = new_hist_family(bytes_rate_buckets);
283+
let http_response_duration_seconds = new_hist_family(duration_seconds_buckets);
293284
let http_connection_errors_total = Family::default();
294285
let http_status_errors_total = Family::default();
295286

@@ -313,7 +304,7 @@ impl FastmetricsLayerBuilder {
313304
http_connection_errors_total,
314305
http_status_errors_total,
315306

316-
disable_label_root: self.disable_label_root,
307+
disable_label_root,
317308
};
318309
interceptor
319310
.register(registry)
@@ -346,36 +337,25 @@ impl FastmetricsLayerBuilder {
346337
}
347338
}
348339

349-
#[derive(Clone)]
350-
struct HistogramFactory {
351-
buckets: Vec<f64>,
352-
}
353-
354-
impl MetricFactory<Histogram> for HistogramFactory {
355-
fn new_metric(&self) -> Histogram {
356-
Histogram::new(self.buckets.iter().cloned())
357-
}
358-
}
359-
360340
#[doc(hidden)]
361341
#[derive(Clone, Debug)]
362342
pub struct FastmetricsInterceptor {
363-
operation_bytes: Family<OperationLabels, Histogram, HistogramFactory>,
364-
operation_bytes_rate: Family<OperationLabels, Histogram, HistogramFactory>,
365-
operation_entries: Family<OperationLabels, Histogram, HistogramFactory>,
366-
operation_entries_rate: Family<OperationLabels, Histogram, HistogramFactory>,
367-
operation_duration_seconds: Family<OperationLabels, Histogram, HistogramFactory>,
343+
operation_bytes: Family<OperationLabels, Histogram>,
344+
operation_bytes_rate: Family<OperationLabels, Histogram>,
345+
operation_entries: Family<OperationLabels, Histogram>,
346+
operation_entries_rate: Family<OperationLabels, Histogram>,
347+
operation_duration_seconds: Family<OperationLabels, Histogram>,
368348
operation_errors_total: Family<OperationLabels, Counter>,
369349
operation_executing: Family<OperationLabels, Gauge>,
370-
operation_ttfb_seconds: Family<OperationLabels, Histogram, HistogramFactory>,
350+
operation_ttfb_seconds: Family<OperationLabels, Histogram>,
371351

372352
http_executing: Family<OperationLabels, Gauge>,
373-
http_request_bytes: Family<OperationLabels, Histogram, HistogramFactory>,
374-
http_request_bytes_rate: Family<OperationLabels, Histogram, HistogramFactory>,
375-
http_request_duration_seconds: Family<OperationLabels, Histogram, HistogramFactory>,
376-
http_response_bytes: Family<OperationLabels, Histogram, HistogramFactory>,
377-
http_response_bytes_rate: Family<OperationLabels, Histogram, HistogramFactory>,
378-
http_response_duration_seconds: Family<OperationLabels, Histogram, HistogramFactory>,
353+
http_request_bytes: Family<OperationLabels, Histogram>,
354+
http_request_bytes_rate: Family<OperationLabels, Histogram>,
355+
http_request_duration_seconds: Family<OperationLabels, Histogram>,
356+
http_response_bytes: Family<OperationLabels, Histogram>,
357+
http_response_bytes_rate: Family<OperationLabels, Histogram>,
358+
http_response_duration_seconds: Family<OperationLabels, Histogram>,
379359
http_connection_errors_total: Family<OperationLabels, Counter>,
380360
http_status_errors_total: Family<OperationLabels, Counter>,
381361

0 commit comments

Comments
 (0)