Skip to content

Commit ec95039

Browse files
committed
refactor: make Metric fields private
1 parent 21575c8 commit ec95039

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

packages/metrics/src/metric.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use super::label_set::LabelSet;
77
use super::prometheus::PrometheusSerializable;
88
use super::sample::Sample;
99
use super::sample_collection::SampleCollection;
10+
use crate::gauge::Gauge;
1011

1112
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
1213
pub struct Metric<T> {
1314
name: MetricName,
1415

1516
#[serde(rename = "samples")]
16-
pub sample_collection: SampleCollection<T>,
17+
sample_collection: SampleCollection<T>,
1718
}
1819

1920
impl<T> Metric<T> {
@@ -42,6 +43,12 @@ impl Metric<Counter> {
4243
}
4344
}
4445

46+
impl Metric<Gauge> {
47+
pub fn set(&mut self, labels: &LabelSet, value: f64, time: DurationSinceUnixEpoch) {
48+
self.sample_collection.set(labels, value, time);
49+
}
50+
}
51+
4552
impl<T: PrometheusSerializable> PrometheusSerializable for Metric<T> {
4653
fn to_prometheus(&self) -> String {
4754
let mut output = String::new();

packages/metrics/src/metric_collection.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use super::gauge::Gauge;
99
use super::label_set::LabelSet;
1010
use super::metric::{Metric, MetricName};
1111
use super::prometheus::PrometheusSerializable;
12-
use super::sample::Sample;
1312
use crate::sample_collection::SampleCollection;
1413

1514
#[derive(Debug, Clone, Default, PartialEq)]
@@ -160,11 +159,11 @@ impl MetricKindCollection<Counter> {
160159
///
161160
/// # Panics
162161
///
163-
/// Panics if the metric name already exists in the collection.
162+
/// Panics if the metric does not exist and it could not be created.
164163
pub fn increment(&mut self, name: &MetricName, labels: &LabelSet, time: DurationSinceUnixEpoch) {
165164
self.crate_metric_if_does_not_exist(name);
166165

167-
let metric = self.metrics.get_mut(name).unwrap();
166+
let metric = self.metrics.get_mut(name).expect("Counter metric should exist");
168167

169168
metric.increment(labels, time);
170169
}
@@ -179,29 +178,19 @@ impl MetricKindCollection<Counter> {
179178
}
180179

181180
impl MetricKindCollection<Gauge> {
181+
/// Sets the gauge for the given metric name and labels.
182+
///
183+
/// If the metric name does not exist, it will be created.
184+
///
182185
/// # Panics
183186
///
184-
/// Panics if the metric name already exists in the collection.
187+
/// Panics if the metric does not exist and it could not be created.
185188
pub fn set(&mut self, name: &MetricName, labels: &LabelSet, value: f64, time: DurationSinceUnixEpoch) {
186189
self.crate_metric_if_does_not_exist(name);
187190

188-
let metric = self.metrics.get_mut(name).unwrap();
191+
let metric = self.metrics.get_mut(name).expect("Gauge metric should exist");
189192

190-
// Use entry API to handle existing or new sample
191-
match metric.sample_collection.samples.entry(labels.clone()) {
192-
std::collections::hash_map::Entry::Occupied(mut entry) => {
193-
let sample = entry.get_mut();
194-
sample.value.set(value);
195-
sample.update_at = time;
196-
}
197-
std::collections::hash_map::Entry::Vacant(entry) => {
198-
entry.insert(Sample {
199-
value: Gauge::new(value),
200-
update_at: time,
201-
labels: labels.clone(),
202-
});
203-
}
204-
}
193+
metric.set(labels, value, time);
205194
}
206195

207196
#[must_use]

0 commit comments

Comments
 (0)