Skip to content

Commit ce149cb

Browse files
committed
feat: allow describing metrics
1 parent 9a95fef commit ce149cb

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::sync::Arc;
22

33
use tokio::sync::{RwLock, RwLockReadGuard};
44
use torrust_tracker_metrics::label::LabelSet;
5+
use torrust_tracker_metrics::metric::description::MetricDescription;
56
use torrust_tracker_metrics::metric::MetricName;
7+
use torrust_tracker_metrics::unit::Unit;
68
use torrust_tracker_primitives::DurationSinceUnixEpoch;
79

810
use super::metrics::Metrics;
@@ -22,9 +24,9 @@ impl Default for Repository {
2224
impl Repository {
2325
#[must_use]
2426
pub fn new() -> Self {
25-
Self {
26-
stats: Arc::new(RwLock::new(Metrics::default())),
27-
}
27+
let stats = Arc::new(RwLock::new(Self::describe_metrics()));
28+
29+
Self { stats }
2830
}
2931

3032
pub async fn get_stats(&self) -> RwLockReadGuard<'_, Metrics> {
@@ -60,4 +62,22 @@ impl Repository {
6062
stats_lock.increase_counter(metric_name, labels, now);
6163
drop(stats_lock);
6264
}
65+
66+
fn describe_metrics() -> Metrics {
67+
let mut metrics = Metrics::default();
68+
69+
metrics.metric_collection.describe_counter(
70+
&MetricName::new("http_tracker_core_announce_requests_received_total"),
71+
Some(Unit::Count),
72+
Some(MetricDescription::new("Total number of announce requests received")),
73+
);
74+
75+
metrics.metric_collection.describe_counter(
76+
&MetricName::new("http_tracker_core_scrape_requests_received_total"),
77+
Some(Unit::Count),
78+
Some(MetricDescription::new("Total number of scrape requests received")),
79+
);
80+
81+
metrics
82+
}
6383
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use derive_more::Display;
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Debug, Display, Clone, Eq, PartialEq, Default, Deserialize, Serialize, Hash, Ord, PartialOrd)]
5+
pub struct MetricDescription(String);
6+
7+
impl MetricDescription {
8+
#[must_use]
9+
pub fn new(name: &str) -> Self {
10+
Self(name.to_owned())
11+
}
12+
}

packages/metrics/src/metric/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod description;
12
pub mod name;
23

34
use serde::{Deserialize, Serialize};

packages/metrics/src/metric_collection.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use super::gauge::Gauge;
1010
use super::label::LabelSet;
1111
use super::metric::{Metric, MetricName};
1212
use super::prometheus::PrometheusSerializable;
13+
use crate::metric::description::MetricDescription;
1314
use crate::sample_collection::SampleCollection;
15+
use crate::unit::Unit;
1416

1517
#[derive(Debug, Default)]
1618
pub struct Metrics {
@@ -27,6 +29,16 @@ impl Metrics {
2729

2830
// Counter-specific methods
2931

32+
/// # Panics
33+
///
34+
/// Panics if it can't get write access to the inner collection.
35+
pub fn describe_counter(&mut self, name: &MetricName, _opt_unit: Option<Unit>, _opt_description: Option<MetricDescription>) {
36+
self.inner.write().unwrap().counters.ensure_metric_exists(name);
37+
}
38+
39+
/// It allows to describe a counter metric so the metrics appear in the JSON
40+
/// response even if there are no samples yet.
41+
///
3042
/// # Panics
3143
///
3244
/// Panics if it can't get read access to the inner collection.
@@ -44,6 +56,16 @@ impl Metrics {
4456

4557
// Gauge-specific methods
4658

59+
/// It allows to describe a gauge metric so the metrics appear in the JSON
60+
/// response even if there are no samples yet.
61+
///
62+
/// # Panics
63+
///
64+
/// Panics if it can't get write access to the inner collection.
65+
pub fn describe_gauge(&mut self, name: &MetricName, _opt_unit: Option<Unit>, _opt_description: Option<MetricDescription>) {
66+
self.inner.write().unwrap().gauges.ensure_metric_exists(name);
67+
}
68+
4769
/// # Panics
4870
///
4971
/// Panics if it can't get read access to the inner collection.
@@ -89,6 +111,10 @@ impl MetricCollection {
89111

90112
// Counter-specific methods
91113

114+
pub fn describe_counter(&mut self, name: &MetricName, _opt_unit: Option<Unit>, _opt_description: Option<MetricDescription>) {
115+
self.counters.ensure_metric_exists(name);
116+
}
117+
92118
#[must_use]
93119
pub fn get_counter_value(&self, name: &MetricName, labels: &LabelSet) -> Counter {
94120
self.counters.get_value(name, labels)
@@ -108,6 +134,10 @@ impl MetricCollection {
108134

109135
// Gauge-specific methods
110136

137+
pub fn describe_gauge(&mut self, name: &MetricName, _opt_unit: Option<Unit>, _opt_description: Option<MetricDescription>) {
138+
self.gauges.ensure_metric_exists(name);
139+
}
140+
111141
#[must_use]
112142
pub fn get_gauge_value(&self, name: &MetricName, labels: &LabelSet) -> Gauge {
113143
self.gauges.get_value(name, labels)

0 commit comments

Comments
 (0)