Skip to content

Commit b6faf30

Browse files
test(TestAsgiApplication): add test for internal span exclusion
Tested-by: Tobias Backer Dirks <[email protected]>
1 parent 5b7759a commit b6faf30

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
185185
Note:
186186
The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.
187187
188+
Internal HTTP spans
189+
*****************************
190+
Internal HTTP send and receive spans are added by default. These can optionally be excluded by setting the boolean environment variables
191+
``OTEL_PYTHON_ASGI_EXCLUDE_SEND_SPAN`` and ``OTEL_PYTHON_ASGI_EXCLUDE_RECEIVE_SPAN`` to ``true``.
192+
188193
API
189194
---
190195
"""

Diff for: instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

+21
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,27 @@ def test_background_execution(self):
572572
_SIMULATED_BACKGROUND_TASK_EXECUTION_TIME_S * 10**9,
573573
)
574574

575+
def test_exclude_internal_spans(self):
576+
"""Test that internal spans are excluded from the emitted spans when
577+
the `exclude_receive_span` or `exclude_send_span` attributes are set.
578+
"""
579+
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
580+
self.seed_app(app)
581+
cases = [
582+
(True, True, ["GET / http receive", "GET / http send"]),
583+
(False, True, ["GET / http send"]),
584+
(True, False, ["GET / http receive"]),
585+
(False, False, []),
586+
]
587+
for exclude_receive_span, exclude_send_span, excluded_spans in cases:
588+
app.exclude_receive_span = exclude_receive_span
589+
app.exclude_send_span = exclude_send_span
590+
self.send_default_request()
591+
span_list = self.memory_exporter.get_finished_spans()
592+
for span in span_list:
593+
for excluded_span in excluded_spans:
594+
self.assertNotEqual(span.name, excluded_span)
595+
575596
def test_trailers(self):
576597
"""Test that trailers are emitted as expected and that the server span is ended
577598
BEFORE the background task is finished."""

0 commit comments

Comments
 (0)