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

chore: Move FutureExt into context #2775

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use crate::otel_warn;
#[cfg(feature = "trace")]
use crate::trace::context::SynchronizedSpan;
Expand Down Expand Up @@ -78,7 +79,7 @@ thread_local! {
#[derive(Clone, Default)]
pub struct Context {
#[cfg(feature = "trace")]
pub(super) span: Option<Arc<SynchronizedSpan>>,
pub(crate) span: Option<Arc<SynchronizedSpan>>,
entries: Option<Arc<EntryMap>>,
}

Expand Down Expand Up @@ -314,15 +315,15 @@ impl Context {
}

#[cfg(feature = "trace")]
pub(super) fn current_with_synchronized_span(value: SynchronizedSpan) -> Self {
pub(crate) fn current_with_synchronized_span(value: SynchronizedSpan) -> Self {
Context {
span: Some(Arc::new(value)),
entries: Context::map_current(|cx| cx.entries.clone()),
}
}

#[cfg(feature = "trace")]
pub(super) fn with_synchronized_span(&self, value: SynchronizedSpan) -> Self {
pub(crate) fn with_synchronized_span(&self, value: SynchronizedSpan) -> Self {
Context {
span: Some(Arc::new(value)),
entries: self.entries.clone(),
Expand Down
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
}
6 changes: 6 additions & 0 deletions opentelemetry/src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod context_store;
mod future_ext;

pub use context_store::Context;
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)
}
}
Loading