From 684f118d6f0e57472015e90ff581508bba8a73a5 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Fri, 9 Aug 2024 14:23:28 +0800 Subject: [PATCH 1/2] feat: renders kvs in OpentelemetryLog --- src/append/opentelemetry.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index 03c2c60..dbfb933 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -18,6 +18,7 @@ use std::time::SystemTime; use log::Record; use opentelemetry::logs::AnyValue; +use opentelemetry::logs::LogRecord; use opentelemetry::logs::Logger; use opentelemetry::logs::LoggerProvider as ILoggerProvider; use opentelemetry::logs::Severity; @@ -111,8 +112,39 @@ 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, 'kvs> { + record: &'a mut LogRecord, + } + + impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvExtractor<'a, 'kvs> { + fn visit_pair( + &mut self, + key: log::kv::Key<'kvs>, + value: log::kv::Value<'kvs>, + ) -> Result<(), log::kv::Error> { + self.record.add_attribute(key, value); + Ok(()) + } + } + + let mut extractor = KvExtractor { + record: &mut record, + }; + log_record.key_values().visit(&mut extractor).ok(); + logger.emit(record); Ok(()) } From 9fe85e5a37027a7219783cfa95ab1d5853ff3e24 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Fri, 9 Aug 2024 14:33:23 +0800 Subject: [PATCH 2/2] fix --- src/append/opentelemetry.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index dbfb933..a4cba41 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -18,13 +18,14 @@ use std::time::SystemTime; use log::Record; use opentelemetry::logs::AnyValue; -use opentelemetry::logs::LogRecord; +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; @@ -125,17 +126,18 @@ impl Append for OpentelemetryLog { record.add_attribute("line", line); } - struct KvExtractor<'a, 'kvs> { + struct KvExtractor<'a> { record: &'a mut LogRecord, } - impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvExtractor<'a, 'kvs> { + 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, value); + self.record + .add_attribute(key.to_string(), value.to_string()); Ok(()) } }