Skip to content

Commit e643a42

Browse files
committed
chore: change the set_parent method to return a Result
1 parent d4a1255 commit e643a42

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

examples/opentelemetry-remote-context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() {
3737
let app_root = span!(tracing::Level::INFO, "app_start");
3838

3939
// Assign parent trace from external context
40-
app_root.set_parent(parent_context);
40+
let _ = app_root.set_parent(parent_context);
4141

4242
// To include tracing context in client requests from _this_ app,
4343
// use `context` to extract the current OpenTelemetry context.

src/layer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,13 +2254,13 @@ mod tests {
22542254

22552255
let child1 = trace_span!("child-1");
22562256
let root_context = root.context(); // SpanData (None)
2257-
child1.set_parent(root_context); // Clone context, but SpanData(None)
2257+
let _ = child1.set_parent(root_context); // Clone context, but SpanData(None)
22582258

22592259
let _enter_root = root.enter();
22602260
drop(_enter_root);
22612261

22622262
let child2 = trace_span!("child-2");
2263-
child2.set_parent(root.context());
2263+
let _ = child2.set_parent(root.context());
22642264
});
22652265

22662266
// Let's check the spans
@@ -2333,10 +2333,10 @@ mod tests {
23332333
_ = root.enter();
23342334

23352335
let child1 = trace_span!("child-1");
2336-
child1.set_parent(root.context());
2336+
let _ = child1.set_parent(root.context());
23372337

23382338
trace_span!(parent: &child1, "child-2");
2339-
child1.set_parent(root.context()); // <-- this is what causes the issue
2339+
let _ = child1.set_parent(root.context()); // <-- this is what causes the issue
23402340

23412341
trace_span!(parent: &child1, "child-3");
23422342
});

src/span_ext.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub trait OpenTelemetrySpanExt {
1717
/// Associates `self` with a given OpenTelemetry trace, using the provided
1818
/// parent [`Context`].
1919
///
20+
/// This method provides error handling for cases where the span context
21+
/// cannot be set, such as when the OpenTelemetry layer is not present
22+
/// or when the span has already been started.
23+
///
2024
/// [`Context`]: opentelemetry::Context
2125
///
2226
/// # Examples
@@ -41,12 +45,12 @@ pub trait OpenTelemetrySpanExt {
4145
/// let app_root = tracing::span!(tracing::Level::INFO, "app_start");
4246
///
4347
/// // Assign parent trace from external context
44-
/// app_root.set_parent(parent_context.clone());
48+
/// let _ = app_root.set_parent(parent_context.clone());
4549
///
4650
/// // Or if the current span has been created elsewhere:
47-
/// Span::current().set_parent(parent_context);
51+
/// let _ = Span::current().set_parent(parent_context);
4852
/// ```
49-
fn set_parent(&self, cx: Context);
53+
fn set_parent(&self, cx: Context) -> Result<(), &'static str>;
5054

5155
/// Associates `self` with a given OpenTelemetry trace, using the provided
5256
/// followed span [`SpanContext`].
@@ -224,10 +228,14 @@ impl OpenTelemetrySpanExt for tracing::Span {
224228
/// Additionally, once a span has been fully built - and the SpanBuilder has been consumed -
225229
/// the parent _cannot_ be mutated.
226230
///
227-
fn set_parent(&self, cx: Context) {
231+
fn set_parent(&self, cx: Context) -> Result<(), &'static str> {
228232
let mut cx = Some(cx);
233+
let mut result = Ok(());
234+
let result_ref = &mut result;
235+
229236
self.with_subscriber(move |(id, subscriber)| {
230237
let Some(get_context) = subscriber.downcast_ref::<WithContext>() else {
238+
*result_ref = Err("OpenTelemetry layer not found");
231239
return;
232240
};
233241
// Set the parent OTel for the current span
@@ -245,10 +253,14 @@ impl OpenTelemetrySpanExt for tracing::Span {
245253
// new parent context when it's eventually built
246254
*parent_cx = new_cx;
247255
}
248-
OtelDataState::Context { .. } => (),
256+
OtelDataState::Context { .. } => {
257+
*result_ref = Err("Span has already been started, cannot set parent");
258+
}
249259
}
250260
});
251261
});
262+
263+
result
252264
}
253265

254266
fn add_link(&self, cx: SpanContext) {

tests/trace_state_propagation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn trace_with_assigned_otel_context() {
3737

3838
tracing::subscriber::with_default(subscriber, || {
3939
let child = tracing::debug_span!("child");
40-
child.set_parent(cx);
40+
let _ = child.set_parent(cx);
4141
});
4242

4343
drop(provider); // flush all spans
@@ -70,7 +70,7 @@ fn propagate_invalid_context() {
7070

7171
tracing::subscriber::with_default(subscriber, || {
7272
let root = tracing::debug_span!("root");
73-
root.set_parent(invalid_cx);
73+
let _ = root.set_parent(invalid_cx);
7474
root.in_scope(|| tracing::debug_span!("child"));
7575
});
7676

@@ -90,7 +90,7 @@ fn inject_context_into_outgoing_requests() {
9090

9191
tracing::subscriber::with_default(subscriber, || {
9292
let root = tracing::debug_span!("root");
93-
root.set_parent(cx);
93+
let _ = root.set_parent(cx);
9494
let _g = root.enter();
9595
let child = tracing::debug_span!("child");
9696
propagator.inject_context(&child.context(), &mut outgoing_req_carrier);
@@ -125,7 +125,7 @@ fn sampling_decision_respects_new_parent() {
125125
// Observation: if you force the _child_ to materialize before the parent, e.g.,
126126
// if you swap these two lines - bad things will happen, and we shouldn't support
127127
// this.
128-
child.set_parent(Context::current_with_span(root_span));
128+
let _ = child.set_parent(Context::current_with_span(root_span));
129129
child.context(); // force a sampling decision
130130
});
131131

0 commit comments

Comments
 (0)