|
1 | 1 | use serde::Serialize; |
2 | 2 | use torrust_tracker_metrics::label::LabelSet; |
3 | 3 | use torrust_tracker_metrics::metric::MetricName; |
| 4 | +use torrust_tracker_metrics::metric_collection::aggregate::Sum; |
4 | 5 | use torrust_tracker_metrics::metric_collection::{Error, MetricCollection}; |
| 6 | +use torrust_tracker_metrics::metric_name; |
5 | 7 | use torrust_tracker_primitives::DurationSinceUnixEpoch; |
6 | 8 |
|
| 9 | +use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL; |
| 10 | + |
7 | 11 | /// Metrics collected by the tracker. |
8 | 12 | #[derive(Debug, Clone, PartialEq, Default, Serialize)] |
9 | 13 | pub struct Metrics { |
@@ -49,3 +53,61 @@ impl Metrics { |
49 | 53 | self.metric_collection.set_gauge(metric_name, labels, value, now) |
50 | 54 | } |
51 | 55 | } |
| 56 | + |
| 57 | +impl Metrics { |
| 58 | + /// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers. |
| 59 | + #[must_use] |
| 60 | + #[allow(clippy::cast_sign_loss)] |
| 61 | + #[allow(clippy::cast_possible_truncation)] |
| 62 | + pub fn tcp4_announces_handled(&self) -> u64 { |
| 63 | + self.metric_collection |
| 64 | + .sum( |
| 65 | + &metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL), |
| 66 | + &[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(), |
| 67 | + ) |
| 68 | + .unwrap_or_default() |
| 69 | + .value() as u64 |
| 70 | + } |
| 71 | + |
| 72 | + /// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers. |
| 73 | + #[must_use] |
| 74 | + #[allow(clippy::cast_sign_loss)] |
| 75 | + #[allow(clippy::cast_possible_truncation)] |
| 76 | + pub fn tcp4_scrapes_handled(&self) -> u64 { |
| 77 | + self.metric_collection |
| 78 | + .sum( |
| 79 | + &metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL), |
| 80 | + &[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(), |
| 81 | + ) |
| 82 | + .unwrap_or_default() |
| 83 | + .value() as u64 |
| 84 | + } |
| 85 | + |
| 86 | + /// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers. |
| 87 | + #[must_use] |
| 88 | + #[allow(clippy::cast_sign_loss)] |
| 89 | + #[allow(clippy::cast_possible_truncation)] |
| 90 | + pub fn tcp6_announces_handled(&self) -> u64 { |
| 91 | + self.metric_collection |
| 92 | + .sum( |
| 93 | + &metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL), |
| 94 | + &[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(), |
| 95 | + ) |
| 96 | + .unwrap_or_default() |
| 97 | + .value() as u64 |
| 98 | + } |
| 99 | + |
| 100 | + /// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers. |
| 101 | + #[must_use] |
| 102 | + #[allow(clippy::cast_sign_loss)] |
| 103 | + #[allow(clippy::cast_possible_truncation)] |
| 104 | + pub fn tcp6_scrapes_handled(&self) -> u64 { |
| 105 | + self.metric_collection |
| 106 | + .sum( |
| 107 | + &metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL), |
| 108 | + &[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(), |
| 109 | + ) |
| 110 | + .unwrap_or_default() |
| 111 | + .value() as u64 |
| 112 | + } |
| 113 | +} |
0 commit comments