Skip to content

Commit 2db3750

Browse files
authored
opentelemetry: forward event source locations (#1911)
This branch adds the source code file, module path, and line number to OpenTelemetry events as the OpenTelemetry `code.filepath`, `code.namespace`, and `code.lineno` fields, respectively, if they are set in the `tracing` event's metadata. Fixes #1910
1 parent 0d4acb2 commit 2db3750

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

tracing-opentelemetry/src/subscriber.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::{OtelData, PreSampledTracer};
22
use opentelemetry::{
33
trace::{self as otel, noop, TraceContextExt},
4-
Context as OtelContext, Key, KeyValue,
4+
Context as OtelContext, Key, KeyValue, Value,
55
};
6+
#[cfg(not(feature = "tracing-log"))]
7+
use std::borrow::Cow;
68
use std::fmt;
79
use std::marker;
810
use std::time::{Instant, SystemTime};
@@ -27,6 +29,7 @@ const SPAN_STATUS_MESSAGE_FIELD: &str = "otel.status_message";
2729
/// [tracing]: https://github.com/tokio-rs/tracing
2830
pub struct OpenTelemetrySubscriber<C, T> {
2931
tracer: T,
32+
event_location: bool,
3033
tracked_inactivity: bool,
3134
get_context: WithContext,
3235
_registry: marker::PhantomData<C>,
@@ -289,6 +292,7 @@ where
289292
pub fn new(tracer: T) -> Self {
290293
OpenTelemetrySubscriber {
291294
tracer,
295+
event_location: true,
292296
tracked_inactivity: true,
293297
get_context: WithContext(Self::get_context),
294298
_registry: marker::PhantomData,
@@ -327,12 +331,24 @@ where
327331
{
328332
OpenTelemetrySubscriber {
329333
tracer,
334+
event_location: self.event_location,
330335
tracked_inactivity: self.tracked_inactivity,
331336
get_context: WithContext(OpenTelemetrySubscriber::<C, Tracer>::get_context),
332337
_registry: self._registry,
333338
}
334339
}
335340

341+
/// Sets whether or not event span's metadata should include detailed location
342+
/// information, such as the file, module and line number.
343+
///
344+
/// By default, event locations are enabled.
345+
pub fn with_event_location(self, event_location: bool) -> Self {
346+
Self {
347+
event_location,
348+
..self
349+
}
350+
}
351+
336352
/// Sets whether or not spans metadata should include the _busy time_
337353
/// (total time for which it was entered), and _idle time_ (total time
338354
/// the span existed but was not entered).
@@ -555,6 +571,33 @@ where
555571
builder.status_code = Some(otel::StatusCode::Error);
556572
}
557573

574+
if self.event_location {
575+
let builder_attrs = builder.attributes.get_or_insert(Vec::new());
576+
577+
#[cfg(not(feature = "tracing-log"))]
578+
let normalized_meta = None;
579+
let (file, module) = match &normalized_meta {
580+
Some(meta) => (
581+
meta.file().map(|s| Value::from(s.to_owned())),
582+
meta.module_path().map(|s| Value::from(s.to_owned())),
583+
),
584+
None => (
585+
event.metadata().file().map(Value::from),
586+
event.metadata().module_path().map(Value::from),
587+
),
588+
};
589+
590+
if let Some(file) = file {
591+
builder_attrs.push(KeyValue::new("code.filepath", file));
592+
}
593+
if let Some(module) = module {
594+
builder_attrs.push(KeyValue::new("code.namespace", module));
595+
}
596+
if let Some(line) = meta.line() {
597+
builder_attrs.push(KeyValue::new("code.lineno", line as i64));
598+
}
599+
}
600+
558601
if let Some(ref mut events) = builder.events {
559602
events.push(otel_event);
560603
} else {

0 commit comments

Comments
 (0)