diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index 03c2c60..a4cba41 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -18,12 +18,14 @@ use std::time::SystemTime; use log::Record; use opentelemetry::logs::AnyValue; +use opentelemetry::logs::LogRecord as _; use opentelemetry::logs::Logger; use opentelemetry::logs::LoggerProvider as ILoggerProvider; use opentelemetry::logs::Severity; use opentelemetry::InstrumentationLibrary; use opentelemetry_otlp::Protocol; use opentelemetry_otlp::WithExportConfig; +use opentelemetry_sdk::logs::LogRecord; use opentelemetry_sdk::logs::LoggerProvider; use crate::append::Append; @@ -111,8 +113,40 @@ impl Append for OpentelemetryLog { record.observed_timestamp = Some(SystemTime::now()); record.severity_number = Some(log_level_to_otel_severity(log_record.level())); record.severity_text = Some(log_record.level().as_str().into()); + record.target = Some(log_record.target().to_string().into()); record.body = Some(AnyValue::from(log_record.args().to_string())); + if let Some(module_path) = log_record.module_path() { + record.add_attribute("module_path", module_path.to_string()); + } + if let Some(file) = log_record.file() { + record.add_attribute("file", file.to_string()); + } + if let Some(line) = log_record.line() { + record.add_attribute("line", line); + } + + struct KvExtractor<'a> { + record: &'a mut LogRecord, + } + + impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvExtractor<'a> { + fn visit_pair( + &mut self, + key: log::kv::Key<'kvs>, + value: log::kv::Value<'kvs>, + ) -> Result<(), log::kv::Error> { + self.record + .add_attribute(key.to_string(), value.to_string()); + Ok(()) + } + } + + let mut extractor = KvExtractor { + record: &mut record, + }; + log_record.key_values().visit(&mut extractor).ok(); + logger.emit(record); Ok(()) }