Skip to content

Commit 15bf875

Browse files
committed
Add suppress_instrumentation functionality
1 parent d73b88f commit 15bf875

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/utils.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pika.spec import Basic, BasicProperties
55

66
from opentelemetry import propagate, trace
7+
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
78
from opentelemetry.propagators.textmap import CarrierT, Getter
89
from opentelemetry.semconv.trace import (
910
MessagingOperationValues,
@@ -76,8 +77,13 @@ def decorated_function(
7677
task_name="(temporary)",
7778
operation=None,
7879
)
80+
if not span:
81+
return original_function(
82+
exchange, routing_key, body, properties, mandatory
83+
)
7984
with trace.use_span(span, end_on_exit=True):
80-
propagate.inject(properties.headers)
85+
if span.is_recording():
86+
propagate.inject(properties.headers)
8187
retval = original_function(
8288
exchange, routing_key, body, properties, mandatory
8389
)
@@ -92,10 +98,14 @@ def get_span(
9298
properties: BasicProperties,
9399
task_name: str,
94100
operation: Optional[MessagingOperationValues] = None,
95-
) -> Span:
101+
) -> Optional[Span]:
96102
if properties.headers is None:
97103
properties.headers = {}
98104
ctx = propagate.extract(properties.headers, getter=pika_getter)
105+
if ctx.get_value("suppress_instrumentation") or ctx.get_value(
106+
_SUPPRESS_INSTRUMENTATION_KEY
107+
):
108+
return None
99109
task_name = properties.type if properties.type else task_name
100110
span = tracer.start_span(
101111
context=ctx, name=generate_span_name(task_name, operation)

instrumentation/opentelemetry-instrumentation-pika/tests/test_pika_instrumentation.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def setUp(self) -> None:
2828
self.channel._impl._consumers = {"mock_key": self.mock_callback}
2929

3030
@mock.patch(
31-
"opentelemetry.instrumentation.pika.PikaInstrumentation.instrument_channel"
31+
"opentelemetry.instrumentation.pika.PikaInstrumentor.instrument_channel"
3232
)
3333
@mock.patch(
34-
"opentelemetry.instrumentation.pika.PikaInstrumentation._uninstrument_channel_functions"
34+
"opentelemetry.instrumentation.pika.PikaInstrumentor._uninstrument_channel_functions"
3535
)
3636
def test_instrument_api(
3737
self,
@@ -54,10 +54,10 @@ def test_instrument_api(
5454
)
5555

5656
@mock.patch(
57-
"opentelemetry.instrumentation.pika.PikaInstrumentation._instrument_channel_functions"
57+
"opentelemetry.instrumentation.pika.PikaInstrumentor._instrument_channel_functions"
5858
)
5959
@mock.patch(
60-
"opentelemetry.instrumentation.pika.PikaInstrumentation._instrument_consumers"
60+
"opentelemetry.instrumentation.pika.PikaInstrumentor._instrument_consumers"
6161
)
6262
def test_instrument(
6363
self,

instrumentation/opentelemetry-instrumentation-pika/tests/test_utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def test_get_span(
3232
channel = mock.MagicMock()
3333
properties = mock.MagicMock()
3434
task_name = "test.test"
35+
context = mock.MagicMock()
36+
context.get_value.return_value = None
37+
extract.return_value = context
3538
span = utils.get_span(tracer, channel, properties, task_name)
3639
extract.assert_called_once()
3740
generate_span_name.assert_called_once()
@@ -44,6 +47,24 @@ def test_get_span(
4447
for call in enrich_span.call_args_list
4548
), "The returned span was not enriched using enrich_span!"
4649

50+
@mock.patch("opentelemetry.instrumentation.pika.utils.generate_span_name")
51+
@mock.patch("opentelemetry.instrumentation.pika.utils.enrich_span")
52+
@mock.patch("opentelemetry.propagate.extract")
53+
def test_get_span_suppressed(
54+
self,
55+
extract: mock.MagicMock,
56+
enrich_span: mock.MagicMock,
57+
generate_span_name: mock.MagicMock,
58+
) -> None:
59+
tracer = mock.MagicMock(spec=Tracer)
60+
channel = mock.MagicMock()
61+
properties = mock.MagicMock()
62+
task_name = "test.test"
63+
span = utils.get_span(tracer, channel, properties, task_name)
64+
self.assertEqual(span, None)
65+
extract.assert_called_once()
66+
generate_span_name.assert_not_called()
67+
4768
def test_generate_span_name_no_operation(self) -> None:
4869
task_name = "test.test"
4970
operation = None

0 commit comments

Comments
 (0)