Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert tracing event name into LogRecord::event_name instead of attributes #1928

Merged
merged 19 commits into from
Jul 13, 2024
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use opentelemetry::{
Key,
};
use std::borrow::Cow;
use tracing_core::{Level, Metadata};
use tracing_core::Level;
#[cfg(feature = "experimental_metadata_attributes")]
use tracing_core::Metadata;
#[cfg(feature = "experimental_metadata_attributes")]
use tracing_log::NormalizeEvent;
use tracing_subscriber::Layer;
Expand Down Expand Up @@ -39,13 +41,6 @@ impl<'a, LR: LogRecord> EventVisitor<'a, LR> {
fn new(log_record: &'a mut LR) -> Self {
EventVisitor { log_record }
}
fn visit_metadata(&mut self, meta: &Metadata) {
self.log_record
.add_attribute(Key::new("name"), AnyValue::from(meta.name()));

#[cfg(feature = "experimental_metadata_attributes")]
self.visit_experimental_metadata(meta);
}

#[cfg(feature = "experimental_metadata_attributes")]
fn visit_experimental_metadata(&mut self, meta: &Metadata) {
Expand Down Expand Up @@ -170,15 +165,17 @@ where

//let mut log_record: LogRecord = LogRecord::default();
let mut log_record = self.logger.create_log_record();
log_record.set_target(meta.target().to_string());
log_record.set_event_name(meta.name());
log_record.set_severity_number(severity_of_level(meta.level()));
log_record.set_severity_text(meta.level().to_string().into());
log_record.set_target(meta.target().to_string());

let mut visitor = EventVisitor::new(&mut log_record);
visitor.visit_metadata(meta);
#[cfg(feature = "experimental_metadata_attributes")]
visitor.visit_experimental_metadata(meta);
// Visit fields.
event.record(&mut visitor);

//emit record
self.logger.emit(log_record);
}

Expand Down Expand Up @@ -261,10 +258,9 @@ mod tests {
.clone()
.expect("Attributes are expected");
#[cfg(not(feature = "experimental_metadata_attributes"))]
assert_eq!(attributes.len(), 4);
assert_eq!(attributes.len(), 3);
#[cfg(feature = "experimental_metadata_attributes")]
assert_eq!(attributes.len(), 9);
assert!(attributes.contains(&(Key::new("name"), "my-event-name".into())));
assert_eq!(attributes.len(), 8);
assert!(attributes.contains(&(Key::new("event_id"), 20.into())));
assert!(attributes.contains(&(Key::new("user_name"), "otel".into())));
assert!(attributes.contains(&(Key::new("user_email"), "otel@opentelemetry.io".into())));
Expand Down Expand Up @@ -358,10 +354,9 @@ mod tests {
.clone()
.expect("Attributes are expected");
#[cfg(not(feature = "experimental_metadata_attributes"))]
assert_eq!(attributes.len(), 4);
assert_eq!(attributes.len(), 3);
#[cfg(feature = "experimental_metadata_attributes")]
assert_eq!(attributes.len(), 9);
assert!(attributes.contains(&(Key::new("name"), "my-event-name".into())));
assert_eq!(attributes.len(), 8);
assert!(attributes.contains(&(Key::new("event_id"), 20.into())));
assert!(attributes.contains(&(Key::new("user_name"), "otel".into())));
assert!(attributes.contains(&(Key::new("user_email"), "otel@opentelemetry.io".into())));
Expand Down Expand Up @@ -419,6 +414,7 @@ mod tests {
assert!(log.record.trace_context.is_none());

// Validate attributes
#[cfg(feature = "experimental_metadata_attributes")]
let attributes: Vec<(Key, AnyValue)> = log
.record
.attributes
Expand All @@ -427,9 +423,7 @@ mod tests {

// Attributes can be polluted when we don't use this feature.
#[cfg(feature = "experimental_metadata_attributes")]
assert_eq!(attributes.len(), 6);

assert!(attributes.contains(&(Key::new("name"), "log event".into())));
assert_eq!(attributes.len(), 5);

#[cfg(feature = "experimental_metadata_attributes")]
{
Expand Down Expand Up @@ -516,6 +510,7 @@ mod tests {
);

// validate attributes.
#[cfg(feature = "experimental_metadata_attributes")]
let attributes: Vec<(Key, AnyValue)> = log
.record
.attributes
Expand All @@ -524,9 +519,7 @@ mod tests {

// Attributes can be polluted when we don't use this feature.
#[cfg(feature = "experimental_metadata_attributes")]
assert_eq!(attributes.len(), 6);

assert!(attributes.contains(&(Key::new("name"), "log event".into())));
assert_eq!(attributes.len(), 5);

#[cfg(feature = "experimental_metadata_attributes")]
{
Expand Down
Loading