use opentelemetry::global;
use opentelemetry::metrics::{Counter, Histogram, UpDownCounter};
#[derive(Clone, Debug)]
pub struct VlorqMetrics {
pub query_counter: Counter<u64>,
pub query_duration_histogram: Histogram<f64>,
pub error_counter: Counter<u64>,
pub llm_duration_histogram: Histogram<f64>,
pub cache_hit_counter: Counter<u64>,
pub cache_miss_counter: Counter<u64>,
pub schema_cache_hits: Counter<u64>,
pub schema_cache_misses: Counter<u64>,
pub llm_prompt_tokens: Counter<u64>,
pub llm_completion_tokens: Counter<u64>,
pub active_queries: UpDownCounter<i64>,
}
impl VlorqMetrics {
#[must_use]
pub fn new() -> Self {
let meter = global::meter("vlorql");
Self {
query_counter: meter.u64_counter("vlorql.queries.total").build(),
query_duration_histogram: meter.f64_histogram("vlorql.query.duration").build(),
error_counter: meter.u64_counter("vlorql.errors.total").build(),
llm_duration_histogram: meter.f64_histogram("vlorql.llm.duration").build(),
cache_hit_counter: meter.u64_counter("vlorql.cache.hits").build(),
cache_miss_counter: meter.u64_counter("vlorql.cache.misses").build(),
schema_cache_hits: meter.u64_counter("vlorql.schema_cache.hits").build(),
schema_cache_misses: meter.u64_counter("vlorql.schema_cache.misses").build(),
llm_prompt_tokens: meter.u64_counter("vlorql.llm.prompt_tokens").build(),
llm_completion_tokens: meter.u64_counter("vlorql.llm.completion_tokens").build(),
active_queries: meter.i64_up_down_counter("vlorql.queries.active").build(),
}
}
}
impl Default for VlorqMetrics {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use opentelemetry::global;
fn setup_metrics() -> VlorqMetrics {
let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder().build();
global::set_meter_provider(provider);
VlorqMetrics::new()
}
#[test]
fn query_counter_does_not_panic() {
let m = setup_metrics();
m.query_counter.add(1, &[]);
m.query_counter.add(5, &[]);
}
#[test]
fn query_duration_histogram_does_not_panic() {
let m = setup_metrics();
m.query_duration_histogram.record(0.042, &[]);
m.query_duration_histogram.record(1.5, &[]);
}
#[test]
fn error_counter_does_not_panic() {
let m = setup_metrics();
m.error_counter.add(
1,
&[opentelemetry::KeyValue::new("error_type", "validation")],
);
m.error_counter
.add(1, &[opentelemetry::KeyValue::new("error_type", "llm")]);
}
#[test]
fn llm_duration_histogram_does_not_panic() {
let m = setup_metrics();
m.llm_duration_histogram.record(0.3, &[]);
m.llm_duration_histogram.record(2.1, &[]);
}
#[test]
fn cache_hit_counter_does_not_panic() {
let m = setup_metrics();
m.cache_hit_counter.add(1, &[]);
}
#[test]
fn cache_miss_counter_does_not_panic() {
let m = setup_metrics();
m.cache_miss_counter.add(1, &[]);
}
#[test]
fn llm_prompt_tokens_does_not_panic() {
let m = setup_metrics();
m.llm_prompt_tokens.add(42, &[]);
}
#[test]
fn llm_completion_tokens_does_not_panic() {
let m = setup_metrics();
m.llm_completion_tokens.add(128, &[]);
}
#[test]
fn active_queries_updown_does_not_panic() {
let m = setup_metrics();
m.active_queries.add(1, &[]); m.active_queries.add(-1, &[]); }
#[test]
fn default_creates_usable_instance() {
let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder().build();
global::set_meter_provider(provider);
let m = VlorqMetrics::default();
m.query_counter.add(1, &[]);
}
}