Skip to content

Commit 5430c75

Browse files
liustvexrmx
andauthored
feat(asgi): respect suppress_http_instrumentation in ASGI middleware (#4375)
Add is_http_instrumentation_enabled() check in OpenTelemetryMiddleware.__call__ so ASGI server spans are skipped when HTTP instrumentation is suppressed. Every other HTTP instrumentor (client and server) already honors this flag. ASGI was the only one that did not. Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent 4d266a0 commit 5430c75

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515

16+
- `opentelemetry-instrumentation-asgi`: Respect `suppress_http_instrumentation` context in ASGI middleware to skip server span creation when HTTP instrumentation is suppressed
17+
([#4375](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4375))
1618
- `opentelemetry-instrumentation-confluent-kafka`: Loosen confluent-kafka upper bound to <3.0.0
1719
([#4289](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4289))
1820
- `opentelemetry-instrumentation`: Add support for wrapt 2.x

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ def client_response_hook(span: Span, scope: Scope, message: dict[str, Any]):
255255
from opentelemetry.instrumentation.propagators import (
256256
get_global_response_propagator,
257257
)
258-
from opentelemetry.instrumentation.utils import _start_internal_or_server_span
258+
from opentelemetry.instrumentation.utils import (
259+
_start_internal_or_server_span,
260+
is_http_instrumentation_enabled,
261+
)
259262
from opentelemetry.metrics import get_meter
260263
from opentelemetry.propagators.textmap import Getter, Setter
261264
from opentelemetry.semconv._incubating.attributes.http_attributes import (
@@ -745,7 +748,10 @@ async def __call__(
745748
send: An awaitable callable taking a single dictionary as argument.
746749
"""
747750
start = default_timer()
748-
if scope["type"] not in ("http", "websocket"):
751+
if not is_http_instrumentation_enabled() or scope["type"] not in (
752+
"http",
753+
"websocket",
754+
):
749755
return await self.app(scope, receive, send)
750756

751757
_, _, url = get_host_port_url_tuple(scope)

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
get_global_response_propagator,
3838
set_global_response_propagator,
3939
)
40+
from opentelemetry.instrumentation.utils import suppress_http_instrumentation
4041
from opentelemetry.sdk import resources
4142
from opentelemetry.sdk.metrics.export import (
4243
HistogramDataPoint,
@@ -1880,6 +1881,19 @@ async def test_no_excluded_urls(self):
18801881
spans = self.get_finished_spans()
18811882
self.assertGreater(len(spans), 0)
18821883

1884+
async def test_suppress_http_instrumentation(self):
1885+
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
1886+
1887+
async def suppression_wrapper(scope, receive, send):
1888+
with suppress_http_instrumentation():
1889+
await app(scope, receive, send)
1890+
1891+
self.seed_app(suppression_wrapper)
1892+
await self.send_default_request()
1893+
await self.get_all_output()
1894+
spans = self.get_finished_spans()
1895+
self.assertEqual(len(spans), 0)
1896+
18831897

18841898
class TestAsgiAttributes(unittest.TestCase):
18851899
def setUp(self):

0 commit comments

Comments
 (0)