use anyhow::Result;
use telemetry_subscribers::span_latency_prom::PrometheusSpanLatencyLayer;
pub const DEFAULT_SPAN_LATENCY_BUCKETS: usize = 15;
#[derive(Debug, Clone)]
pub struct PrometheusConfig {
pub registry: ::prometheus::Registry,
pub span_latency_buckets: usize,
pub enable_span_latency: bool,
}
impl PrometheusConfig {
pub fn new(registry: ::prometheus::Registry) -> Self {
Self {
registry,
span_latency_buckets: DEFAULT_SPAN_LATENCY_BUCKETS,
enable_span_latency: true,
}
}
pub fn with_span_latency_buckets(mut self, buckets: usize) -> Self {
self.span_latency_buckets = buckets;
self
}
pub fn with_span_latency_enabled(mut self, enabled: bool) -> Self {
self.enable_span_latency = enabled;
self
}
}
pub fn init_prometheus(
prometheus_config: &PrometheusConfig,
) -> Result<Option<PrometheusSpanLatencyLayer>> {
validate_prometheus_registry(&prometheus_config.registry);
if prometheus_config.enable_span_latency {
let span_lat_layer = PrometheusSpanLatencyLayer::try_new(
&prometheus_config.registry,
prometheus_config.span_latency_buckets,
)
.map_err(|e| anyhow::anyhow!("Could not initialize span latency layer: {:?}", e))?;
Ok(Some(span_lat_layer))
} else {
Ok(None)
}
}
fn validate_prometheus_registry(_registry: &::prometheus::Registry) -> bool {
true
}