-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathmetrics.rs
More file actions
97 lines (89 loc) · 3.36 KB
/
metrics.rs
File metadata and controls
97 lines (89 loc) · 3.36 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use serde::Serialize;
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric::MetricName;
use torrust_tracker_metrics::metric_collection::aggregate::sum::Sum;
use torrust_tracker_metrics::metric_collection::{Error, MetricCollection};
use torrust_tracker_metrics::metric_name;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
/// Metrics collected by the tracker.
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
pub struct Metrics {
/// A collection of metrics.
pub metric_collection: MetricCollection,
}
impl Metrics {
/// # Errors
///
/// Returns an error if the metric does not exist and it cannot be created.
pub fn increase_counter(
&mut self,
metric_name: &MetricName,
labels: &LabelSet,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
self.metric_collection.increment_counter(metric_name, labels, now)
}
/// # Errors
///
/// Returns an error if the metric does not exist and it cannot be created.
pub fn set_gauge(
&mut self,
metric_name: &MetricName,
labels: &LabelSet,
value: f64,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
self.metric_collection.set_gauge(metric_name, labels, value, now)
}
}
impl Metrics {
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp4_announces_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
)
.unwrap_or_default() as u64
}
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp4_scrapes_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
)
.unwrap_or_default() as u64
}
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp6_announces_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
)
.unwrap_or_default() as u64
}
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp6_scrapes_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
)
.unwrap_or_default() as u64
}
}