refactor: implement Default
@@ -36,6 +36,29 @@ pub struct MetricRegistry {
impl MetricRegistry {
/// initialise a new metrics registry.
pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Returns the data in the Prom exposition format.
+ pub fn metrics_as_text(&self) -> Vec<u8> {
+ let metric_families = dbg!(self.exporter.registry().gather());
+ let mut result = Vec::new();
+ TextEncoder::new()
+ .encode(&metric_families, &mut result)
+ .unwrap();
+ result
+ }
+
+ /// This method should be used to register a new domain such as a
+ /// sub-system, crate or similar.
+ pub fn register_domain(&self, name: &'static str) -> Domain {
+ let meter = self.provider.meter(name, None);
+ Domain::new(name, meter)
+ }
+}
+
+impl Default for MetricRegistry {
+ fn default() -> Self {
let registry = Registry::new();
let default_histogram_boundaries = vec![0.5, 0.9, 0.99];
let selector = Box::new(Selector::Histogram(default_histogram_boundaries.clone()));
@@ -48,6 +71,7 @@ impl MetricRegistry {
// Initialise the prometheus exporter
let default_summary_quantiles = vec![0.5, 0.9, 0.99];
+
let exporter = PrometheusExporter::new(
registry,
controller,
@@ -60,23 +84,6 @@ impl MetricRegistry {
Self { provider, exporter }
}
-
- /// Returns the data in the Prom exposition format.
- pub fn metrics_as_text(&self) -> Vec<u8> {
- let metric_families = dbg!(self.exporter.registry().gather());
- let mut result = Vec::new();
- TextEncoder::new()
- .encode(&metric_families, &mut result)
- .unwrap();
- result
- }
-
- /// This method should be used to register a new domain such as a
- /// sub-system, crate or similar.
- pub fn register_domain(&self, name: &'static str) -> Domain {
- let meter = self.provider.meter(name, None);
- Domain::new(name, meter)
- }
}
/// A `Domain` provides a namespace that describes a collection of related
@@ -142,7 +149,7 @@ impl Domain {
let duration = self
.meter
- .f64_value_recorder(self.build_metric_prefix("request.duration.seconds", name.clone()))
+ .f64_value_recorder(self.build_metric_prefix("request.duration.seconds", name))
.with_description("distribution of request latencies")
.init();
