Skip to content

Commit 4eec993

Browse files
committed
use the tracing event name as the otel event name
1 parent 293d206 commit 4eec993

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/layer.rs

+45
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,15 @@ where
10711071
sem_conv_config: self.sem_conv_config,
10721072
});
10731073

1074+
// If the event name is still empty, then there was no special handling of error fields.
1075+
// It should be safe to set the event name to the name provided by tracing.
1076+
// 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.
1077+
// Ideally, the name should be set above when the event is constructed.
1078+
// see: https://github.com/tokio-rs/tracing-opentelemetry/pull/28
1079+
if otel_event.name.is_empty() {
1080+
otel_event.name = std::borrow::Cow::Borrowed(event.metadata().name());
1081+
}
1082+
10741083
let mut extensions = span.extensions_mut();
10751084
let otel_data = extensions.get_mut::<OtelData>();
10761085

@@ -1489,6 +1498,42 @@ mod tests {
14891498
);
14901499
}
14911500

1501+
#[test]
1502+
fn records_event_name() {
1503+
let tracer = TestTracer(Arc::new(Mutex::new(None)));
1504+
let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone()));
1505+
1506+
tracing::subscriber::with_default(subscriber, || {
1507+
tracing::debug_span!("test span").in_scope(|| {
1508+
tracing::event!(tracing::Level::INFO, "event name 1"); // this is equivalent to 'message = "event name 1"'
1509+
tracing::event!(name: "event name 2", tracing::Level::INFO, field1 = "field1");
1510+
tracing::event!(name: "event name 3", tracing::Level::INFO, error = "field2");
1511+
tracing::event!(name: "event name 4", tracing::Level::INFO, message = "field3");
1512+
tracing::event!(name: "event name 5", tracing::Level::INFO, name = "field4");
1513+
});
1514+
});
1515+
1516+
let events = tracer
1517+
.0
1518+
.lock()
1519+
.unwrap()
1520+
.as_ref()
1521+
.unwrap()
1522+
.builder
1523+
.events
1524+
.as_ref()
1525+
.unwrap()
1526+
.clone();
1527+
1528+
let mut iter = events.iter();
1529+
1530+
assert_eq!(iter.next().unwrap().name, "event name 1");
1531+
assert_eq!(iter.next().unwrap().name, "event name 2");
1532+
assert_eq!(iter.next().unwrap().name, "exception"); // error attribute is handled specially
1533+
assert_eq!(iter.next().unwrap().name, "field3"); // message attribute is handled specially
1534+
assert_eq!(iter.next().unwrap().name, "event name 5"); // name attribute should not conflict with event name.
1535+
}
1536+
14921537
#[test]
14931538
fn records_no_error_fields() {
14941539
let tracer = TestTracer(Arc::new(Mutex::new(None)));

0 commit comments

Comments
 (0)