Skip to content

Commit 9ffd0b8

Browse files
committed
fix aiohttp-client to respect http instrumentation suppression
1 parent 1d97282 commit 9ffd0b8

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def response_hook(span: Span, params: typing.Union[
137137
from opentelemetry.instrumentation.aiohttp_client.version import __version__
138138
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
139139
from opentelemetry.instrumentation.utils import (
140-
is_instrumentation_enabled,
140+
is_http_instrumentation_enabled,
141141
unwrap,
142142
)
143143
from opentelemetry.metrics import MeterProvider, get_meter
@@ -325,7 +325,7 @@ async def on_request_start(
325325
params: aiohttp.TraceRequestStartParams,
326326
):
327327
if (
328-
not is_instrumentation_enabled()
328+
not is_http_instrumentation_enabled()
329329
or trace_config_ctx.excluded_urls.url_disabled(str(params.url))
330330
):
331331
trace_config_ctx.span = None
@@ -485,9 +485,6 @@ def _instrument(
485485

486486
# pylint:disable=unused-argument
487487
def instrumented_init(wrapped, instance, args, kwargs):
488-
if not is_instrumentation_enabled():
489-
return wrapped(*args, **kwargs)
490-
491488
client_trace_configs = list(kwargs.get("trace_configs") or [])
492489
client_trace_configs.extend(trace_configs)
493490

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

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
from opentelemetry.instrumentation.aiohttp_client import (
4141
AioHttpClientInstrumentor,
4242
)
43-
from opentelemetry.instrumentation.utils import suppress_instrumentation
43+
from opentelemetry.instrumentation.utils import (
44+
suppress_http_instrumentation,
45+
suppress_instrumentation,
46+
)
4447
from opentelemetry.semconv._incubating.attributes.http_attributes import (
4548
HTTP_HOST,
4649
HTTP_METHOD,
@@ -1068,33 +1071,60 @@ async def uninstrument_request(server: aiohttp.test_utils.TestServer):
10681071
self._assert_spans(1)
10691072

10701073
def test_suppress_instrumentation(self):
1071-
with suppress_instrumentation():
1072-
run_with_test_server(
1073-
self.get_default_request(), self.URL, self.default_handler
1074-
)
1075-
self._assert_spans(0)
1074+
for suppress_ctx in (
1075+
suppress_instrumentation,
1076+
suppress_http_instrumentation,
1077+
):
1078+
with self.subTest(suppress_ctx=suppress_ctx.__name__):
1079+
with suppress_ctx():
1080+
run_with_test_server(
1081+
self.get_default_request(),
1082+
self.URL,
1083+
self.default_handler,
1084+
)
1085+
self._assert_spans(0)
1086+
self._assert_metrics(0)
10761087

10771088
@staticmethod
1078-
async def suppressed_request(server: aiohttp.test_utils.TestServer):
1079-
async with aiohttp.test_utils.TestClient(server) as client:
1080-
with suppress_instrumentation():
1081-
await client.get(TestAioHttpClientInstrumentor.URL)
1089+
def make_suppressed_request(suppress_ctx):
1090+
async def suppressed_request(server: aiohttp.test_utils.TestServer):
1091+
async with aiohttp.test_utils.TestClient(server) as client:
1092+
with suppress_ctx():
1093+
await client.get(TestAioHttpClientInstrumentor.URL)
1094+
1095+
return suppressed_request
10821096

10831097
def test_suppress_instrumentation_after_creation(self):
1084-
run_with_test_server(
1085-
self.suppressed_request, self.URL, self.default_handler
1086-
)
1087-
self._assert_spans(0)
1098+
for suppress_ctx in (
1099+
suppress_instrumentation,
1100+
suppress_http_instrumentation,
1101+
):
1102+
with self.subTest(suppress_ctx=suppress_ctx.__name__):
1103+
run_with_test_server(
1104+
self.make_suppressed_request(suppress_ctx),
1105+
self.URL,
1106+
self.default_handler,
1107+
)
1108+
self._assert_spans(0)
1109+
self._assert_metrics(0)
10881110

10891111
def test_suppress_instrumentation_with_server_exception(self):
10901112
# pylint:disable=unused-argument
10911113
async def raising_handler(request):
10921114
raise aiohttp.web.HTTPFound(location=self.URL)
10931115

1094-
run_with_test_server(
1095-
self.suppressed_request, self.URL, raising_handler
1096-
)
1097-
self._assert_spans(0)
1116+
for suppress_ctx in (
1117+
suppress_instrumentation,
1118+
suppress_http_instrumentation,
1119+
):
1120+
with self.subTest(suppress_ctx=suppress_ctx.__name__):
1121+
run_with_test_server(
1122+
self.make_suppressed_request(suppress_ctx),
1123+
self.URL,
1124+
raising_handler,
1125+
)
1126+
self._assert_spans(0)
1127+
self._assert_metrics(0)
10981128

10991129
def test_url_filter(self):
11001130
def strip_query_params(url: yarl.URL) -> str:

0 commit comments

Comments
 (0)