Skip to main content

aether_telemetry/
gen_ai_metrics.rs

1use crate::genai_constants::{
2    GEN_AI_CLIENT_OPERATION_DURATION, GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK,
3    GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK, GEN_AI_CLIENT_TOKEN_USAGE,
4};
5use opentelemetry::metrics::{Histogram, Meter};
6
7#[derive(Clone)]
8pub struct GenAiMetrics {
9    pub(crate) duration: Histogram<f64>,
10    pub(crate) time_to_first_chunk: Histogram<f64>,
11    pub(crate) time_per_output_chunk: Histogram<f64>,
12    pub(crate) token_usage: Histogram<u64>,
13}
14
15impl GenAiMetrics {
16    pub fn new(meter: &Meter) -> Self {
17        Self {
18            duration: meter
19                .f64_histogram(GEN_AI_CLIENT_OPERATION_DURATION)
20                .with_description("GenAI operation duration")
21                .with_unit("s")
22                .build(),
23
24            time_to_first_chunk: meter
25                .f64_histogram(GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK)
26                .with_description("Time from request start until the first response chunk")
27                .with_unit("s")
28                .build(),
29
30            time_per_output_chunk: meter
31                .f64_histogram(GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK)
32                .with_description("Elapsed time between consecutive output chunks")
33                .with_unit("s")
34                .build(),
35
36            token_usage: meter
37                .u64_histogram(GEN_AI_CLIENT_TOKEN_USAGE)
38                .with_description("Number of input and output tokens used")
39                .with_unit("{token}")
40                .build(),
41        }
42    }
43}