diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c9fc6747b..4fb2671c8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `opentelemetry-instrumentation-aiohttp-client` Add support for optional custom trace_configs argument. + ([1079](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1079)) - `opentelemetry-instrumentation-sqlalchemy` add support to instrument multiple engines ([#1132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1132)) - `opentelemetry-instrumentation-logging` add log hook support diff --git a/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py b/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py index 49652a2c11..e2eaaa7442 100644 --- a/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py @@ -262,18 +262,24 @@ def _instrument( url_filter: _UrlFilterT = None, request_hook: _RequestHookT = None, response_hook: _ResponseHookT = None, + trace_configs: typing.Optional[aiohttp.TraceConfig] = None, ): """Enables tracing of all ClientSessions When a ClientSession gets created a TraceConfig is automatically added to the session's trace_configs. """ + + if trace_configs is None: + trace_configs = [] + # pylint:disable=unused-argument def instrumented_init(wrapped, instance, args, kwargs): if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY): return wrapped(*args, **kwargs) - trace_configs = list(kwargs.get("trace_configs") or ()) + if kwargs.get("trace_configs"): + trace_configs.extend(kwargs.get("trace_configs")) trace_config = create_trace_config( url_filter=url_filter, @@ -328,12 +334,15 @@ def _instrument(self, **kwargs): such as API keys or user personal information. ``request_hook``: An optional callback that is invoked right after a span is created. ``response_hook``: An optional callback which is invoked right before the span is finished processing a response. + ``trace_configs``: An optional list of aiohttp.TraceConfig items, allowing customize enrichment of spans + based on aiohttp events (see specification: https://docs.aiohttp.org/en/stable/tracing_reference.html) """ _instrument( tracer_provider=kwargs.get("tracer_provider"), url_filter=kwargs.get("url_filter"), request_hook=kwargs.get("request_hook"), response_hook=kwargs.get("response_hook"), + trace_configs=kwargs.get("trace_configs"), ) def _uninstrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py b/instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py index 800ee1ba6b..92ca8f55be 100644 --- a/instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py +++ b/instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py @@ -386,6 +386,20 @@ def test_instrument(self): ) self.assertEqual(200, span.attributes[SpanAttributes.HTTP_STATUS_CODE]) + def test_instrument_with_custom_trace_config(self): + AioHttpClientInstrumentor().uninstrument() + AioHttpClientInstrumentor().instrument( + trace_configs=[aiohttp_client.create_trace_config()] + ) + + self.assert_spans(0) + + run_with_test_server( + self.get_default_request(), self.URL, self.default_handler + ) + + self.assert_spans(2) + def test_instrument_with_existing_trace_config(self): trace_config = aiohttp.TraceConfig() @@ -432,7 +446,7 @@ async def uninstrument_request(server: aiohttp.test_utils.TestServer): run_with_test_server( self.get_default_request(), self.URL, self.default_handler ) - self.assert_spans(1) + self.assert_spans(2) def test_suppress_instrumentation(self): token = context.attach(