11use criterion:: { criterion_group, criterion_main, Criterion } ;
2- use opentelemetry:: Context ;
2+ use opentelemetry:: { Context , ContextGuard } ;
33use std:: hint:: black_box;
44
55// Run this benchmark with:
@@ -21,41 +21,56 @@ fn criterion_benchmark(c: &mut Criterion) {
2121
2222 // Benchmark the cost of entering a suppressed scope
2323 group. bench_function ( "enter_telemetry_suppressed_scope" , |b| {
24+ let _restore_ctx = Context :: default ( ) . attach ( ) ;
2425 b. iter ( || {
25- let _guard = black_box ( Context :: enter_telemetry_suppressed_scope ( ) ) ;
26+ let _guard = black_box ( enter_telemetry_suppressed_scope ( ) ) ;
2627 } ) ;
2728 } ) ;
2829
2930 // For comparison - normal context attach
3031 group. bench_function ( "normal_attach" , |b| {
32+ let _restore_ctx = Context :: default ( ) . attach ( ) ;
3133 b. iter ( || {
32- let _guard = black_box ( Context :: current ( ) . attach ( ) ) ;
34+ let _guard = black_box ( normal_attach ( ) ) ;
3335 } ) ;
3436 } ) ;
3537
3638 // Benchmark checking if current is suppressed (when not suppressed)
3739 group. bench_function ( "is_current_telemetry_suppressed_false" , |b| {
3840 // Make sure we're in a non-suppressed context
39- let _restore_ctx = Context :: current ( ) . attach ( ) ;
41+ let _restore_ctx = Context :: default ( ) . attach ( ) ;
4042 b. iter ( || {
41- let is_suppressed = black_box ( Context :: is_current_telemetry_suppressed ( ) ) ;
42- black_box ( is_suppressed) ;
43+ let _is_suppressed = black_box ( is_current_telemetry_suppressed ( ) ) ;
4344 } ) ;
4445 } ) ;
4546
4647 // Benchmark checking if current is suppressed (when suppressed)
4748 group. bench_function ( "is_current_telemetry_suppressed_true" , |b| {
4849 // Enter suppressed context for the duration of the benchmark
49- let _suppressed_guard = Context :: enter_telemetry_suppressed_scope ( ) ;
50+ let _suppressed_guard = Context :: default ( ) . with_telemetry_suppressed ( ) . attach ( ) ;
5051 b. iter ( || {
51- let is_suppressed = black_box ( Context :: is_current_telemetry_suppressed ( ) ) ;
52- black_box ( is_suppressed) ;
52+ let _is_suppressed = black_box ( is_current_telemetry_suppressed ( ) ) ;
5353 } ) ;
5454 } ) ;
5555
5656 group. finish ( ) ;
5757}
5858
59+ #[ inline( never) ]
60+ fn enter_telemetry_suppressed_scope ( ) -> ContextGuard {
61+ Context :: enter_telemetry_suppressed_scope ( )
62+ }
63+
64+ #[ inline( never) ]
65+ fn normal_attach ( ) -> ContextGuard {
66+ Context :: current ( ) . attach ( )
67+ }
68+
69+ #[ inline( never) ]
70+ fn is_current_telemetry_suppressed ( ) -> bool {
71+ Context :: is_current_telemetry_suppressed ( )
72+ }
73+
5974criterion_group ! {
6075 name = benches;
6176 config = Criterion :: default ( )
0 commit comments