use super::MetricsError;
use super::labels::{ComponentLabels, OwnedGauge};
use super::names;
use super::ownership::{SeriesClaim, series_key};
use metrics::Counter;
use std::time::Duration;
#[derive(Debug)]
pub struct BackpressureMetrics {
paused: OwnedGauge,
paused_seconds: OwnedGauge,
pause_events: Counter,
inflight_bytes: OwnedGauge,
_claim: Option<SeriesClaim>,
}
impl BackpressureMetrics {
pub fn new(labels: &ComponentLabels) -> Self {
Self::build(labels, SeriesClaim::claim_or_shadow(Self::key(labels)))
}
pub fn try_new(labels: &ComponentLabels) -> Result<Self, MetricsError> {
let claim = SeriesClaim::try_claim(Self::key(labels))?;
Ok(Self::build(labels, Some(claim)))
}
fn key(labels: &ComponentLabels) -> String {
series_key("backpressure", labels, "")
}
fn build(labels: &ComponentLabels, claim: Option<SeriesClaim>) -> Self {
let owned = claim.is_some();
BackpressureMetrics {
paused: OwnedGauge::new(labels.gauge(names::BACKPRESSURE_PAUSED), owned),
paused_seconds: OwnedGauge::new(
labels.gauge(names::BACKPRESSURE_PAUSED_SECONDS_TOTAL),
owned,
),
pause_events: labels.counter(names::BACKPRESSURE_PAUSE_EVENTS_TOTAL),
inflight_bytes: OwnedGauge::new(
labels.gauge(names::BACKPRESSURE_INFLIGHT_BYTES),
owned,
),
_claim: claim,
}
}
pub fn pause_started(&self) {
self.paused.set(1.0);
self.pause_events.increment(1);
}
pub fn pause_ended(&self, paused_for: Duration) {
self.paused.set(0.0);
self.paused_seconds.increment(paused_for.as_secs_f64());
}
#[inline]
pub fn set_inflight_bytes(&self, bytes: usize) {
self.inflight_bytes.set(bytes as f64);
}
}