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
Show file tree
Hide file tree
Changes from 15 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
3 changes: 3 additions & 0 deletions opentelemetry-appender-tracing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) Utilize the `LogRecord::set_target()` method to pass the tracing target to the SDK.
Exporters might use the target to override the instrumentation scope, which previously contained "opentelemetry-appender-tracing".

- **Breaking** [1928](https://github.com/open-telemetry/opentelemetry-rust/pull/1928) Insert tracing event name into LogRecord::event_name instead of attributes.
- Custom exporters must now serialize this field directly from LogRecord::event_name instead of iterating over the attributes.

## v0.4.0

- Removed unwanted dependency on opentelemetry-sdk.
Expand Down
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
21 changes: 16 additions & 5 deletions opentelemetry-proto/src/transform/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,22 @@
severity_number: severity_number.into(),
severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(),
body: log_record.body.map(Into::into),
attributes: log_record
.attributes
.map(Attributes::from_iter)
.unwrap_or_default()
.0,
attributes: {
let mut attributes = log_record
.attributes
.map(Attributes::from_iter)
.unwrap_or_default()
.0;
if let Some(event_name) = log_record.event_name.as_ref() {
attributes.push(KeyValue {
key: "name".into(),
value: Some(AnyValue {
value: Some(Value::StringValue(event_name.to_string())),
}),
})

Check warning on line 107 in opentelemetry-proto/src/transform/logs.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/logs.rs#L102-L107

Added lines #L102 - L107 were not covered by tests
}
attributes
},
dropped_attributes_count: 0,
flags: trace_context
.map(|ctx| {
Expand Down
Loading