|
| 1 | +use serde::Serialize; |
| 2 | +use torrust_tracker_metrics::label::LabelSet; |
| 3 | +use torrust_tracker_metrics::metric::MetricName; |
| 4 | +use torrust_tracker_metrics::metric_collection::{Error, MetricCollection}; |
| 5 | +use torrust_tracker_primitives::DurationSinceUnixEpoch; |
| 6 | + |
| 7 | +/// Metrics collected by the torrent repository. |
| 8 | +#[derive(Debug, Clone, PartialEq, Default, Serialize)] |
| 9 | +pub struct Metrics { |
| 10 | + /// A collection of metrics. |
| 11 | + pub metric_collection: MetricCollection, |
| 12 | +} |
| 13 | + |
| 14 | +impl Metrics { |
| 15 | + /// # Errors |
| 16 | + /// |
| 17 | + /// Returns an error if the metric does not exist and it cannot be created. |
| 18 | + pub fn increment_counter( |
| 19 | + &mut self, |
| 20 | + metric_name: &MetricName, |
| 21 | + labels: &LabelSet, |
| 22 | + now: DurationSinceUnixEpoch, |
| 23 | + ) -> Result<(), Error> { |
| 24 | + self.metric_collection.increase_counter(metric_name, labels, now) |
| 25 | + } |
| 26 | + |
| 27 | + /// # Errors |
| 28 | + /// |
| 29 | + /// Returns an error if the metric does not exist and it cannot be created. |
| 30 | + pub fn set_gauge( |
| 31 | + &mut self, |
| 32 | + metric_name: &MetricName, |
| 33 | + labels: &LabelSet, |
| 34 | + value: f64, |
| 35 | + now: DurationSinceUnixEpoch, |
| 36 | + ) -> Result<(), Error> { |
| 37 | + self.metric_collection.set_gauge(metric_name, labels, value, now) |
| 38 | + } |
| 39 | + |
| 40 | + /// # Errors |
| 41 | + /// |
| 42 | + /// Returns an error if the metric does not exist and it cannot be created. |
| 43 | + pub fn increment_gauge( |
| 44 | + &mut self, |
| 45 | + metric_name: &MetricName, |
| 46 | + labels: &LabelSet, |
| 47 | + now: DurationSinceUnixEpoch, |
| 48 | + ) -> Result<(), Error> { |
| 49 | + self.metric_collection.increment_gauge(metric_name, labels, now) |
| 50 | + } |
| 51 | + |
| 52 | + /// # Errors |
| 53 | + /// |
| 54 | + /// Returns an error if the metric does not exist and it cannot be created. |
| 55 | + pub fn decrement_gauge( |
| 56 | + &mut self, |
| 57 | + metric_name: &MetricName, |
| 58 | + labels: &LabelSet, |
| 59 | + now: DurationSinceUnixEpoch, |
| 60 | + ) -> Result<(), Error> { |
| 61 | + self.metric_collection.decrement_gauge(metric_name, labels, now) |
| 62 | + } |
| 63 | +} |
0 commit comments