Skip to content

Commit

Permalink
move FutureExt into context mod
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgerring committed Mar 10, 2025
1 parent c589841 commit f781de0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
31 changes: 31 additions & 0 deletions opentelemetry/src/context/future_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::Context;
use crate::trace::WithContext;

/// Extension trait allowing futures, streams, and sinks to be traced with a span.
pub trait FutureExt: Sized {
/// Attaches the provided [`Context`] to this type, returning a `WithContext`
/// wrapper.
///
/// When the wrapped type is a future, stream, or sink, the attached context
/// will be set as current while it is being polled.
///
/// [`Context`]: Context
fn with_context(self, otel_cx: Context) -> WithContext<Self> {
WithContext {
inner: self,
otel_cx,
}
}

Check warning on line 18 in opentelemetry/src/context/future_ext.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry/src/context/future_ext.rs#L13-L18

Added lines #L13 - L18 were not covered by tests

/// Attaches the current [`Context`] to this type, returning a `WithContext`
/// wrapper.
///
/// When the wrapped type is a future, stream, or sink, the attached context
/// will be set as the default while it is being polled.
///
/// [`Context`]: Context
fn with_current_context(self) -> WithContext<Self> {
let otel_cx = Context::current();
self.with_context(otel_cx)
}

Check warning on line 30 in opentelemetry/src/context/future_ext.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry/src/context/future_ext.rs#L27-L30

Added lines #L27 - L30 were not covered by tests
}
4 changes: 3 additions & 1 deletion opentelemetry/src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod context_store;
mod future_ext;

pub use context_store::Context;
pub use context_store::ContextGuard;
pub use context_store::ContextGuard;
pub use future_ext::FutureExt;
35 changes: 5 additions & 30 deletions opentelemetry/src/trace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use std::{
task::{Context as TaskContext, Poll},
};

// Re-export for compatability. This used to be contained here.
pub use crate::context::FutureExt;

const NOOP_SPAN: SynchronizedSpan = SynchronizedSpan {
span_context: SpanContext::NONE,
inner: None,
Expand Down Expand Up @@ -377,8 +380,8 @@ pin_project! {
#[derive(Clone, Debug)]
pub struct WithContext<T> {
#[pin]
inner: T,
otel_cx: Context,
pub(crate) inner: T,
pub(crate) otel_cx: Context,
}
}

Expand Down Expand Up @@ -445,31 +448,3 @@ where
}
}

/// Extension trait allowing futures, streams, and sinks to be traced with a span.
pub trait FutureExt: Sized {
/// Attaches the provided [`Context`] to this type, returning a `WithContext`
/// wrapper.
///
/// When the wrapped type is a future, stream, or sink, the attached context
/// will be set as current while it is being polled.
///
/// [`Context`]: crate::Context
fn with_context(self, otel_cx: Context) -> WithContext<Self> {
WithContext {
inner: self,
otel_cx,
}
}

/// Attaches the current [`Context`] to this type, returning a `WithContext`
/// wrapper.
///
/// When the wrapped type is a future, stream, or sink, the attached context
/// will be set as the default while it is being polled.
///
/// [`Context`]: crate::Context
fn with_current_context(self) -> WithContext<Self> {
let otel_cx = Context::current();
self.with_context(otel_cx)
}
}

0 comments on commit f781de0

Please sign in to comment.