pub(crate) mod layer;
pub(crate) mod span_ext;
pub(crate) use layer::OpenTelemetryLayer;
pub(crate) use layer::layer;
use opentelemetry::Key;
use opentelemetry::KeyValue;
use opentelemetry::Value;
use opentelemetry::trace::TraceContextExt;
pub(crate) use span_ext::OpenTelemetrySpanExt;
use super::utils::upsert_attribute;
#[derive(Debug, Clone, Default)]
pub(crate) struct OtelData {
pub(crate) current_cx: opentelemetry::Context,
pub(crate) attributes: Vec<KeyValue>,
pub(crate) original_name: &'static str,
#[cfg(not(test))]
pub(crate) event_attributes: Option<ahash::HashMap<Key, Value>>,
#[cfg(test)]
pub(crate) event_attributes: Option<indexmap::IndexMap<Key, Value>>,
pub(crate) forced_status: Option<opentelemetry::trace::Status>,
pub(crate) forced_span_name: Option<String>,
}
impl OtelData {
pub(crate) fn upsert_attribute(&mut self, kv: KeyValue) {
upsert_attribute(&mut self.attributes, kv.clone());
self.current_cx.span().set_attribute(kv);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn upsert_attribute_replaces_same_key() {
let mut otel_data = OtelData::default();
otel_data.upsert_attribute(KeyValue::new("cache.status", "MISS"));
otel_data.upsert_attribute(KeyValue::new("cache.status", "HIT"));
assert_eq!(
otel_data.attributes,
vec![KeyValue::new("cache.status", "HIT")]
);
}
}