Skip to content

Commit

Permalink
opentelemetry: forward event metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 7, 2022
1 parent 0d4acb2 commit 7304921
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tracing-opentelemetry/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use opentelemetry::{
trace::{self as otel, noop, TraceContextExt},
Context as OtelContext, Key, KeyValue,
};
#[cfg(not(feature = "tracing-log"))]
use std::borrow::Cow;
use std::fmt;
use std::marker;
use std::time::{Instant, SystemTime};
Expand All @@ -27,6 +29,7 @@ const SPAN_STATUS_MESSAGE_FIELD: &str = "otel.status_message";
/// [tracing]: https://github.com/tokio-rs/tracing
pub struct OpenTelemetrySubscriber<C, T> {
tracer: T,
event_location: bool,
tracked_inactivity: bool,
get_context: WithContext,
_registry: marker::PhantomData<C>,
Expand Down Expand Up @@ -289,6 +292,7 @@ where
pub fn new(tracer: T) -> Self {
OpenTelemetrySubscriber {
tracer,
event_location: true,
tracked_inactivity: true,
get_context: WithContext(Self::get_context),
_registry: marker::PhantomData,
Expand Down Expand Up @@ -327,12 +331,22 @@ where
{
OpenTelemetrySubscriber {
tracer,
event_location: self.event_location,
tracked_inactivity: self.tracked_inactivity,
get_context: WithContext(OpenTelemetrySubscriber::<C, Tracer>::get_context),
_registry: self._registry,
}
}

/// Sets whether or not event span's metadata should include detailed location
/// information, such as the file, module and line number.
pub fn with_event_location(self, event_location: bool) -> Self {
Self {
event_location,
..self
}
}

/// Sets whether or not spans metadata should include the _busy time_
/// (total time for which it was entered), and _idle time_ (total time
/// the span existed but was not entered).
Expand Down Expand Up @@ -555,6 +569,25 @@ where
builder.status_code = Some(otel::StatusCode::Error);
}

if self.event_location {
let builder_attrs = builder.attributes.get_or_insert(Vec::new());
if let Some(file) = meta.file() {
#[cfg(feature = "tracing-log")]
builder_attrs.push(KeyValue::new("code.filepath", file.to_owned()));
#[cfg(not(feature = "tracing-log"))]
builder_attrs.push(KeyValue::new("code.filepath", Cow::Borrowed(file)));
}
if let Some(module) = meta.module_path() {
#[cfg(feature = "tracing-log")]
builder_attrs.push(KeyValue::new("code.namespace", module.to_owned()));
#[cfg(not(feature = "tracing-log"))]
builder_attrs.push(KeyValue::new("code.namespace", Cow::Borrowed(module)));
}
if let Some(line) = meta.line() {
builder_attrs.push(KeyValue::new("code.lineno", line as i64));
}
}

if let Some(ref mut events) = builder.events {
events.push(otel_event);
} else {
Expand Down

0 comments on commit 7304921

Please sign in to comment.