Skip to content

Commit

Permalink
use the tracing event name as the otel event name
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlk authored and djc committed Jan 29, 2025
1 parent 0ed001a commit 1e4ad6b
Showing 1 changed file with 45 additions and 0 deletions.
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

0 comments on commit 1e4ad6b

Please sign in to comment.