Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ tracing-subscriber = { version = "0.3.0", default-features = false, features = [
"std",
] }
tracing-log = { version = "0.2.0", default-features = false, optional = true }
rustversion = "1.0.9"
smallvec = { version = "1.0", optional = true }
thiserror = { version = "2", default-features = false }

# Fix minimal-versions
lazy_static = { version = "1.0.2", optional = true }

[dev-dependencies]
async-trait = "0.1.56"
criterion = { version = "0.5.1", default-features = false, features = [
"html_reports",
] }
Expand All @@ -61,9 +58,7 @@ opentelemetry-otlp = { version = "0.31.0", features = [
opentelemetry-semantic-conventions = { version = "0.31.0", features = [
"semconv_experimental",
] }
futures-util = { version = "0.3.17", default-features = false }
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
tracing = { version = "0.1.35", default-features = false, features = [
"std",
"attributes",
Expand Down
26 changes: 20 additions & 6 deletions src/span_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use opentelemetry::{
trace::{SpanContext, Status, TraceContextExt},
Context, Key, KeyValue, Value,
};
use std::{borrow::Cow, time::SystemTime};
use thiserror::Error;
use std::{borrow::Cow, fmt, time::SystemTime};

/// Utility functions to allow tracing [`Span`]s to accept and return
/// [OpenTelemetry] [`Context`]s.
Expand Down Expand Up @@ -227,28 +226,43 @@ pub trait OpenTelemetrySpanExt {
}

/// An error returned if [`OpenTelemetrySpanExt::set_parent`] could not set the parent.
#[derive(Error, Debug)]
#[derive(Debug)]
pub enum SetParentError {
/// The layer could not be found and therefore the action could not be carried out. This can
/// happen with some advanced layers that do not handle downcasting well, for example
/// [`tracing_subscriber::reload::Layer`].
#[error("OpenTelemetry layer not found")]
LayerNotFound,

/// The span has been already started.
///
/// Someone already called a context-starting method such as [`OpenTelemetrySpanExt::context`]
/// or the span has been entered and automatic context starting was not configured out.
#[error("Span has already been started, cannot set parent")]
AlreadyStarted,

/// The span is filtered out by tracing filters.
///
/// If the filtered out span had children, they will not be connected to the parent span either.
#[error("Span disabled")]
SpanDisabled,
}

impl fmt::Display for SetParentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SetParentError::LayerNotFound => {
write!(f, "OpenTelemetry layer not found")
}
SetParentError::AlreadyStarted => {
write!(f, "Span has already been started, cannot set parent")
}
SetParentError::SpanDisabled => {
write!(f, "Span disabled")
}
}
}
}

impl std::error::Error for SetParentError {}

impl OpenTelemetrySpanExt for tracing::Span {
fn set_parent(&self, cx: Context) -> Result<(), SetParentError> {
let mut cx = Some(cx);
Expand Down
Loading