#[cfg(prometheus_client)]
use prometheus_client::encoding::{EncodeLabelValue, LabelValueEncoder};
pub struct Objective {
pub(crate) name: &'static str,
pub(crate) success_rate: Option<ObjectivePercentile>,
pub(crate) latency: Option<(ObjectiveLatency, ObjectivePercentile)>,
}
impl Objective {
pub const fn new(name: &'static str) -> Self {
Objective {
name,
success_rate: None,
latency: None,
}
}
pub const fn success_rate(mut self, percentile: ObjectivePercentile) -> Self {
self.success_rate = Some(percentile);
self
}
pub const fn latency(
mut self,
latency_threshold: ObjectiveLatency,
percentile: ObjectivePercentile,
) -> Self {
self.latency = Some((latency_threshold, percentile));
self
}
}
#[cfg_attr(any(prometheus_client, debug_assertions), derive(Clone, Copy))]
#[cfg_attr(prometheus_client, derive(Debug, PartialEq, Eq, Hash))]
#[non_exhaustive]
pub enum ObjectivePercentile {
P90,
P95,
P99,
P99_9,
#[cfg(feature = "custom-objective-percentile")]
Custom(&'static str),
}
impl ObjectivePercentile {
pub(crate) const fn as_str(&self) -> &'static str {
match self {
ObjectivePercentile::P90 => "90",
ObjectivePercentile::P95 => "95",
ObjectivePercentile::P99 => "99",
ObjectivePercentile::P99_9 => "99.9",
#[cfg(feature = "custom-objective-percentile")]
ObjectivePercentile::Custom(custom) => custom,
}
}
}
#[cfg(prometheus_client)]
impl EncodeLabelValue for ObjectivePercentile {
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
self.as_str().encode(encoder)
}
}
#[cfg_attr(prometheus_client, derive(Clone, Debug, PartialEq, Eq, Hash))]
#[non_exhaustive]
pub enum ObjectiveLatency {
Ms5,
Ms10,
Ms25,
Ms50,
Ms75,
Ms100,
Ms250,
Ms500,
Ms750,
Ms1000,
Ms2500,
Ms5000,
Ms7500,
Ms10000,
#[cfg(feature = "custom-objective-latency")]
Custom(&'static str),
}
impl ObjectiveLatency {
pub(crate) const fn as_str(&self) -> &'static str {
match self {
ObjectiveLatency::Ms5 => "0.005",
ObjectiveLatency::Ms10 => "0.01",
ObjectiveLatency::Ms25 => "0.025",
ObjectiveLatency::Ms50 => "0.05",
ObjectiveLatency::Ms75 => "0.075",
ObjectiveLatency::Ms100 => "0.1",
ObjectiveLatency::Ms250 => "0.25",
ObjectiveLatency::Ms500 => "0.5",
ObjectiveLatency::Ms750 => "0.75",
ObjectiveLatency::Ms1000 => "1",
ObjectiveLatency::Ms2500 => "2.5",
ObjectiveLatency::Ms5000 => "5",
ObjectiveLatency::Ms7500 => "7.5",
ObjectiveLatency::Ms10000 => "10",
#[cfg(feature = "custom-objective-latency")]
ObjectiveLatency::Custom(custom) => custom,
}
}
}
#[cfg(prometheus_client)]
impl EncodeLabelValue for ObjectiveLatency {
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
self.as_str().encode(encoder)
}
}