Skip to content

Commit 92c5425

Browse files
DevinCarrhawkw
authored andcommitted
opentelemetry: enforce event_location for span tags (#2124)
The `with_event_location(false)` method will now properly omit `code.*` tags from spans. ## Motivation Recently, I attempted to remove the `code.*` tags from opentelemetry tracing spans utilizing the [`with_event_location`] of OpenTelemetrySubscriber. This did not work as expected because the [`on_new_span`] doesn't account for the `event_location` boolean similar to how [`on_event`] does. ## Solution The change presented will expand the use of the `location` field to check before adding the `code.*` KeyValue attributes in `on_new_span`. In addition, `with_event_location` was renamed to `with_location`, as it now controls both span *and* event locations, and the `with_event_location` method has been deprecated. ## Testing Additional unit tests are included in [tracing-opentelemetry/src/subscriber.rs] to cover both boolean cases of `with_location`. [`with_event_location`]: https://github.com/tokio-rs/tracing/blob/master/tracing-opentelemetry/src/subscriber.rs#L343 [`on_new_span`]: https://github.com/tokio-rs/tracing/blob/master/tracing-opentelemetry/src/subscriber.rs#L448-L460 [`on_event`]: https://github.com/tokio-rs/tracing/blob/master/tracing-opentelemetry/src/subscriber.rs#L582 [tracing-opentelemetry/src/subscriber.rs]: https://github.com/tokio-rs/tracing/pull/2124/files#diff-69011e8b23dffcbe19b9b72e5ac54330a7871f424a90700ed7f5c5686daf431bR911-R975)
1 parent 1afc9d5 commit 92c5425

File tree

1 file changed

+88
-14
lines changed

1 file changed

+88
-14
lines changed

tracing-opentelemetry/src/layer.rs

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const SPAN_STATUS_MESSAGE_FIELD: &str = "otel.status_message";
2828
/// [tracing]: https://github.com/tokio-rs/tracing
2929
pub struct OpenTelemetryLayer<S, T> {
3030
tracer: T,
31-
event_location: bool,
31+
location: bool,
3232
tracked_inactivity: bool,
3333
get_context: WithContext,
3434
_registry: marker::PhantomData<S>,
@@ -312,7 +312,7 @@ where
312312
pub fn new(tracer: T) -> Self {
313313
OpenTelemetryLayer {
314314
tracer,
315-
event_location: true,
315+
location: true,
316316
tracked_inactivity: true,
317317
get_context: WithContext(Self::get_context),
318318
_registry: marker::PhantomData,
@@ -351,20 +351,32 @@ where
351351
{
352352
OpenTelemetryLayer {
353353
tracer,
354-
event_location: self.event_location,
354+
location: self.location,
355355
tracked_inactivity: self.tracked_inactivity,
356356
get_context: WithContext(OpenTelemetryLayer::<S, Tracer>::get_context),
357357
_registry: self._registry,
358358
}
359359
}
360360

361+
/// Sets whether or not span and event metadata should include detailed
362+
/// location information, such as the file, module and line number.
363+
///
364+
/// By default, locations are enabled.
365+
pub fn with_location(self, location: bool) -> Self {
366+
Self { location, ..self }
367+
}
368+
361369
/// Sets whether or not event span's metadata should include detailed location
362370
/// information, such as the file, module and line number.
363371
///
364372
/// By default, event locations are enabled.
373+
#[deprecated(
374+
since = "0.17.3",
375+
note = "renamed to `OpenTelemetrySubscriber::with_location`"
376+
)]
365377
pub fn with_event_location(self, event_location: bool) -> Self {
366378
Self {
367-
event_location,
379+
location: event_location,
368380
..self
369381
}
370382
}
@@ -467,18 +479,20 @@ where
467479
.attributes
468480
.get_or_insert(Vec::with_capacity(attrs.fields().len() + 3));
469481

470-
let meta = attrs.metadata();
482+
if self.location {
483+
let meta = attrs.metadata();
471484

472-
if let Some(filename) = meta.file() {
473-
builder_attrs.push(KeyValue::new("code.filepath", filename));
474-
}
485+
if let Some(filename) = meta.file() {
486+
builder_attrs.push(KeyValue::new("code.filepath", filename));
487+
}
475488

476-
if let Some(module) = meta.module_path() {
477-
builder_attrs.push(KeyValue::new("code.namespace", module));
478-
}
489+
if let Some(module) = meta.module_path() {
490+
builder_attrs.push(KeyValue::new("code.namespace", module));
491+
}
479492

480-
if let Some(line) = meta.line() {
481-
builder_attrs.push(KeyValue::new("code.lineno", line as i64));
493+
if let Some(line) = meta.line() {
494+
builder_attrs.push(KeyValue::new("code.lineno", line as i64));
495+
}
482496
}
483497

484498
attrs.record(&mut SpanAttributeVisitor(&mut builder));
@@ -601,7 +615,7 @@ where
601615
builder.status_code = Some(otel::StatusCode::Error);
602616
}
603617

604-
if self.event_location {
618+
if self.location {
605619
#[cfg(not(feature = "tracing-log"))]
606620
let normalized_meta: Option<tracing_core::Metadata<'_>> = None;
607621
let (file, module) = match &normalized_meta {
@@ -999,4 +1013,64 @@ mod tests {
9991013
)
10001014
);
10011015
}
1016+
1017+
#[test]
1018+
fn includes_span_location() {
1019+
let tracer = TestTracer(Arc::new(Mutex::new(None)));
1020+
let subscriber = tracing_subscriber::registry()
1021+
.with(layer().with_tracer(tracer.clone()).with_location(true));
1022+
1023+
tracing::subscriber::with_default(subscriber, || {
1024+
tracing::debug_span!("request");
1025+
});
1026+
1027+
let attributes = tracer
1028+
.0
1029+
.lock()
1030+
.unwrap()
1031+
.as_ref()
1032+
.unwrap()
1033+
.builder
1034+
.attributes
1035+
.as_ref()
1036+
.unwrap()
1037+
.clone();
1038+
let keys = attributes
1039+
.iter()
1040+
.map(|attr| attr.key.as_str())
1041+
.collect::<Vec<&str>>();
1042+
assert!(keys.contains(&"code.filepath"));
1043+
assert!(keys.contains(&"code.namespace"));
1044+
assert!(keys.contains(&"code.lineno"));
1045+
}
1046+
1047+
#[test]
1048+
fn excludes_span_location() {
1049+
let tracer = TestTracer(Arc::new(Mutex::new(None)));
1050+
let subscriber = tracing_subscriber::registry()
1051+
.with(layer().with_tracer(tracer.clone()).with_location(false));
1052+
1053+
tracing::subscriber::with_default(subscriber, || {
1054+
tracing::debug_span!("request");
1055+
});
1056+
1057+
let attributes = tracer
1058+
.0
1059+
.lock()
1060+
.unwrap()
1061+
.as_ref()
1062+
.unwrap()
1063+
.builder
1064+
.attributes
1065+
.as_ref()
1066+
.unwrap()
1067+
.clone();
1068+
let keys = attributes
1069+
.iter()
1070+
.map(|attr| attr.key.as_str())
1071+
.collect::<Vec<&str>>();
1072+
assert!(!keys.contains(&"code.filepath"));
1073+
assert!(!keys.contains(&"code.namespace"));
1074+
assert!(!keys.contains(&"code.lineno"));
1075+
}
10021076
}

0 commit comments

Comments
 (0)