spin_telemetry/
detector.rsuse std::{env, time::Duration};
use opentelemetry::{Key, KeyValue, Value};
use opentelemetry_sdk::{
resource::{EnvResourceDetector, ResourceDetector},
Resource,
};
const OTEL_SERVICE_NAME: &str = "OTEL_SERVICE_NAME";
#[derive(Debug)]
pub struct SpinResourceDetector {
spin_version: String,
}
impl SpinResourceDetector {
pub fn new(spin_version: String) -> Self {
SpinResourceDetector { spin_version }
}
}
impl ResourceDetector for SpinResourceDetector {
fn detect(&self, _timeout: Duration) -> Resource {
let service_name = env::var(OTEL_SERVICE_NAME)
.ok()
.filter(|s| !s.is_empty())
.map(Value::from)
.or_else(|| {
EnvResourceDetector::new()
.detect(Duration::from_secs(0))
.get(Key::new("service.name"))
})
.unwrap_or_else(|| "spin".into());
Resource::new(vec![
KeyValue::new("service.name", service_name),
KeyValue::new("service.version", self.spin_version.clone()),
])
}
}