@@ -816,3 +816,161 @@ async def sample_async_generator_function(n):
816816 spans = exporter .get_exported_spans ()
817817
818818 assert len (spans ) == 0
819+
820+
821+ # --------- Suppress Instrumentation Tests ---------
822+
823+
824+ def test_suppress_instrumentation_sync_function (setup_tracer ):
825+ """Test that suppress_instrumentation prevents spans from being created for sync functions."""
826+ from opentelemetry .instrumentation .utils import suppress_instrumentation
827+
828+ exporter , provider = setup_tracer
829+
830+ @traced ()
831+ def sample_function (x , y ):
832+ return x + y
833+
834+ # Call without suppression - should create span
835+ result = sample_function (2 , 3 )
836+ assert result == 5
837+
838+ spans = exporter .get_exported_spans ()
839+ assert len (spans ) == 1
840+
841+ exporter .clear_exported_spans ()
842+
843+ # Call with suppression - should NOT create span
844+ with suppress_instrumentation ():
845+ result = sample_function (4 , 5 )
846+ assert result == 9
847+
848+ provider .shutdown ()
849+ spans = exporter .get_exported_spans ()
850+ assert len (spans ) == 0
851+
852+
853+ @pytest .mark .asyncio
854+ async def test_suppress_instrumentation_async_function (setup_tracer ):
855+ """Test that suppress_instrumentation prevents spans from being created for async functions."""
856+ from opentelemetry .instrumentation .utils import suppress_instrumentation
857+
858+ exporter , provider = setup_tracer
859+
860+ @traced ()
861+ async def sample_async_function (x , y ):
862+ return x * y
863+
864+ # Call without suppression - should create span
865+ result = await sample_async_function (2 , 3 )
866+ assert result == 6
867+
868+ await sleep (0.1 )
869+ spans = exporter .get_exported_spans ()
870+ assert len (spans ) == 1
871+
872+ exporter .clear_exported_spans ()
873+
874+ # Call with suppression - should NOT create span
875+ with suppress_instrumentation ():
876+ result = await sample_async_function (4 , 5 )
877+ assert result == 20
878+
879+ provider .shutdown ()
880+ await sleep (0.1 )
881+ spans = exporter .get_exported_spans ()
882+ assert len (spans ) == 0
883+
884+
885+ def test_suppress_instrumentation_generator_function (setup_tracer ):
886+ """Test that suppress_instrumentation prevents spans from being created for generator functions."""
887+ from opentelemetry .instrumentation .utils import suppress_instrumentation
888+
889+ exporter , provider = setup_tracer
890+
891+ @traced ()
892+ def sample_generator_function (n ):
893+ for i in range (n ):
894+ yield i
895+
896+ # Call without suppression - should create span
897+ results = list (sample_generator_function (3 ))
898+ assert results == [0 , 1 , 2 ]
899+
900+ spans = exporter .get_exported_spans ()
901+ assert len (spans ) == 1
902+
903+ exporter .clear_exported_spans ()
904+
905+ # Call with suppression - should NOT create span
906+ with suppress_instrumentation ():
907+ results = list (sample_generator_function (4 ))
908+ assert results == [0 , 1 , 2 , 3 ]
909+
910+ provider .shutdown ()
911+ spans = exporter .get_exported_spans ()
912+ assert len (spans ) == 0
913+
914+
915+ @pytest .mark .asyncio
916+ async def test_suppress_instrumentation_async_generator_function (setup_tracer ):
917+ """Test that suppress_instrumentation prevents spans from being created for async generator functions."""
918+ from opentelemetry .instrumentation .utils import suppress_instrumentation
919+
920+ exporter , provider = setup_tracer
921+
922+ @traced ()
923+ async def sample_async_generator_function (n ):
924+ for i in range (n ):
925+ yield i
926+
927+ # Call without suppression - should create span
928+ results = [item async for item in sample_async_generator_function (3 )]
929+ assert results == [0 , 1 , 2 ]
930+
931+ spans = exporter .get_exported_spans ()
932+ assert len (spans ) == 1
933+
934+ exporter .clear_exported_spans ()
935+
936+ # Call with suppression - should NOT create span
937+ with suppress_instrumentation ():
938+ results = [item async for item in sample_async_generator_function (4 )]
939+ assert results == [0 , 1 , 2 , 3 ]
940+
941+ provider .shutdown ()
942+ spans = exporter .get_exported_spans ()
943+ assert len (spans ) == 0
944+
945+
946+ def test_suppress_instrumentation_nested_functions (setup_tracer ):
947+ """Test that suppress_instrumentation prevents spans for nested traced function calls."""
948+ from opentelemetry .instrumentation .utils import suppress_instrumentation
949+
950+ exporter , provider = setup_tracer
951+
952+ @traced ()
953+ def inner_function (x ):
954+ return x * 2
955+
956+ @traced ()
957+ def outer_function (x ):
958+ return inner_function (x ) + 1
959+
960+ # Call without suppression - should create 2 spans
961+ result = outer_function (5 )
962+ assert result == 11
963+
964+ spans = exporter .get_exported_spans ()
965+ assert len (spans ) == 2
966+
967+ exporter .clear_exported_spans ()
968+
969+ # Call with suppression - should NOT create any spans
970+ with suppress_instrumentation ():
971+ result = outer_function (10 )
972+ assert result == 21
973+
974+ provider .shutdown ()
975+ spans = exporter .get_exported_spans ()
976+ assert len (spans ) == 0
0 commit comments