use std::sync::OnceLock;
use opentelemetry::{
KeyValue,
metrics::{Counter, Meter},
};
fn meter() -> &'static Meter {
static METER: OnceLock<Meter> = OnceLock::new();
METER.get_or_init(|| opentelemetry::global::meter("myko"))
}
macro_rules! dispatch_counter {
($fn_name:ident, $otel_name:literal, $tag_key:literal) => {
pub fn $fn_name(id: &str, origin: &str) {
static COUNTER: OnceLock<Counter<u64>> = OnceLock::new();
let counter = COUNTER.get_or_init(|| meter().u64_counter($otel_name).build());
counter.add(
1,
&[
KeyValue::new($tag_key, id.to_string()),
KeyValue::new("origin", origin.to_string()),
],
);
}
};
}
macro_rules! response_counter {
($fn_name:ident, $otel_name:literal, $tag_key:literal) => {
pub fn $fn_name(id: &str) {
static COUNTER: OnceLock<Counter<u64>> = OnceLock::new();
let counter = COUNTER.get_or_init(|| meter().u64_counter($otel_name).build());
counter.add(1, &[KeyValue::new($tag_key, id.to_string())]);
}
};
}
pub fn record_command(command_id: &str, source: &str) {
static COUNTER: OnceLock<Counter<u64>> = OnceLock::new();
let counter = COUNTER.get_or_init(|| meter().u64_counter("myko.command.count").build());
counter.add(
1,
&[
KeyValue::new("command", command_id.to_string()),
KeyValue::new("source", source.to_string()),
],
);
}
dispatch_counter!(record_query, "myko.query.count", "query");
dispatch_counter!(record_report, "myko.report.count", "report");
dispatch_counter!(record_view, "myko.view.count", "view");
response_counter!(record_query_response, "myko.query.response.count", "query");
response_counter!(
record_report_response,
"myko.report.response.count",
"report"
);
response_counter!(record_view_response, "myko.view.response.count", "view");