Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry: forward event metadata #1911

Merged
merged 6 commits into from
Feb 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
hawkw marked this conversation as resolved.
Show resolved Hide resolved
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));
}
hawkw marked this conversation as resolved.
Show resolved Hide resolved
}

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