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

use the tracing event name as the otel event name #188

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all 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
45 changes: 45 additions & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,15 @@ where
sem_conv_config: self.sem_conv_config,
});

// If the event name is still empty, then there was no special handling of error fields.
// It should be safe to set the event name to the name provided by tracing.
// This is a hack but there are existing hacks that depend on the name being empty, so to avoid breaking those the event name is set here.
// Ideally, the name should be set above when the event is constructed.
// see: https://github.com/tokio-rs/tracing-opentelemetry/pull/28
if otel_event.name.is_empty() {
otel_event.name = std::borrow::Cow::Borrowed(event.metadata().name());
}

let mut extensions = span.extensions_mut();
let otel_data = extensions.get_mut::<OtelData>();

Expand Down Expand Up @@ -1489,6 +1498,42 @@ mod tests {
);
}

#[test]
fn records_event_name() {
let tracer = TestTracer(Arc::new(Mutex::new(None)));
let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone()));

tracing::subscriber::with_default(subscriber, || {
tracing::debug_span!("test span").in_scope(|| {
tracing::event!(tracing::Level::INFO, "event name 1"); // this is equivalent to 'message = "event name 1"'
tracing::event!(name: "event name 2", tracing::Level::INFO, field1 = "field1");
tracing::event!(name: "event name 3", tracing::Level::INFO, error = "field2");
tracing::event!(name: "event name 4", tracing::Level::INFO, message = "field3");
tracing::event!(name: "event name 5", tracing::Level::INFO, name = "field4");
});
});

let events = tracer
.0
.lock()
.unwrap()
.as_ref()
.unwrap()
.builder
.events
.as_ref()
.unwrap()
.clone();

let mut iter = events.iter();

assert_eq!(iter.next().unwrap().name, "event name 1");
assert_eq!(iter.next().unwrap().name, "event name 2");
assert_eq!(iter.next().unwrap().name, "exception"); // error attribute is handled specially
assert_eq!(iter.next().unwrap().name, "field3"); // message attribute is handled specially
assert_eq!(iter.next().unwrap().name, "event name 5"); // name attribute should not conflict with event name.
}

#[test]
fn records_no_error_fields() {
let tracer = TestTracer(Arc::new(Mutex::new(None)));
Expand Down
Loading