Skip to content

Commit 49bd7c8

Browse files
committed
aiohttp-client: add support for OTEL_PYTHON_EXCLUDED_URLS / OTEL_PYTHON_HTTPX_EXCLUDED_URLS
1 parent e5c5e46 commit 49bd7c8

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def response_hook(span: Span, params: typing.Union[
135135
)
136136
from opentelemetry.trace import Span, SpanKind, TracerProvider, get_tracer
137137
from opentelemetry.trace.status import Status, StatusCode
138-
from opentelemetry.util.http import redact_url, sanitize_method
138+
from opentelemetry.util.http import (
139+
get_excluded_urls,
140+
redact_url,
141+
sanitize_method,
142+
)
139143

140144
_UrlFilterT = typing.Optional[typing.Callable[[yarl.URL], str]]
141145
_RequestHookT = typing.Optional[
@@ -271,6 +275,8 @@ def create_trace_config(
271275

272276
metric_attributes = {}
273277

278+
excluded_urls = get_excluded_urls("AIOHTTP_CLIENT")
279+
274280
def _end_trace(trace_config_ctx: types.SimpleNamespace):
275281
elapsed_time = max(default_timer() - trace_config_ctx.start_time, 0)
276282
if trace_config_ctx.token:
@@ -304,7 +310,10 @@ async def on_request_start(
304310
trace_config_ctx: types.SimpleNamespace,
305311
params: aiohttp.TraceRequestStartParams,
306312
):
307-
if not is_instrumentation_enabled():
313+
if (
314+
not is_instrumentation_enabled()
315+
or trace_config_ctx.excluded_urls.url_disabled(str(params.url))
316+
):
308317
trace_config_ctx.span = None
309318
return
310319

@@ -426,6 +435,7 @@ def _trace_config_ctx_factory(**kwargs):
426435
start_time=start_time,
427436
duration_histogram_old=duration_histogram_old,
428437
duration_histogram_new=duration_histogram_new,
438+
excluded_urls=excluded_urls,
429439
**kwargs,
430440
)
431441

instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import asyncio
1818
import contextlib
19+
import os
1920
import typing
2021
import unittest
2122
import urllib.parse
@@ -803,6 +804,24 @@ async def do_request(url):
803804
)
804805
self.memory_exporter.clear()
805806

807+
@mock.patch.dict(
808+
os.environ, {"OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS": "/some/path"}
809+
)
810+
def test_ignores_excluded_urls(self):
811+
async def request_handler(request):
812+
assert "traceparent" not in request.headers
813+
return aiohttp.web.Response(status=HTTPStatus.OK)
814+
815+
host, port = self._http_request(
816+
trace_config=aiohttp_client.create_trace_config(),
817+
request_handler=request_handler,
818+
url="/some/path?query=param&other=param2",
819+
status_code=HTTPStatus.OK,
820+
)
821+
822+
self._assert_spans([], 0)
823+
self._assert_metrics(0)
824+
806825

807826
class TestAioHttpClientInstrumentor(TestBase):
808827
URL = "/test-path"
@@ -1115,6 +1134,21 @@ def response_hook(
11151134
self.assertIn("response_hook_attr", span.attributes)
11161135
self.assertEqual(span.attributes["response_hook_attr"], "value")
11171136

1137+
@mock.patch.dict(
1138+
os.environ, {"OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS": "/test-path"}
1139+
)
1140+
def test_ignores_excluded_urls(self):
1141+
# need the env var set at instrument time
1142+
AioHttpClientInstrumentor().uninstrument()
1143+
AioHttpClientInstrumentor().instrument()
1144+
1145+
url = "/test-path?query=params"
1146+
host, port = run_with_test_server(
1147+
self.get_default_request(url), url, self.default_handler
1148+
)
1149+
self._assert_spans(0)
1150+
self._assert_metrics(0)
1151+
11181152

11191153
class TestLoadingAioHttpInstrumentor(unittest.TestCase):
11201154
def test_loading_instrumentor(self):

0 commit comments

Comments
 (0)