Skip to content

Commit a0f92eb

Browse files
authored
[trace] Move mark_as_active_span and get_active_span into context (open-telemetry#310)
1 parent f99535d commit a0f92eb

File tree

3 files changed

+79
-77
lines changed

3 files changed

+79
-77
lines changed

src/api/trace/context.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Context extensions for tracing
2-
use crate::Context;
2+
use crate::{Context, ContextGuard};
33
lazy_static::lazy_static! {
44
static ref NOOP_SPAN: crate::trace::NoopSpan = crate::trace::NoopSpan::new();
55
}
66

77
struct Span(Box<dyn crate::trace::Span + Send + Sync>);
8+
89
struct RemoteSpanContext(crate::trace::SpanContext);
910

1011
/// Methods for storing and retrieving trace data in a context.
@@ -79,3 +80,69 @@ impl TraceContextExt for Context {
7980
.map(|span_context| &span_context.0)
8081
}
8182
}
83+
84+
/// Mark a given `Span` as active.
85+
///
86+
/// The `Tracer` MUST provide a way to update its active `Span`, and MAY provide convenience
87+
/// methods to manage a `Span`'s lifetime and the scope in which a `Span` is active. When an
88+
/// active `Span` is made inactive, the previously-active `Span` SHOULD be made active. A `Span`
89+
/// maybe finished (i.e. have a non-null end time) but still be active. A `Span` may be active
90+
/// on one thread after it has been made inactive on another.
91+
///
92+
/// # Examples
93+
///
94+
/// ```
95+
/// use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
96+
/// use opentelemetry::trace::{get_active_span, mark_span_as_active};
97+
///
98+
/// fn my_function() {
99+
/// let tracer = global::tracer("my-component-a");
100+
/// // start an active span in one function
101+
/// let span = tracer.start("span-name");
102+
/// let _guard = mark_span_as_active(span);
103+
/// // anything happening in functions we call can still access the active span...
104+
/// my_other_function();
105+
/// }
106+
///
107+
/// fn my_other_function() {
108+
/// // call methods on the current span from
109+
/// get_active_span(|span| {
110+
/// span.add_event("An event!".to_string(), vec![KeyValue::new("happened", true)]);
111+
/// });
112+
/// }
113+
/// ```
114+
#[must_use = "Dropping the guard detaches the context."]
115+
pub fn mark_span_as_active<T: crate::trace::Span + Send + Sync>(span: T) -> ContextGuard {
116+
let cx = Context::current_with_span(span);
117+
cx.attach()
118+
}
119+
120+
/// Executes a closure with a reference to this thread's current span.
121+
///
122+
/// # Examples
123+
///
124+
/// ```
125+
/// use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
126+
/// use opentelemetry::trace::get_active_span;
127+
///
128+
/// fn my_function() {
129+
/// // start an active span in one function
130+
/// global::tracer("my-component").in_span("span-name", |_cx| {
131+
/// // anything happening in functions we call can still access the active span...
132+
/// my_other_function();
133+
/// })
134+
/// }
135+
///
136+
/// fn my_other_function() {
137+
/// // call methods on the current span from
138+
/// get_active_span(|span| {
139+
/// span.add_event("An event!".to_string(), vec![KeyValue::new("happened", true)]);
140+
/// })
141+
/// }
142+
/// ```
143+
pub fn get_active_span<F, T>(f: F) -> T
144+
where
145+
F: FnOnce(&dyn crate::trace::Span) -> T,
146+
{
147+
f(Context::current().span())
148+
}

src/api/trace/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ mod span_processor;
123123
mod tracer;
124124

125125
pub use self::{
126-
context::TraceContextExt,
126+
context::{get_active_span, mark_span_as_active, TraceContextExt},
127127
event::Event,
128128
futures::FutureExt,
129129
id_generator::IdGenerator,

src/api/trace/tracer.rs

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
trace::{
44
Event, Link, Span, SpanContext, SpanId, SpanKind, StatusCode, TraceContextExt, TraceId,
55
},
6-
Context, ContextGuard, KeyValue,
6+
Context, KeyValue,
77
};
88
use std::fmt;
99
use std::time::SystemTime;
@@ -86,15 +86,15 @@ use std::time::SystemTime;
8686
/// greater control over when the span is no longer considered active.
8787
///
8888
/// ```
89-
/// use opentelemetry::{global, trace::{Span, Tracer}};
89+
/// use opentelemetry::{global, trace::{Span, Tracer, mark_span_as_active}};
9090
/// let tracer = global::tracer("my-component");
9191
///
9292
/// let parent_span = tracer.start("foo");
93-
/// let parent_active = tracer.mark_span_as_active(parent_span);
93+
/// let parent_active = mark_span_as_active(parent_span);
9494
///
9595
/// {
9696
/// let child = tracer.start("bar");
97-
/// let _child_active = tracer.mark_span_as_active(child);
97+
/// let _child_active = mark_span_as_active(child);
9898
///
9999
/// // do work in the context of the child span...
100100
///
@@ -115,12 +115,12 @@ use std::time::SystemTime;
115115
/// the following example _will not_ work:
116116
///
117117
/// ```no_run
118-
/// # use opentelemetry::{global, trace::Tracer};
118+
/// # use opentelemetry::{global, trace::{Tracer, mark_span_as_active}};
119119
/// # let tracer = global::tracer("foo");
120120
/// # let span = tracer.start("foo-span");
121121
/// async {
122122
/// // Does not work
123-
/// let _g = tracer.mark_span_as_active(span);
123+
/// let _g = mark_span_as_active(span);
124124
/// // ...
125125
/// };
126126
/// ```
@@ -230,71 +230,6 @@ pub trait Tracer: fmt::Debug + 'static {
230230
/// Create a span from a `SpanBuilder`
231231
fn build_with_context(&self, builder: SpanBuilder, cx: &Context) -> Self::Span;
232232

233-
/// Mark a given `Span` as active.
234-
///
235-
/// The `Tracer` MUST provide a way to update its active `Span`, and MAY provide convenience
236-
/// methods to manage a `Span`'s lifetime and the scope in which a `Span` is active. When an
237-
/// active `Span` is made inactive, the previously-active `Span` SHOULD be made active. A `Span`
238-
/// maybe finished (i.e. have a non-null end time) but still be active. A `Span` may be active
239-
/// on one thread after it has been made inactive on another.
240-
///
241-
/// # Examples
242-
///
243-
/// ```
244-
/// use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
245-
///
246-
/// fn my_function() {
247-
/// let tracer = global::tracer("my-component-a");
248-
/// // start an active span in one function
249-
/// let span = tracer.start("span-name");
250-
/// let _guard = tracer.mark_span_as_active(span);
251-
/// // anything happening in functions we call can still access the active span...
252-
/// my_other_function();
253-
/// }
254-
///
255-
/// fn my_other_function() {
256-
/// // call methods on the current span from
257-
/// global::tracer("my-component-b").get_active_span(|span| {
258-
/// span.add_event("An event!".to_string(), vec![KeyValue::new("happened", true)]);
259-
/// });
260-
/// }
261-
/// ```
262-
#[must_use = "Dropping the guard detaches the context."]
263-
fn mark_span_as_active(&self, span: Self::Span) -> ContextGuard {
264-
let cx = Context::current_with_span(span);
265-
cx.attach()
266-
}
267-
268-
/// Executes a closure with a reference to this thread's current span.
269-
///
270-
/// # Examples
271-
///
272-
/// ```
273-
/// use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
274-
///
275-
/// fn my_function() {
276-
/// // start an active span in one function
277-
/// global::tracer("my-component").in_span("span-name", |_cx| {
278-
/// // anything happening in functions we call can still access the active span...
279-
/// my_other_function();
280-
/// })
281-
/// }
282-
///
283-
/// fn my_other_function() {
284-
/// // call methods on the current span from
285-
/// global::tracer("my-component").get_active_span(|span| {
286-
/// span.add_event("An event!".to_string(), vec![KeyValue::new("happened", true)]);
287-
/// })
288-
/// }
289-
/// ```
290-
fn get_active_span<F, T>(&self, f: F) -> T
291-
where
292-
F: FnOnce(&dyn Span) -> T,
293-
Self: Sized,
294-
{
295-
f(Context::current().span())
296-
}
297-
298233
/// Start a new span and execute the given closure with reference to the span's
299234
/// context.
300235
///
@@ -305,7 +240,7 @@ pub trait Tracer: fmt::Debug + 'static {
305240
/// # Examples
306241
///
307242
/// ```
308-
/// use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
243+
/// use opentelemetry::{global, trace::{Span, Tracer, get_active_span}, KeyValue};
309244
///
310245
/// fn my_function() {
311246
/// // start an active span in one function
@@ -317,7 +252,7 @@ pub trait Tracer: fmt::Debug + 'static {
317252
///
318253
/// fn my_other_function() {
319254
/// // call methods on the current span from
320-
/// global::tracer("my-component").get_active_span(|span| {
255+
/// get_active_span(|span| {
321256
/// span.add_event("An event!".to_string(), vec![KeyValue::new("happened", true)]);
322257
/// })
323258
/// }
@@ -343,7 +278,7 @@ pub trait Tracer: fmt::Debug + 'static {
343278
/// # Examples
344279
///
345280
/// ```
346-
/// use opentelemetry::{global, trace::{Span, SpanKind, Tracer}, KeyValue};
281+
/// use opentelemetry::{global, trace::{Span, SpanKind, Tracer, get_active_span}, KeyValue};
347282
///
348283
/// fn my_function() {
349284
/// let tracer = global::tracer("my-component");
@@ -358,7 +293,7 @@ pub trait Tracer: fmt::Debug + 'static {
358293
///
359294
/// fn my_other_function() {
360295
/// // call methods on the current span from
361-
/// global::tracer("my-component").get_active_span(|span| {
296+
/// get_active_span(|span| {
362297
/// span.add_event("An event!".to_string(), vec![KeyValue::new("happened", true)]);
363298
/// })
364299
/// }

0 commit comments

Comments
 (0)