mirror of
https://github.com/tursodatabase/libsql.git
synced 2025-06-11 01:36:54 +00:00
This commit adds a new `x-libsql-client-version` header emitted by clients. The server will collect these values and emit them as a `libsql_client_version{version="libsql-hrana-0.1.11"}`. This also exposes special doc hidden functions that our other clients that use the rust one to emit their own metric. Closes #546
91 lines
2.2 KiB
Rust
91 lines
2.2 KiB
Rust
#![allow(dead_code)]
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use metrics::{SharedString, Unit};
|
|
use metrics_util::{
|
|
debugging::{DebugValue, Snapshotter},
|
|
CompositeKey, MetricKind,
|
|
};
|
|
|
|
pub mod http;
|
|
pub mod net;
|
|
|
|
pub fn print_metrics() {
|
|
let snapshot = MetricsSnapshot::current();
|
|
|
|
eprintln!("{:?}", snapshot);
|
|
}
|
|
|
|
#[track_caller]
|
|
pub fn snapshot_metrics() -> MetricsSnapshot {
|
|
MetricsSnapshot::current()
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct MetricsSnapshot {
|
|
snapshot: HashMap<CompositeKey, (Option<Unit>, Option<SharedString>, DebugValue)>,
|
|
}
|
|
|
|
impl MetricsSnapshot {
|
|
#[track_caller]
|
|
pub fn current() -> Self {
|
|
let snapshot = Snapshotter::current_thread_snapshot()
|
|
.expect("No snapshot available")
|
|
.into_hashmap();
|
|
|
|
MetricsSnapshot { snapshot }
|
|
}
|
|
|
|
pub fn get_counter(&self, metric_name: &str) -> Option<u64> {
|
|
println!("{:?}", self.snapshot);
|
|
for (key, (_, _, val)) in &self.snapshot {
|
|
if key.kind() == MetricKind::Counter && key.key().name() == metric_name {
|
|
match val {
|
|
DebugValue::Counter(v) => return Some(*v),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn get_gauge(&self, metric_name: &str) -> Option<f64> {
|
|
for (key, (_, _, val)) in &self.snapshot {
|
|
if key.kind() == MetricKind::Gauge && key.key().name() == metric_name {
|
|
match val {
|
|
DebugValue::Gauge(v) => return Some(v.0),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn snapshot(
|
|
&self,
|
|
) -> &HashMap<CompositeKey, (Option<Unit>, Option<SharedString>, DebugValue)> {
|
|
&self.snapshot
|
|
}
|
|
|
|
#[track_caller]
|
|
pub fn assert_gauge(&self, metric_name: &str, value: f64) -> &Self {
|
|
let val = self.get_gauge(metric_name).expect("metric does not exist");
|
|
|
|
assert_eq!(val, value);
|
|
self
|
|
}
|
|
|
|
#[track_caller]
|
|
pub fn assert_counter(&self, metric_name: &str, value: u64) -> &Self {
|
|
let val = self
|
|
.get_counter(metric_name)
|
|
.expect("metric does not exist");
|
|
|
|
assert_eq!(val, value);
|
|
self
|
|
}
|
|
}
|