@@ -330,37 +330,11 @@ impl Context {
330330 }
331331 }
332332
333- /// Returns whether telemetry is suppressed in this context.
334- ///
335- /// # Examples
336- ///
337- /// ```
338- /// use opentelemetry::Context;
339- ///
340- /// let cx = Context::new();
341- /// assert_eq!(cx.is_telemetry_suppressed(), false);
342- ///
343- /// let suppressed_cx = cx.with_telemetry_suppressed();
344- /// assert_eq!(suppressed_cx.is_telemetry_suppressed(), true);
345- /// ```
346- pub fn is_telemetry_suppressed ( & self ) -> bool {
333+ fn is_telemetry_suppressed ( & self ) -> bool {
347334 self . suppress_telemetry
348335 }
349336
350- /// Returns a copy of the context with telemetry suppression enabled.
351- ///
352- /// # Examples
353- ///
354- /// ```
355- /// use opentelemetry::Context;
356- ///
357- /// let cx = Context::new();
358- /// assert_eq!(cx.is_telemetry_suppressed(), false);
359- ///
360- /// let suppressed_cx = cx.with_telemetry_suppressed();
361- /// assert_eq!(suppressed_cx.is_telemetry_suppressed(), true);
362- /// ```
363- pub fn with_telemetry_suppressed ( & self ) -> Self {
337+ fn with_telemetry_suppressed ( & self ) -> Self {
364338 Context {
365339 entries : self . entries . clone ( ) ,
366340 #[ cfg( feature = "trace" ) ]
@@ -369,79 +343,22 @@ impl Context {
369343 }
370344 }
371345
372- /// Returns a copy of the context with telemetry suppression disabled.
373- ///
374- /// # Examples
375- ///
376- /// ```
377- /// use opentelemetry::Context;
378- ///
379- /// let cx = Context::new().with_telemetry_suppressed();
380- /// assert_eq!(cx.is_telemetry_suppressed(), true);
381- ///
382- /// let unsuppressed_cx = cx.with_telemetry_unsuppressed();
383- /// assert_eq!(unsuppressed_cx.is_telemetry_suppressed(), false);
384- /// ```
385- pub fn with_telemetry_unsuppressed ( & self ) -> Self {
386- Context {
387- entries : self . entries . clone ( ) ,
388- #[ cfg( feature = "trace" ) ]
389- span : self . span . clone ( ) ,
390- suppress_telemetry : false ,
391- }
392- }
393-
394- /// Returns a clone of the current thread's context with telemetry suppression enabled.
395- ///
396- /// This is a more efficient form of `Context::current().with_telemetry_suppressed()`
397- /// as it avoids the intermediate context clone.
398- ///
399- /// # Examples
400- ///
401- /// ```
402- /// use opentelemetry::Context;
403- ///
404- /// // Get a suppressed context based on the current one
405- /// let suppressed = Context::current_with_telemetry_suppressed();
406- /// assert_eq!(suppressed.is_telemetry_suppressed(), true);
407- /// ```
408- pub fn current_with_telemetry_suppressed ( ) -> Self {
409- Self :: map_current ( |cx| cx. with_telemetry_suppressed ( ) )
410- }
411-
412- /// Returns a clone of the current thread's context with telemetry suppression disabled.
413- ///
414- /// This is a more efficient form of `Context::current().with_telemetry_unsuppressed()`
415- /// as it avoids the intermediate context clone.
416- ///
417- /// # Examples
418- ///
419- /// ```
420- /// use opentelemetry::Context;
421- ///
422- /// // Get an unsuppressed context based on the current one
423- /// let unsuppressed = Context::current_with_telemetry_unsuppressed();
424- /// assert_eq!(unsuppressed.is_telemetry_suppressed(), false);
425- /// ```
426- pub fn current_with_telemetry_unsuppressed ( ) -> Self {
427- Self :: map_current ( |cx| cx. with_telemetry_unsuppressed ( ) )
428- }
429-
430346 /// Enters a scope where telemetry is suppressed.
431347 ///
432- /// This is a convenience method that creates a new context with telemetry
433- /// suppression enabled and attaches it.
348+ /// This is to be used when you want to suppress telemetry data for the
349+ /// specific context, typically used by Exporters etc. to prevent
350+ /// telemetry-induced-telemetry.
434351 ///
435352 /// # Examples
436353 ///
437354 /// ```
438355 /// use opentelemetry::Context;
439356 ///
440357 /// let _guard = Context::enter_suppressed();
441- /// assert_eq!(Context::current().is_telemetry_suppressed (), true);
358+ /// assert_eq!(Context::is_current_suppressed (), true);
442359 /// ```
443360 pub fn enter_suppressed ( ) -> ContextGuard {
444- Self :: current_with_telemetry_suppressed ( ) . attach ( )
361+ Self :: map_current ( |cx| cx . with_telemetry_suppressed ( ) ) . attach ( )
445362 }
446363
447364 /// Returns whether telemetry is suppressed in the current context.
@@ -466,32 +383,6 @@ impl Context {
466383 Self :: map_current ( |cx| cx. is_telemetry_suppressed ( ) )
467384 }
468385
469- /// Enters a scope where telemetry is not suppressed.
470- ///
471- /// This is a convenience method that creates a new context with telemetry
472- /// suppression disabled and attaches it.
473- ///
474- /// # Examples
475- ///
476- /// ```
477- /// use opentelemetry::Context;
478- ///
479- /// let _guard = Context::enter_suppressed();
480- /// assert_eq!(Context::current().is_telemetry_suppressed(), true);
481- ///
482- /// // Inside a suppressed scope, create an unsuppressed one
483- /// {
484- /// let _inner_guard = Context::enter_unsuppressed();
485- /// assert_eq!(Context::current().is_telemetry_suppressed(), false);
486- /// }
487- ///
488- /// // After inner guard is dropped, we're back to the suppressed scope
489- /// assert_eq!(Context::current().is_telemetry_suppressed(), true);
490- /// ```
491- pub fn enter_unsuppressed ( ) -> ContextGuard {
492- Self :: current_with_telemetry_unsuppressed ( ) . attach ( )
493- }
494-
495386 #[ cfg( feature = "trace" ) ]
496387 pub ( crate ) fn current_with_synchronized_span ( value : SynchronizedSpan ) -> Self {
497388 Self :: map_current ( |cx| Context {
@@ -839,64 +730,111 @@ mod tests {
839730 }
840731
841732 #[ test]
842- fn telemetry_suppression ( ) {
843- // Default context has telemetry suppression disabled
733+ fn test_is_telemetry_suppressed ( ) {
734+ // Default context has suppression disabled
844735 let cx = Context :: new ( ) ;
845- assert ! ( ! cx. is_telemetry_suppressed( ) ) ;
736+ assert_eq ! ( cx. is_telemetry_suppressed( ) , false ) ;
846737
847738 // With suppression enabled
848739 let suppressed = cx. with_telemetry_suppressed ( ) ;
849- assert ! ( suppressed. is_telemetry_suppressed( ) ) ;
740+ assert_eq ! ( suppressed. is_telemetry_suppressed( ) , true ) ;
741+ }
850742
851- // With suppression disabled again
852- let unsuppressed = suppressed. with_telemetry_unsuppressed ( ) ;
853- assert ! ( !unsuppressed. is_telemetry_suppressed( ) ) ;
743+ #[ test]
744+ fn test_with_telemetry_suppressed ( ) {
745+ // Start with a normal context
746+ let cx = Context :: new ( ) ;
747+ assert_eq ! ( cx. is_telemetry_suppressed( ) , false ) ;
854748
855- // Add a value while preserving suppression state
856- let suppressed_with_value = suppressed. with_value ( ValueA ( 1 ) ) ;
857- assert ! ( suppressed_with_value. is_telemetry_suppressed( ) ) ;
858- assert_eq ! ( suppressed_with_value. get:: <ValueA >( ) , Some ( & ValueA ( 1 ) ) ) ;
749+ // Create a suppressed context
750+ let suppressed = cx. with_telemetry_suppressed ( ) ;
751+
752+ // Original should remain unchanged
753+ assert_eq ! ( cx. is_telemetry_suppressed( ) , false ) ;
754+
755+ // New context should be suppressed
756+ assert_eq ! ( suppressed. is_telemetry_suppressed( ) , true ) ;
757+
758+ // Test with values to ensure they're preserved
759+ let cx_with_value = cx. with_value ( ValueA ( 42 ) ) ;
760+ let suppressed_with_value = cx_with_value. with_telemetry_suppressed ( ) ;
761+
762+ assert_eq ! ( cx_with_value. is_telemetry_suppressed( ) , false ) ;
763+ assert_eq ! ( suppressed_with_value. is_telemetry_suppressed( ) , true ) ;
764+ assert_eq ! ( suppressed_with_value. get:: <ValueA >( ) , Some ( & ValueA ( 42 ) ) ) ;
765+ }
859766
860- // Test entering a suppressed scope
767+ #[ test]
768+ fn test_enter_suppressed ( ) {
769+ // Ensure we start with a clean context
770+ let _reset_guard = Context :: new ( ) . attach ( ) ;
771+
772+ // Default context should not be suppressed
773+ assert_eq ! ( Context :: is_current_suppressed( ) , false ) ;
774+
775+ // Enter a suppressed scope
861776 {
862777 let _guard = Context :: enter_suppressed ( ) ;
863- assert ! ( Context :: current( ) . is_telemetry_suppressed( ) ) ;
778+ assert_eq ! ( Context :: is_current_suppressed( ) , true ) ;
779+ assert_eq ! ( Context :: current( ) . is_telemetry_suppressed( ) , true ) ;
780+ }
864781
865- // Test nested scopes with different suppression states
782+ // After guard is dropped, should be back to unsuppressed
783+ assert_eq ! ( Context :: is_current_suppressed( ) , false ) ;
784+ assert_eq ! ( Context :: current( ) . is_telemetry_suppressed( ) , false ) ;
785+ }
786+
787+ #[ test]
788+ fn test_is_current_suppressed ( ) {
789+ // Ensure we start with a clean context
790+ let _reset_guard = Context :: new ( ) . attach ( ) ;
791+
792+ // Default context should not be suppressed
793+ assert_eq ! ( Context :: is_current_suppressed( ) , false ) ;
794+
795+ // Enter a suppressed scope
796+ {
797+ let _guard = Context :: enter_suppressed ( ) ;
798+ assert_eq ! ( Context :: is_current_suppressed( ) , true ) ;
799+
800+ // Test nested context - should not be suppressed
866801 {
867- let _inner_guard = Context :: enter_unsuppressed ( ) ;
868- assert ! ( ! Context :: current ( ) . is_telemetry_suppressed ( ) ) ;
802+ let _inner_guard = Context :: new ( ) . attach ( ) ;
803+ assert_eq ! ( Context :: is_current_suppressed ( ) , false ) ;
869804 }
870805
871- // Back to suppressed
872- assert ! ( Context :: current ( ) . is_telemetry_suppressed ( ) ) ;
806+ // Back to suppressed after inner guard is dropped
807+ assert_eq ! ( Context :: is_current_suppressed ( ) , true ) ;
873808 }
874809
875- // Back to default
876- assert ! ( ! Context :: current ( ) . is_telemetry_suppressed ( ) ) ;
810+ // Back to unsuppressed after outer guard is dropped
811+ assert_eq ! ( Context :: is_current_suppressed ( ) , false ) ;
877812 }
878813
879814 #[ test]
880- fn is_current_suppressed_test ( ) {
881- // Default context has telemetry suppression disabled
882- assert ! ( ! Context :: is_current_suppressed ( ) ) ;
815+ fn test_nested_suppression_scopes ( ) {
816+ // Ensure we start with a clean context
817+ let _reset_guard = Context :: new ( ) . attach ( ) ;
883818
884- // Enter a suppressed scope
819+ // Default context should not be suppressed
820+ assert_eq ! ( Context :: is_current_suppressed( ) , false ) ;
821+
822+ // First level suppression
885823 {
886- let _guard = Context :: enter_suppressed ( ) ;
887- assert ! ( Context :: is_current_suppressed( ) ) ;
824+ let _outer = Context :: enter_suppressed ( ) ;
825+ assert_eq ! ( Context :: is_current_suppressed( ) , true ) ;
888826
889- // Test nested scopes with different suppression states
827+ // Second level suppression (redundant but should work)
890828 {
891- let _inner_guard = Context :: enter_unsuppressed ( ) ;
892- assert ! ( ! Context :: is_current_suppressed( ) ) ;
829+ let _inner = Context :: enter_suppressed ( ) ;
830+ assert_eq ! ( Context :: is_current_suppressed( ) , true ) ;
893831 }
894832
895- // Back to suppressed
896- assert ! ( Context :: is_current_suppressed( ) ) ;
833+ // Still suppressed after inner scope
834+ assert_eq ! ( Context :: is_current_suppressed( ) , true ) ;
897835 }
898836
899- // Back to default
900- assert ! ( ! Context :: is_current_suppressed( ) ) ;
837+ // Back to unsuppressed
838+ assert_eq ! ( Context :: is_current_suppressed( ) , false ) ;
901839 }
902840}
0 commit comments