use std::sync::OnceLock;
use haproxy_api::Txn;
use mlua::prelude::LuaString;
use opentelemetry::{Context, TraceId};
static TRACE_CACHE: OnceLock<quick_cache::sync::Cache<String, Context>> = OnceLock::new();
fn init_cache() -> quick_cache::sync::Cache<String, Context> {
quick_cache::sync::Cache::new(1_000_000)
}
pub(crate) fn get_context(txn: &Txn) -> Option<Context> {
let trace_id = txn.get_var::<LuaString>("txn.otel_trace_id").ok()?;
TRACE_CACHE
.get_or_init(init_cache)
.get(&*trace_id.to_str().ok()?)
}
pub(crate) fn store_context(txn: &Txn, trace_id: TraceId, context: Context) {
let trace_id = const_hex::encode(trace_id.to_bytes());
let _ = txn.set_var("txn.otel_trace_id", &*trace_id);
TRACE_CACHE
.get_or_init(init_cache)
.insert(trace_id, context);
}
pub(crate) fn remove_context(txn: &Txn) -> Option<Context> {
let trace_id = txn.get_var::<LuaString>("txn.otel_trace_id").ok()?;
TRACE_CACHE
.get_or_init(init_cache)
.remove(&*trace_id.to_str().ok()?)
.map(|(_, context)| context)
}