ai_lib/
metrics.rs

1use async_trait::async_trait;
2
3/// Simple, injectable metrics trait used by adapters/clients.
4/// Keep the surface minimal: counters, gauges and a timer RAII helper.
5#[async_trait]
6pub trait Metrics: Send + Sync + 'static {
7    /// Increment a named counter by `value`.
8    async fn incr_counter(&self, name: &str, value: u64);
9
10    /// Record a gauge metric.
11    async fn record_gauge(&self, name: &str, value: f64);
12
13    /// Start a timer for a named operation. Returns a boxed Timer which should be stopped
14    /// when the operation completes. Implementations may return None if timers aren't supported.
15    async fn start_timer(&self, name: &str) -> Option<Box<dyn Timer + Send>>;
16
17    /// Record a histogram value for a named metric.
18    async fn record_histogram(&self, name: &str, value: f64);
19
20    /// Record a histogram value with tags/labels.
21    async fn record_histogram_with_tags(&self, name: &str, value: f64, tags: &[(&str, &str)]);
22
23    /// Increment a counter with tags/labels.
24    async fn incr_counter_with_tags(&self, name: &str, value: u64, tags: &[(&str, &str)]);
25
26    /// Record a gauge with tags/labels.
27    async fn record_gauge_with_tags(&self, name: &str, value: f64, tags: &[(&str, &str)]);
28
29    /// Record an error occurrence.
30    async fn record_error(&self, name: &str, error_type: &str);
31
32    /// Record a success/failure boolean metric.
33    async fn record_success(&self, name: &str, success: bool);
34}
35
36/// Timer interface returned by Metrics::start_timer.
37pub trait Timer: Send {
38    /// Stop the timer and record the duration.
39    fn stop(self: Box<Self>);
40}
41
42/// No-op metrics implementation suitable as a default.
43pub struct NoopMetrics;
44
45#[async_trait]
46impl Metrics for NoopMetrics {
47    async fn incr_counter(&self, _name: &str, _value: u64) {}
48    async fn record_gauge(&self, _name: &str, _value: f64) {}
49    async fn start_timer(&self, _name: &str) -> Option<Box<dyn Timer + Send>> {
50        None
51    }
52    async fn record_histogram(&self, _name: &str, _value: f64) {}
53    async fn record_histogram_with_tags(&self, _name: &str, _value: f64, _tags: &[(&str, &str)]) {}
54    async fn incr_counter_with_tags(&self, _name: &str, _value: u64, _tags: &[(&str, &str)]) {}
55    async fn record_gauge_with_tags(&self, _name: &str, _value: f64, _tags: &[(&str, &str)]) {}
56    async fn record_error(&self, _name: &str, _error_type: &str) {}
57    async fn record_success(&self, _name: &str, _success: bool) {}
58}
59
60/// A no-op timer (returned when StartTimer implementations want to return a concrete value).
61pub struct NoopTimer;
62impl Timer for NoopTimer {
63    fn stop(self: Box<Self>) {}
64}
65
66impl NoopMetrics {
67    pub fn new() -> Self {
68        NoopMetrics
69    }
70}
71
72impl Default for NoopMetrics {
73    fn default() -> Self {
74        Self::new()
75    }
76}
77
78/// Convenience methods for common metric patterns
79#[allow(async_fn_in_trait)]
80pub trait MetricsExt: Metrics {
81    /// Record a request with timing and success/failure
82    async fn record_request(
83        &self,
84        name: &str,
85        timer: Option<Box<dyn Timer + Send>>,
86        success: bool,
87    ) {
88        if let Some(t) = timer {
89            t.stop();
90        }
91        self.record_success(name, success).await;
92    }
93
94    /// Record a request with timing, success/failure, and tags
95    async fn record_request_with_tags(
96        &self,
97        name: &str,
98        timer: Option<Box<dyn Timer + Send>>,
99        success: bool,
100        tags: &[(&str, &str)],
101    ) {
102        if let Some(t) = timer {
103            t.stop();
104        }
105        self.record_success(name, success).await;
106        // Record additional metrics with tags
107        self.incr_counter_with_tags(&format!("{}.total", name), 1, tags)
108            .await;
109        if success {
110            self.incr_counter_with_tags(&format!("{}.success", name), 1, tags)
111                .await;
112        } else {
113            self.incr_counter_with_tags(&format!("{}.failure", name), 1, tags)
114                .await;
115        }
116    }
117
118    /// Record an error with context
119    async fn record_error_with_context(&self, name: &str, error_type: &str, context: &str) {
120        self.record_error(name, error_type).await;
121        self.incr_counter_with_tags(name, 1, &[("error_type", error_type), ("context", context)])
122            .await;
123    }
124}
125
126impl<T: Metrics> MetricsExt for T {}
127
128/// Centralized metric key helpers to standardize naming across adapters
129pub mod keys {
130    /// Request counter key
131    pub fn requests(provider: &str) -> String {
132        format!("{}.requests", provider)
133    }
134    /// Request duration timer key (ms)
135    pub fn request_duration_ms(provider: &str) -> String {
136        format!("{}.request_duration_ms", provider)
137    }
138    /// Success ratio gauge/histogram can be derived; optional explicit keys
139    pub fn success(provider: &str) -> String { format!("{}.success", provider) }
140    pub fn failure(provider: &str) -> String { format!("{}.failure", provider) }
141}
142
143/// Minimal cost accounting helper (feature-gated)
144#[cfg(feature = "cost_metrics")]
145pub mod cost {
146    use crate::metrics::Metrics;
147
148    /// Compute cost using env vars like COST_INPUT_PER_1K and COST_OUTPUT_PER_1K (USD)
149    pub fn estimate_usd(input_tokens: u32, output_tokens: u32) -> f64 {
150        let in_rate = std::env::var("COST_INPUT_PER_1K").ok().and_then(|s| s.parse::<f64>().ok()).unwrap_or(0.0);
151        let out_rate = std::env::var("COST_OUTPUT_PER_1K").ok().and_then(|s| s.parse::<f64>().ok()).unwrap_or(0.0);
152        (input_tokens as f64 / 1000.0) * in_rate + (output_tokens as f64 / 1000.0) * out_rate
153    }
154
155    /// Report cost via Metrics as histogram (usd) and counters by provider/model
156    pub async fn record_cost<M: Metrics + ?Sized>(m: &M, provider: &str, model: &str, usd: f64) {
157        m.record_histogram_with_tags("cost.usd", usd, &[("provider", provider), ("model", model)]).await;
158    }
159}
160
161// Environment variables for optional features
162//
163// cost_metrics (if enabled):
164// - COST_INPUT_PER_1K: USD per 1000 input tokens
165// - COST_OUTPUT_PER_1K: USD per 1000 output tokens
166//
167// Note: In enterprise deployments (ai-lib PRO), these can be centrally managed
168// and hot-reloaded via external configuration providers.