Skip to content

Commit 51f196c

Browse files
committed
refact: add lock to metrics to avoid names duplication in different types
1 parent fe88861 commit 51f196c

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

packages/http-tracker-core/src/statistics/metrics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ pub struct Metrics {
2525

2626
impl Metrics {
2727
pub fn increase_counter(&mut self, metric_name: &MetricName, labels: &LabelSet, now: DurationSinceUnixEpoch) {
28-
tracing::info!("Increasing counter for metric: {metric_name} with labels: {labels:?}");
29-
3028
self.metric_collection.increase_counter(metric_name, labels, now);
3129
}
30+
31+
pub fn set_gauge(&mut self, metric_name: &MetricName, labels: &LabelSet, value: f64, now: DurationSinceUnixEpoch) {
32+
self.metric_collection.set_gauge(metric_name, labels, value, now);
33+
}
3234
}

packages/metrics/src/metric_collection.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::{HashMap, HashSet};
2+
use std::sync::RwLock;
23

34
use serde::ser::{SerializeSeq, Serializer};
45
use serde::{Deserialize, Deserializer, Serialize};
@@ -11,6 +12,57 @@ use super::metric::{Metric, MetricName};
1112
use super::prometheus::PrometheusSerializable;
1213
use crate::sample_collection::SampleCollection;
1314

15+
#[derive(Debug, Default)]
16+
pub struct Metrics {
17+
inner: RwLock<MetricCollection>,
18+
}
19+
20+
impl Metrics {
21+
#[must_use]
22+
pub fn new(counters: MetricKindCollection<Counter>, gauges: MetricKindCollection<Gauge>) -> Self {
23+
Self {
24+
inner: RwLock::new(MetricCollection::new(counters, gauges)),
25+
}
26+
}
27+
28+
// Counter-specific methods
29+
30+
/// # Panics
31+
///
32+
/// Panics if it can't get read access to the inner collection.
33+
#[must_use]
34+
pub fn get_counter_value(&self, name: &MetricName, labels: &LabelSet) -> Counter {
35+
self.inner.read().unwrap().counters.get_value(name, labels)
36+
}
37+
38+
/// # Panics
39+
///
40+
/// Panics if it can't get write access to the inner collection.
41+
pub fn increase_counter(&mut self, name: &MetricName, labels: &LabelSet, time: DurationSinceUnixEpoch) {
42+
self.inner.write().unwrap().increase_counter(name, labels, time);
43+
}
44+
45+
// Gauge-specific methods
46+
47+
/// # Panics
48+
///
49+
/// Panics if it can't get read access to the inner collection.
50+
#[must_use]
51+
pub fn get_gauge_value(&self, name: &MetricName, labels: &LabelSet) -> Gauge {
52+
self.inner.read().unwrap().gauges.get_value(name, labels)
53+
}
54+
55+
/// # Panics
56+
///
57+
/// Panics if it can't get write access to the inner collection.
58+
pub fn set_gauge(&mut self, name: &MetricName, labels: &LabelSet, value: f64, time: DurationSinceUnixEpoch) {
59+
self.inner.write().unwrap().set_gauge(name, labels, value, time);
60+
}
61+
}
62+
63+
/// Use this type only when behind a lock that guarantees thread-safety.
64+
/// Otherwise, there could be race conditions that lead to duplicate metric
65+
/// names in different metric types.
1466
#[derive(Debug, Clone, Default, PartialEq)]
1567
pub struct MetricCollection {
1668
counters: MetricKindCollection<Counter>,

0 commit comments

Comments
 (0)