From d96cbb3a1755f4f6b16d826618ac89199c90527c Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Thu, 28 Apr 2022 21:29:37 +0300 Subject: [PATCH 01/11] feat(aiohttp-trace-configs): support trace_configs as an argument for _instrument --- .../opentelemetry/instrumentation/aiohttp_client/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 515bd126c4..6450d27f28 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 @@ -265,6 +265,7 @@ def _instrument( url_filter: _UrlFilterT = None, request_hook: _RequestHookT = None, response_hook: _ResponseHookT = None, + trace_configs: list = (), ): """Enables tracing of all ClientSessions @@ -276,8 +277,6 @@ 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 ()) - trace_config = create_trace_config( url_filter=url_filter, request_hook=request_hook, @@ -337,6 +336,7 @@ def _instrument(self, **kwargs): 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): From 898bc7fb3c96d49c8a7ea0634a768544594779fb Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sun, 1 May 2022 15:04:30 +0300 Subject: [PATCH 02/11] feat(aiohttp-trace-configs): test added trace config --- .../instrumentation/aiohttp_client/__init__.py | 2 +- .../tests/test_aiohttp_client_integration.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 6450d27f28..3834ef385c 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 @@ -336,7 +336,7 @@ def _instrument(self, **kwargs): url_filter=kwargs.get("url_filter"), request_hook=kwargs.get("request_hook"), response_hook=kwargs.get("response_hook"), - trace_configs=kwargs.get("trace_configs"), + trace_configs=[kwargs.get("trace_config")], ) 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..615ae5aaa2 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 @@ -403,6 +403,17 @@ async def create_session(server: aiohttp.test_utils.TestServer): run_with_test_server(create_session, self.URL, self.default_handler) self.assert_spans(1) + def test_instrument_with_custom_trace_config(self): + AioHttpClientInstrumentor().uninstrument() + self.assert_spans(0) + AioHttpClientInstrumentor().instrument(trace_config=aiohttp_client.create_trace_config()) + + run_with_test_server( + self.get_default_request(), self.URL, self.default_handler + ) + + self.assert_spans(2) + def test_uninstrument(self): AioHttpClientInstrumentor().uninstrument() run_with_test_server( From c25c53f392174e1d70cd9e448f320ce90e7567c2 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sun, 1 May 2022 15:16:27 +0300 Subject: [PATCH 03/11] feat(aiohttp-trace-configs): add in code documentation --- .../opentelemetry/instrumentation/aiohttp_client/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 3564d97523..e12d194ebd 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 @@ -327,6 +327,8 @@ 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"), @@ -340,6 +342,6 @@ def _uninstrument(self, **kwargs): _uninstrument() @staticmethod - def uninstrument_session(client_session: aiohttp.ClientSession): + def uninstrument_session(client_session: aiohttp.TraceConfig .ClientSession): """Disables instrumentation for the given session""" _uninstrument_session(client_session) From 92c901cfa4e42a73fb3db1551d9fb6c33fed62d8 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sun, 1 May 2022 15:57:36 +0300 Subject: [PATCH 04/11] feat(aiohttp-trace-configs): typo fix --- .../opentelemetry/instrumentation/aiohttp_client/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e12d194ebd..dd24cdc963 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 @@ -342,6 +342,6 @@ def _uninstrument(self, **kwargs): _uninstrument() @staticmethod - def uninstrument_session(client_session: aiohttp.TraceConfig .ClientSession): + def uninstrument_session(client_session: aiohttp.ClientSession): """Disables instrumentation for the given session""" _uninstrument_session(client_session) From de86d9054bd9d50926f1cf1e183cb2f6e78ea13e Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sun, 8 May 2022 10:29:05 +0300 Subject: [PATCH 05/11] feat(aiohttp-trace-configs): Add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33af250b7e..e4633df164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.11.1-0.30b1...HEAD) +### Added +- `opentelemetry-instrumentation-aiohttp-client` Add support for optional custom trace_configs argument. + ([1079](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1079)) + ### Fixed - `opentelemetry-instrumentation-aiohttp-client` make span attributes available to sampler ([1072](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1072)) From 56fe09099ebe18e7e8397e2ad4c0fadbc2287680 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sun, 8 May 2022 12:23:29 +0300 Subject: [PATCH 06/11] feat(aiohttp-trace-configs): lint fix --- .../tests/test_aiohttp_client_integration.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 615ae5aaa2..11bb64b6dc 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 @@ -406,7 +406,9 @@ async def create_session(server: aiohttp.test_utils.TestServer): def test_instrument_with_custom_trace_config(self): AioHttpClientInstrumentor().uninstrument() self.assert_spans(0) - AioHttpClientInstrumentor().instrument(trace_config=aiohttp_client.create_trace_config()) + AioHttpClientInstrumentor().instrument( + trace_config=aiohttp_client.create_trace_config() + ) run_with_test_server( self.get_default_request(), self.URL, self.default_handler From a9da45aaf8428e828cbe99083b9256b12379d69a Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Mon, 9 May 2022 17:52:54 +0300 Subject: [PATCH 07/11] feat(aiohttp-trace-configs): py backward computability fix --- .../opentelemetry/instrumentation/aiohttp_client/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 dd24cdc963..82c8185466 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 @@ -335,7 +335,7 @@ def _instrument(self, **kwargs): url_filter=kwargs.get("url_filter"), request_hook=kwargs.get("request_hook"), response_hook=kwargs.get("response_hook"), - trace_configs=[kwargs.get("trace_config")], + trace_configs=list(kwargs.get("trace_configs")), ) def _uninstrument(self, **kwargs): From 89b2b1d2f1a3891737456c43025ba49580c83c28 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Tue, 10 May 2022 22:17:58 +0300 Subject: [PATCH 08/11] feat(aiohttp-trace-configs): cr fixes --- .../instrumentation/aiohttp_client/__init__.py | 6 +++++- .../tests/test_aiohttp_client_integration.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) 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 82c8185466..a02c7c1536 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 @@ -274,6 +274,10 @@ def instrumented_init(wrapped, instance, args, kwargs): if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY): return wrapped(*args, **kwargs) + trace_configs.append(kwargs.get("trace_configs")) if kwargs.get( + "trace_configs" + ) else None + trace_config = create_trace_config( url_filter=url_filter, request_hook=request_hook, @@ -335,7 +339,7 @@ def _instrument(self, **kwargs): url_filter=kwargs.get("url_filter"), request_hook=kwargs.get("request_hook"), response_hook=kwargs.get("response_hook"), - trace_configs=list(kwargs.get("trace_configs")), + trace_configs=list(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 11bb64b6dc..88583615e1 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 @@ -407,7 +407,7 @@ def test_instrument_with_custom_trace_config(self): AioHttpClientInstrumentor().uninstrument() self.assert_spans(0) AioHttpClientInstrumentor().instrument( - trace_config=aiohttp_client.create_trace_config() + trace_configs=aiohttp_client.create_trace_config() ) run_with_test_server( From 6ba255dde5e72bac845292cbffe7bd1dfed60345 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Tue, 31 May 2022 17:21:47 +0300 Subject: [PATCH 09/11] feat(aiohttp-trace-configs): cr fix --- .../opentelemetry/instrumentation/aiohttp_client/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a02c7c1536..e3b4ab8d80 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,7 +262,7 @@ def _instrument( url_filter: _UrlFilterT = None, request_hook: _RequestHookT = None, response_hook: _ResponseHookT = None, - trace_configs: list = (), + trace_configs: Collection = (), ): """Enables tracing of all ClientSessions From 29787c1888fcb0312f862fe6764a74ebf3fbb6e4 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sat, 25 Jun 2022 21:21:06 +0300 Subject: [PATCH 10/11] feat(aiohttp-trace-configs): test fix --- .../aiohttp_client/__init__.py | 13 +++++---- .../tests/test_aiohttp_client_integration.py | 29 ++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) 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 e3b4ab8d80..8a2f17e258 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,21 +262,24 @@ def _instrument( url_filter: _UrlFilterT = None, request_hook: _RequestHookT = None, response_hook: _ResponseHookT = None, - trace_configs: Collection = (), + trace_configs: [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.append(kwargs.get("trace_configs")) if kwargs.get( - "trace_configs" - ) else None + if kwargs.get("trace_configs"): + trace_configs.extend(kwargs.get("trace_configs")) trace_config = create_trace_config( url_filter=url_filter, @@ -339,7 +342,7 @@ def _instrument(self, **kwargs): url_filter=kwargs.get("url_filter"), request_hook=kwargs.get("request_hook"), response_hook=kwargs.get("response_hook"), - trace_configs=list(kwargs.get("trace_configs", [])), + 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 88583615e1..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() @@ -403,19 +417,6 @@ async def create_session(server: aiohttp.test_utils.TestServer): run_with_test_server(create_session, self.URL, self.default_handler) self.assert_spans(1) - def test_instrument_with_custom_trace_config(self): - AioHttpClientInstrumentor().uninstrument() - self.assert_spans(0) - AioHttpClientInstrumentor().instrument( - trace_configs=aiohttp_client.create_trace_config() - ) - - run_with_test_server( - self.get_default_request(), self.URL, self.default_handler - ) - - self.assert_spans(2) - def test_uninstrument(self): AioHttpClientInstrumentor().uninstrument() run_with_test_server( @@ -445,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( From 8bc6483d679004dedd4c17fdc1fd3ce2ecf4255c Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Sun, 26 Jun 2022 10:52:36 +0300 Subject: [PATCH 11/11] feat(aiohttp-trace-configs): cr fix --- CHANGELOG.md | 6 ++---- .../instrumentation/aiohttp_client/__init__.py | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a1ba611ff..6465bbd97e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,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 @@ -37,10 +39,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 -### Added -- `opentelemetry-instrumentation-aiohttp-client` Add support for optional custom trace_configs argument. - ([1079](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1079)) - ### Fixed - `opentelemetry-instrumentation-aiohttp-client` make span attributes available to sampler ([#1072](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1072)) 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 8a2f17e258..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,7 +262,7 @@ def _instrument( url_filter: _UrlFilterT = None, request_hook: _RequestHookT = None, response_hook: _ResponseHookT = None, - trace_configs: [aiohttp.TraceConfig] = None, + trace_configs: typing.Optional[aiohttp.TraceConfig] = None, ): """Enables tracing of all ClientSessions