Skip to content

Commit 95f9ab9

Browse files
authored
Add level metadata to spans (#182)
See #180 for a description. This PR implements the `OpenTelemetryLayer::with_level` setting, to disable (enabled by default) attaching the level tag to exported spans.
1 parent ab629c3 commit 95f9ab9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/layer.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct OpenTelemetryLayer<S, T> {
3939
location: bool,
4040
tracked_inactivity: bool,
4141
with_threads: bool,
42+
with_level: bool,
4243
sem_conv_config: SemConvConfig,
4344
get_context: WithContext,
4445
_registry: marker::PhantomData<S>,
@@ -562,6 +563,7 @@ where
562563
location: true,
563564
tracked_inactivity: true,
564565
with_threads: true,
566+
with_level: false,
565567
sem_conv_config: SemConvConfig {
566568
error_fields_to_exceptions: true,
567569
error_records_to_exceptions: true,
@@ -614,9 +616,11 @@ where
614616
location: self.location,
615617
tracked_inactivity: self.tracked_inactivity,
616618
with_threads: self.with_threads,
619+
with_level: self.with_level,
617620
sem_conv_config: self.sem_conv_config,
618621
get_context: WithContext(OpenTelemetryLayer::<S, Tracer>::get_context),
619622
_registry: self._registry,
623+
// cannot use ``..self` here due to different generics
620624
}
621625
}
622626

@@ -742,6 +746,19 @@ where
742746
}
743747
}
744748

749+
/// Sets whether or not span metadata should include the `tracing` verbosity level information as a `level` field.
750+
///
751+
/// The level is always added to events, and based on [`OpenTelemetryLayer::with_error_events_to_status`]
752+
/// error-level events will mark the span status as an error.
753+
///
754+
/// By default, level information is disabled.
755+
pub fn with_level(self, level: bool) -> Self {
756+
Self {
757+
with_level: level,
758+
..self
759+
}
760+
}
761+
745762
/// Retrieve the parent OpenTelemetry [`Context`] from the current tracing
746763
/// [`span`] through the [`Registry`]. This [`Context`] links spans to their
747764
/// parent for proper hierarchical visualization.
@@ -817,6 +834,9 @@ where
817834
if self.with_threads {
818835
extra_attrs += 2;
819836
}
837+
if self.with_level {
838+
extra_attrs += 1;
839+
}
820840
extra_attrs
821841
}
822842
}
@@ -895,6 +915,10 @@ where
895915
}
896916
}
897917

918+
if self.with_level {
919+
builder_attrs.push(KeyValue::new("level", attrs.metadata().level().as_str()));
920+
}
921+
898922
let mut updates = SpanBuilderUpdates::default();
899923
attrs.record(&mut SpanAttributeVisitor {
900924
span_builder_updates: &mut updates,
@@ -1607,6 +1631,42 @@ mod tests {
16071631
assert!(!keys.contains(&"thread.id"));
16081632
}
16091633

1634+
#[test]
1635+
fn includes_level() {
1636+
let tracer = TestTracer(Arc::new(Mutex::new(None)));
1637+
let subscriber = tracing_subscriber::registry()
1638+
.with(layer().with_tracer(tracer.clone()).with_level(true));
1639+
1640+
tracing::subscriber::with_default(subscriber, || {
1641+
tracing::debug_span!("request");
1642+
});
1643+
1644+
let attributes = tracer.with_data(|data| data.builder.attributes.as_ref().unwrap().clone());
1645+
let keys = attributes
1646+
.iter()
1647+
.map(|kv| kv.key.as_str())
1648+
.collect::<Vec<&str>>();
1649+
assert!(keys.contains(&"level"));
1650+
}
1651+
1652+
#[test]
1653+
fn excludes_level() {
1654+
let tracer = TestTracer(Arc::new(Mutex::new(None)));
1655+
let subscriber = tracing_subscriber::registry()
1656+
.with(layer().with_tracer(tracer.clone()).with_level(false));
1657+
1658+
tracing::subscriber::with_default(subscriber, || {
1659+
tracing::debug_span!("request");
1660+
});
1661+
1662+
let attributes = tracer.with_data(|data| data.builder.attributes.as_ref().unwrap().clone());
1663+
let keys = attributes
1664+
.iter()
1665+
.map(|kv| kv.key.as_str())
1666+
.collect::<Vec<&str>>();
1667+
assert!(!keys.contains(&"level"));
1668+
}
1669+
16101670
#[test]
16111671
fn propagates_error_fields_from_event_to_span() {
16121672
let tracer = TestTracer(Arc::new(Mutex::new(None)));

0 commit comments

Comments
 (0)