Skip to content

Commit f781de0

Browse files
committed
move FutureExt into context mod
1 parent c589841 commit f781de0

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::Context;
2+
use crate::trace::WithContext;
3+
4+
/// Extension trait allowing futures, streams, and sinks to be traced with a span.
5+
pub trait FutureExt: Sized {
6+
/// Attaches the provided [`Context`] to this type, returning a `WithContext`
7+
/// wrapper.
8+
///
9+
/// When the wrapped type is a future, stream, or sink, the attached context
10+
/// will be set as current while it is being polled.
11+
///
12+
/// [`Context`]: Context
13+
fn with_context(self, otel_cx: Context) -> WithContext<Self> {
14+
WithContext {
15+
inner: self,
16+
otel_cx,
17+
}
18+
}
19+
20+
/// Attaches the current [`Context`] to this type, returning a `WithContext`
21+
/// wrapper.
22+
///
23+
/// When the wrapped type is a future, stream, or sink, the attached context
24+
/// will be set as the default while it is being polled.
25+
///
26+
/// [`Context`]: Context
27+
fn with_current_context(self) -> WithContext<Self> {
28+
let otel_cx = Context::current();
29+
self.with_context(otel_cx)
30+
}
31+
}

opentelemetry/src/context/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mod context_store;
2+
mod future_ext;
23

34
pub use context_store::Context;
4-
pub use context_store::ContextGuard;
5+
pub use context_store::ContextGuard;
6+
pub use future_ext::FutureExt;

opentelemetry/src/trace/context.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use std::{
1515
task::{Context as TaskContext, Poll},
1616
};
1717

18+
// Re-export for compatability. This used to be contained here.
19+
pub use crate::context::FutureExt;
20+
1821
const NOOP_SPAN: SynchronizedSpan = SynchronizedSpan {
1922
span_context: SpanContext::NONE,
2023
inner: None,
@@ -377,8 +380,8 @@ pin_project! {
377380
#[derive(Clone, Debug)]
378381
pub struct WithContext<T> {
379382
#[pin]
380-
inner: T,
381-
otel_cx: Context,
383+
pub(crate) inner: T,
384+
pub(crate) otel_cx: Context,
382385
}
383386
}
384387

@@ -445,31 +448,3 @@ where
445448
}
446449
}
447450

448-
/// Extension trait allowing futures, streams, and sinks to be traced with a span.
449-
pub trait FutureExt: Sized {
450-
/// Attaches the provided [`Context`] to this type, returning a `WithContext`
451-
/// wrapper.
452-
///
453-
/// When the wrapped type is a future, stream, or sink, the attached context
454-
/// will be set as current while it is being polled.
455-
///
456-
/// [`Context`]: crate::Context
457-
fn with_context(self, otel_cx: Context) -> WithContext<Self> {
458-
WithContext {
459-
inner: self,
460-
otel_cx,
461-
}
462-
}
463-
464-
/// Attaches the current [`Context`] to this type, returning a `WithContext`
465-
/// wrapper.
466-
///
467-
/// When the wrapped type is a future, stream, or sink, the attached context
468-
/// will be set as the default while it is being polled.
469-
///
470-
/// [`Context`]: crate::Context
471-
fn with_current_context(self) -> WithContext<Self> {
472-
let otel_cx = Context::current();
473-
self.with_context(otel_cx)
474-
}
475-
}

0 commit comments

Comments
 (0)