|
| 1 | +use prometheus::{HistogramVec, IntCounterVec}; |
| 2 | + |
| 3 | +use crate::components::metrics::MetricsRegistry; |
| 4 | + |
| 5 | +#[derive(Clone, Debug)] |
| 6 | +pub struct IpfsMetrics { |
| 7 | + request_count: Box<IntCounterVec>, |
| 8 | + error_count: Box<IntCounterVec>, |
| 9 | + not_found_count: Box<IntCounterVec>, |
| 10 | + request_duration: Box<HistogramVec>, |
| 11 | +} |
| 12 | + |
| 13 | +impl IpfsMetrics { |
| 14 | + pub(super) fn new(registry: &MetricsRegistry) -> Self { |
| 15 | + let request_count = registry |
| 16 | + .new_int_counter_vec( |
| 17 | + "ipfs_request_count", |
| 18 | + "The total number of IPFS requests.", |
| 19 | + &["deployment"], |
| 20 | + ) |
| 21 | + .unwrap(); |
| 22 | + |
| 23 | + let error_count = registry |
| 24 | + .new_int_counter_vec( |
| 25 | + "ipfs_error_count", |
| 26 | + "The total number of failed IPFS requests.", |
| 27 | + &["deployment"], |
| 28 | + ) |
| 29 | + .unwrap(); |
| 30 | + |
| 31 | + let not_found_count = registry |
| 32 | + .new_int_counter_vec( |
| 33 | + "ipfs_not_found_count", |
| 34 | + "The total number of IPFS requests that timed out.", |
| 35 | + &["deployment"], |
| 36 | + ) |
| 37 | + .unwrap(); |
| 38 | + |
| 39 | + let request_duration = registry |
| 40 | + .new_histogram_vec( |
| 41 | + "ipfs_request_duration", |
| 42 | + "The duration of successful IPFS requests.\n\ |
| 43 | + The time it takes to download the response body is not included.", |
| 44 | + vec!["deployment".to_owned()], |
| 45 | + vec![ |
| 46 | + 0.2, 0.5, 1.0, 5.0, 10.0, 20.0, 30.0, 60.0, 90.0, 120.0, 180.0, 240.0, |
| 47 | + ], |
| 48 | + ) |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + Self { |
| 52 | + request_count, |
| 53 | + error_count, |
| 54 | + not_found_count, |
| 55 | + request_duration, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + pub(super) fn add_request(&self, deployment_hash: &str) { |
| 60 | + self.request_count |
| 61 | + .with_label_values(&[deployment_hash]) |
| 62 | + .inc() |
| 63 | + } |
| 64 | + |
| 65 | + pub(super) fn add_error(&self, deployment_hash: &str) { |
| 66 | + self.error_count.with_label_values(&[deployment_hash]).inc() |
| 67 | + } |
| 68 | + |
| 69 | + pub(super) fn add_not_found(&self, deployment_hash: &str) { |
| 70 | + self.not_found_count |
| 71 | + .with_label_values(&[deployment_hash]) |
| 72 | + .inc() |
| 73 | + } |
| 74 | + |
| 75 | + pub(super) fn observe_request_duration(&self, deployment_hash: &str, duration_secs: f64) { |
| 76 | + self.request_duration |
| 77 | + .with_label_values(&[deployment_hash]) |
| 78 | + .observe(duration_secs.clamp(0.2, 240.0)); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(debug_assertions)] |
| 83 | +impl Default for IpfsMetrics { |
| 84 | + fn default() -> Self { |
| 85 | + Self::new(&MetricsRegistry::mock()) |
| 86 | + } |
| 87 | +} |
0 commit comments