From dcd69277151b4da69d0788d70b359d4a06e82ec0 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 20 Nov 2024 16:01:44 -0800 Subject: [PATCH 1/2] make logrecord fields private --- opentelemetry-proto/src/transform/logs.rs | 21 ++++---- opentelemetry-sdk/src/logs/record.rs | 65 ++++++++++++++++++++--- opentelemetry-stdout/src/logs/exporter.rs | 16 +++--- 3 files changed, 77 insertions(+), 25 deletions(-) diff --git a/opentelemetry-proto/src/transform/logs.rs b/opentelemetry-proto/src/transform/logs.rs index a85b2ed7a7..fb4895334b 100644 --- a/opentelemetry-proto/src/transform/logs.rs +++ b/opentelemetry-proto/src/transform/logs.rs @@ -58,8 +58,8 @@ pub mod tonic { impl From<&opentelemetry_sdk::logs::LogRecord> for LogRecord { fn from(log_record: &opentelemetry_sdk::logs::LogRecord) -> Self { - let trace_context = log_record.trace_context.as_ref(); - let severity_number = match log_record.severity_number { + let trace_context = log_record.trace_context(); + let severity_number = match log_record.severity_number() { Some(Severity::Trace) => SeverityNumber::Trace, Some(Severity::Trace2) => SeverityNumber::Trace2, Some(Severity::Trace3) => SeverityNumber::Trace3, @@ -88,8 +88,8 @@ pub mod tonic { }; LogRecord { - time_unix_nano: log_record.timestamp.map(to_nanos).unwrap_or_default(), - observed_time_unix_nano: to_nanos(log_record.observed_timestamp.unwrap()), + time_unix_nano: log_record.timestamp().map(to_nanos).unwrap_or_default(), + observed_time_unix_nano: to_nanos(log_record.observed_timestamp().unwrap()), attributes: { let attributes: Vec = log_record .attributes_iter() @@ -119,8 +119,11 @@ pub mod tonic { attributes }, severity_number: severity_number.into(), - severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(), - body: log_record.body.clone().map(Into::into), + severity_text: log_record + .severity_text() + .map(Into::into) + .unwrap_or_default(), + body: log_record.body().cloned().map(Into::into), dropped_attributes_count: 0, flags: trace_context .map(|ctx| { @@ -170,7 +173,7 @@ pub mod tonic { .schema_url() .map(ToOwned::to_owned) .unwrap_or_default(), - scope: Some((instrumentation, log_record.target.clone()).into()), + scope: Some((instrumentation, log_record.target().cloned()).into()), log_records: vec![log_record.into()], }], } @@ -193,8 +196,8 @@ pub mod tonic { >, (log_record, instrumentation)| { let key = log_record - .target - .clone() + .target() + .cloned() .unwrap_or_else(|| Cow::Owned(instrumentation.name().to_owned())); scope_map .entry(key) diff --git a/opentelemetry-sdk/src/logs/record.rs b/opentelemetry-sdk/src/logs/record.rs index 24f3f6a165..3e4f5c8c18 100644 --- a/opentelemetry-sdk/src/logs/record.rs +++ b/opentelemetry-sdk/src/logs/record.rs @@ -24,28 +24,28 @@ pub(crate) type LogRecordAttributes = /// is provided to `LogExporter`s as input. pub struct LogRecord { /// Event name. Optional as not all the logging API support it. - pub event_name: Option<&'static str>, + pub(crate) event_name: Option<&'static str>, /// Target of the log record - pub target: Option>, + pub(crate) target: Option>, /// Record timestamp - pub timestamp: Option, + pub(crate) timestamp: Option, /// Timestamp for when the record was observed by OpenTelemetry - pub observed_timestamp: Option, + pub(crate) observed_timestamp: Option, /// Trace context for logs associated with spans - pub trace_context: Option, + pub(crate) trace_context: Option, /// The original severity string from the source - pub severity_text: Option<&'static str>, + pub(crate) severity_text: Option<&'static str>, /// The corresponding severity value, normalized - pub severity_number: Option, + pub(crate) severity_number: Option, /// Record body - pub body: Option, + pub(crate) body: Option, /// Additional attributes associated with this record pub(crate) attributes: LogRecordAttributes, @@ -118,7 +118,56 @@ impl opentelemetry::logs::LogRecord for LogRecord { } impl LogRecord { + /// Returns the event name + #[inline] + pub fn event_name(&self) -> Option<&'static str> { + self.event_name + } + + /// Returns the target + #[inline] + pub fn target(&self) -> Option<&Cow<'static, str>> { + self.target.as_ref() + } + + /// Returns the timestamp + #[inline] + pub fn timestamp(&self) -> Option { + self.timestamp + } + + /// Returns the observed timestamp + #[inline] + pub fn observed_timestamp(&self) -> Option { + self.observed_timestamp + } + + /// Returns the trace context + #[inline] + pub fn trace_context(&self) -> Option<&TraceContext> { + self.trace_context.as_ref() + } + + /// Returns the severity text + #[inline] + pub fn severity_text(&self) -> Option<&'static str> { + self.severity_text + } + + /// Returns the severity number + #[inline] + pub fn severity_number(&self) -> Option { + self.severity_number + } + + /// Returns the body + #[inline] + pub fn body(&self) -> Option<&AnyValue> { + self.body.as_ref() + } + /// Provides an iterator over the attributes. + #[inline] pub fn attributes_iter(&self) -> impl Iterator { self.attributes.iter().filter_map(|opt| opt.as_ref()) } diff --git a/opentelemetry-stdout/src/logs/exporter.rs b/opentelemetry-stdout/src/logs/exporter.rs index c5edb880bf..4a1070e832 100644 --- a/opentelemetry-stdout/src/logs/exporter.rs +++ b/opentelemetry-stdout/src/logs/exporter.rs @@ -69,34 +69,34 @@ fn print_logs(batch: LogBatch<'_>) { for (i, log) in batch.iter().enumerate() { println!("Log #{}", i); let (record, _library) = log; - if let Some(event_name) = record.event_name { + if let Some(event_name) = record.event_name() { println!("\t EventName: {:?}", event_name); } - if let Some(target) = &record.target { + if let Some(target) = record.target() { println!("\t Target (Scope): {:?}", target); } - if let Some(trace_context) = &record.trace_context { + if let Some(trace_context) = record.trace_context() { println!("\t TraceId: {:?}", trace_context.trace_id); println!("\t SpanId: {:?}", trace_context.span_id); } - if let Some(timestamp) = record.timestamp { + if let Some(timestamp) = record.timestamp() { let datetime: DateTime = timestamp.into(); println!("\t Timestamp: {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f")); } - if let Some(timestamp) = record.observed_timestamp { + if let Some(timestamp) = record.observed_timestamp() { let datetime: DateTime = timestamp.into(); println!( "\t Observed Timestamp: {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") ); } - if let Some(severity) = record.severity_text { + if let Some(severity) = record.severity_text() { println!("\t SeverityText: {:?}", severity); } - if let Some(severity) = record.severity_number { + if let Some(severity) = record.severity_number() { println!("\t SeverityNumber: {:?}", severity); } - if let Some(body) = &record.body { + if let Some(body) = record.body() { println!("\t Body: {:?}", body); } From f85aa8978405d1a33fed21019f281abfb83fd787 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 20 Nov 2024 16:17:02 -0800 Subject: [PATCH 2/2] more changes --- opentelemetry-appender-log/src/lib.rs | 4 +-- opentelemetry-appender-tracing/src/layer.rs | 38 ++++++++------------- opentelemetry-proto/src/transform/logs.rs | 2 +- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/opentelemetry-appender-log/src/lib.rs b/opentelemetry-appender-log/src/lib.rs index 69f4eabeed..2d0e7709c5 100644 --- a/opentelemetry-appender-log/src/lib.rs +++ b/opentelemetry-appender-log/src/lib.rs @@ -830,11 +830,11 @@ mod tests { assert_eq!(logs.len(), 5); for log in logs { - let body: String = match log.record.body.as_ref().unwrap() { + let body: String = match log.record.body().unwrap() { super::AnyValue::String(s) => s.to_string(), _ => panic!("AnyValue::String expected"), }; - assert_eq!(body, log.record.severity_text.unwrap()); + assert_eq!(body, log.record.severity_text().unwrap()); } } diff --git a/opentelemetry-appender-tracing/src/layer.rs b/opentelemetry-appender-tracing/src/layer.rs index 62b840108b..3b15c158a9 100644 --- a/opentelemetry-appender-tracing/src/layer.rs +++ b/opentelemetry-appender-tracing/src/layer.rs @@ -335,10 +335,10 @@ mod tests { // Validate common fields assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing"); - assert_eq!(log.record.severity_number, Some(Severity::Error)); + assert_eq!(log.record.severity_number(), Some(Severity::Error)); // Validate trace context is none. - assert!(log.record.trace_context.is_none()); + assert!(log.record.trace_context().is_none()); // Validate attributes #[cfg(not(feature = "experimental_metadata_attributes"))] @@ -429,25 +429,20 @@ mod tests { // validate common fields. assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing"); - assert_eq!(log.record.severity_number, Some(Severity::Error)); + assert_eq!(log.record.severity_number(), Some(Severity::Error)); // validate trace context. - assert!(log.record.trace_context.is_some()); + assert!(log.record.trace_context().is_some()); assert_eq!( - log.record.trace_context.as_ref().unwrap().trace_id, + log.record.trace_context().unwrap().trace_id, trace_id_expected ); assert_eq!( - log.record.trace_context.as_ref().unwrap().span_id, + log.record.trace_context().unwrap().span_id, span_id_expected ); assert_eq!( - log.record - .trace_context - .as_ref() - .unwrap() - .trace_flags - .unwrap(), + log.record.trace_context().unwrap().trace_flags.unwrap(), TraceFlags::SAMPLED ); @@ -527,10 +522,10 @@ mod tests { // Validate common fields assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing"); - assert_eq!(log.record.severity_number, Some(Severity::Error)); + assert_eq!(log.record.severity_number(), Some(Severity::Error)); // Validate trace context is none. - assert!(log.record.trace_context.is_none()); + assert!(log.record.trace_context().is_none()); // Attributes can be polluted when we don't use this feature. #[cfg(feature = "experimental_metadata_attributes")] @@ -606,25 +601,20 @@ mod tests { // validate common fields. assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing"); - assert_eq!(log.record.severity_number, Some(Severity::Error)); + assert_eq!(log.record.severity_number(), Some(Severity::Error)); // validate trace context. - assert!(log.record.trace_context.is_some()); + assert!(log.record.trace_context().is_some()); assert_eq!( - log.record.trace_context.as_ref().unwrap().trace_id, + log.record.trace_context().unwrap().trace_id, trace_id_expected ); assert_eq!( - log.record.trace_context.as_ref().unwrap().span_id, + log.record.trace_context().unwrap().span_id, span_id_expected ); assert_eq!( - log.record - .trace_context - .as_ref() - .unwrap() - .trace_flags - .unwrap(), + log.record.trace_context().unwrap().trace_flags.unwrap(), TraceFlags::SAMPLED ); diff --git a/opentelemetry-proto/src/transform/logs.rs b/opentelemetry-proto/src/transform/logs.rs index fb4895334b..81f0f70798 100644 --- a/opentelemetry-proto/src/transform/logs.rs +++ b/opentelemetry-proto/src/transform/logs.rs @@ -102,7 +102,7 @@ pub mod tonic { .collect(); #[cfg(feature = "populate-logs-event-name")] { - if let Some(event_name) = &log_record.event_name { + if let Some(event_name) = &log_record.event_name() { let mut attributes_with_name = attributes; attributes_with_name.push(KeyValue { key: "event.name".into(),