Skip to content

Commit 9dc674f

Browse files
committed
fmt and add test expose more methods as required
1 parent 5a29bf5 commit 9dc674f

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

opentelemetry/src/context.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,13 @@ impl Context {
330330
}
331331
}
332332

333-
fn is_telemetry_suppressed(&self) -> bool {
333+
/// Returns whether telemetry is suppressed in this context.
334+
pub fn is_telemetry_suppressed(&self) -> bool {
334335
self.suppress_telemetry
335336
}
336337

337-
fn with_telemetry_suppressed(&self) -> Self {
338+
/// Returns a new context with telemetry suppression enabled.
339+
pub fn with_telemetry_suppressed(&self) -> Self {
338340
Context {
339341
entries: self.entries.clone(),
340342
#[cfg(feature = "trace")]
@@ -1095,4 +1097,52 @@ mod tests {
10951097
// Back to unsuppressed
10961098
assert!(!Context::is_current_suppressed());
10971099
}
1100+
1101+
#[tokio::test]
1102+
async fn test_async_suppression() {
1103+
async fn nested_operation() {
1104+
assert!(Context::is_current_suppressed());
1105+
1106+
let cx_with_additional_value = Context::current().with_value(ValueB(24));
1107+
1108+
async {
1109+
assert_eq!(
1110+
Context::current().get::<ValueB>(),
1111+
Some(&ValueB(24)),
1112+
"Parent value should still be available after adding new value"
1113+
);
1114+
assert!(Context::is_current_suppressed());
1115+
1116+
// Do some async work to simulate real-world scenario
1117+
sleep(Duration::from_millis(10)).await;
1118+
1119+
// Values should still be available after async work
1120+
assert_eq!(
1121+
Context::current().get::<ValueB>(),
1122+
Some(&ValueB(24)),
1123+
"Parent value should still be available after adding new value"
1124+
);
1125+
assert!(Context::is_current_suppressed());
1126+
}
1127+
.with_context(cx_with_additional_value)
1128+
.await;
1129+
}
1130+
1131+
// Set up suppressed context, but don't attach it to current
1132+
let suppressed_parent = Context::new().with_telemetry_suppressed();
1133+
// Current should not be suppressed as we haven't attached it
1134+
assert!(!Context::is_current_suppressed());
1135+
1136+
// Create and run async operation with the suppressed context explicitly propagated
1137+
nested_operation()
1138+
.with_context(suppressed_parent.clone())
1139+
.await;
1140+
1141+
// After async operation completes:
1142+
// Suppression should be active
1143+
assert!(suppressed_parent.is_telemetry_suppressed());
1144+
1145+
// Current should still be not suppressed
1146+
assert!(!Context::is_current_suppressed());
1147+
}
10981148
}

0 commit comments

Comments
 (0)