forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.rs
More file actions
54 lines (43 loc) · 1.26 KB
/
Copy pathrepository.rs
File metadata and controls
54 lines (43 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::sync::Arc;
use tokio::sync::{RwLock, RwLockReadGuard};
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric::MetricName;
use torrust_tracker_metrics::metric_collection::Error;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use super::describe_metrics;
use super::metrics::Metrics;
/// A repository for the tracker metrics.
#[derive(Clone)]
pub struct Repository {
pub stats: Arc<RwLock<Metrics>>,
}
impl Default for Repository {
fn default() -> Self {
Self::new()
}
}
impl Repository {
#[must_use]
pub fn new() -> Self {
let stats = Arc::new(RwLock::new(describe_metrics()));
Self { stats }
}
pub async fn get_stats(&self) -> RwLockReadGuard<'_, Metrics> {
self.stats.read().await
}
/// # Errors
///
/// This function will return an error if the metric collection fails to
/// increase the counter.
pub async fn increase_counter(
&self,
metric_name: &MetricName,
labels: &LabelSet,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
let mut stats_lock = self.stats.write().await;
let result = stats_lock.increase_counter(metric_name, labels, now);
drop(stats_lock);
result
}
}