Skip to content

feat: Add Suppression flag to context #2821

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

Merged
merged 25 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
361f2cd
feat: Add Suppression flag to context
cijothomas Mar 18, 2025
ea670ca
simplif
cijothomas Mar 18, 2025
7e64aa9
nit comment
cijothomas Mar 18, 2025
5f55f42
clippy to simplify
cijothomas Mar 18, 2025
23ce8e3
fix perf
cijothomas Mar 18, 2025
84f3727
inline boost
cijothomas Mar 18, 2025
f9678c0
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 18, 2025
c2714c1
Merge branch 'main' into cijothomas/context-suppress
lalitb Mar 18, 2025
a4d8c70
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 18, 2025
f5ea469
reduce public api
cijothomas Mar 18, 2025
f4bb48f
comment on text
cijothomas Mar 19, 2025
ba4be0a
clip
cijothomas Mar 19, 2025
32d217c
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 19, 2025
fb73294
comment
cijothomas Mar 19, 2025
5a29bf5
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 22, 2025
9dc674f
fmt and add test expose more methods as required
cijothomas Mar 22, 2025
c043437
rename
cijothomas Mar 22, 2025
5ac4cda
fmts
cijothomas Mar 23, 2025
b474512
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 24, 2025
73518bc
remove dummywor
cijothomas Mar 25, 2025
c7ba6a0
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 25, 2025
1550d53
improve tests
cijothomas Mar 26, 2025
ce59a31
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 26, 2025
0148509
Merge branch 'main' into cijothomas/context-suppress
cijothomas Mar 26, 2025
e13c2c6
Merge branch 'main' into cijothomas/context-suppress
lalitb Mar 26, 2025
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
20 changes: 20 additions & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

## vNext

Added the ability to prevent recursive telemetry generation through new
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add PR link as well.

context-based suppression mechanisms. This feature helps prevent feedback loops
and excessive telemetry when OpenTelemetry components perform their own
operations.

New methods added to `Context`:

- `is_telemetry_suppressed()` - Checks if telemetry is suppressed in this
context
- `with_telemetry_suppressed()` - Creates a new context with telemetry
suppression enabled
- `is_current_telemetry_suppressed()` - Efficiently checks if the current thread's context
has telemetry suppressed
- `enter_telemetry_suppressed_scope()` - Convenience method to enter a scope where telemetry is
suppressed

These methods allow SDK components, exporters, and processors to temporarily
disable telemetry generation during their internal operations, ensuring more
predictable and efficient observability pipelines.

## 0.29.0

Released 2025-Mar-21
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ name = "context_attach"
harness = false
required-features = ["tracing"]

[[bench]]
name = "context_suppression"
harness = false

[[bench]]
name = "baggage"
harness = false
Expand Down
66 changes: 66 additions & 0 deletions opentelemetry/benches/context_suppression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use opentelemetry::Context;

// Run this benchmark with:
// cargo bench --bench context_suppression

// The benchmark results:
// criterion = "0.5.1"
// Hardware: Apple M4 Pro
// Total Number of Cores:   14 (10 performance and 4 efficiency)
// | Benchmark | Time |
// |---------------------------------------|--------|
// | enter_telemetry_suppressed_scope | 9.0 ns |
// | normal_attach | 9.0 ns |
// | is_current_telemetry_suppressed_false | 750 ps |
// | is_current_telemetry_suppressed_true | 750 ps |

fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("telemetry_suppression");

// Benchmark the cost of entering a suppressed scope
group.bench_function("enter_telemetry_suppressed_scope", |b| {
b.iter(|| {
let _guard = black_box(Context::enter_telemetry_suppressed_scope());
let _ = black_box(dummy_work());
});
});

// For comparison - normal context attach
group.bench_function("normal_attach", |b| {
b.iter(|| {
let _guard = black_box(Context::current().attach());
let _ = black_box(dummy_work());
});
});

// Benchmark checking if current is suppressed (when not suppressed)
group.bench_function("is_current_telemetry_suppressed_false", |b| {
// Make sure we're in a non-suppressed context
let _restore_ctx = Context::current().attach();
b.iter(|| {
let is_suppressed = black_box(Context::is_current_telemetry_suppressed());
black_box(is_suppressed);
});
});

// Benchmark checking if current is suppressed (when suppressed)
group.bench_function("is_current_telemetry_suppressed_true", |b| {
// Enter suppressed context for the duration of the benchmark
let _suppressed_guard = Context::enter_telemetry_suppressed_scope();
b.iter(|| {
let is_suppressed = black_box(Context::is_current_telemetry_suppressed());
black_box(is_suppressed);
});
});

group.finish();
}

#[inline(never)]
fn dummy_work() -> i32 {
black_box(1 + 1)
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading