From 295a6cb0cd1dceec00bddf09dc87ef9955b308e0 Mon Sep 17 00:00:00 2001 From: Nina Stawski Date: Wed, 27 Apr 2022 14:05:15 -0700 Subject: [PATCH 1/3] Make span attributes available in aiohttp-client Fixes opentelemetry-python-contrib#939 --- CHANGELOG.md | 2 ++ .../aiohttp_client/__init__.py | 19 +++++++------------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bcd1ecf2a..3c892e6af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ 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) ### Fixed +- `opentelemetry-instrumentation-aiohttp-client` make span attributes available to sampler + ([1072](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1072)) - `opentelemetry-instrumentation-aws-lambda` Fixed an issue - in some rare cases (API GW proxy integration test) headers are set to None, breaking context propagators. ([#1055](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1055)) 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..ec720b5700 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 @@ -180,27 +180,22 @@ async def on_request_start( http_method = params.method.upper() request_span_name = f"HTTP {http_method}" + request_url = remove_url_credentials(trace_config_ctx.url_filter(params.url)) if callable(trace_config_ctx.url_filter) else remove_url_credentials(str(params.url)), + + span_attributes = { + SpanAttributes.HTTP_METHOD: http_method, + SpanAttributes.HTTP_URL: request_url, + } trace_config_ctx.span = trace_config_ctx.tracer.start_span( request_span_name, kind=SpanKind.CLIENT, + attributes=span_attributes ) if callable(request_hook): request_hook(trace_config_ctx.span, params) - if trace_config_ctx.span.is_recording(): - attributes = { - SpanAttributes.HTTP_METHOD: http_method, - SpanAttributes.HTTP_URL: remove_url_credentials( - trace_config_ctx.url_filter(params.url) - ) - if callable(trace_config_ctx.url_filter) - else remove_url_credentials(str(params.url)), - } - for key, value in attributes.items(): - trace_config_ctx.span.set_attribute(key, value) - trace_config_ctx.token = context_api.attach( trace.set_span_in_context(trace_config_ctx.span) ) From b5d806208b337c4340f88d3a5ff14853f65a23ff Mon Sep 17 00:00:00 2001 From: Nina Stawski Date: Wed, 27 Apr 2022 14:37:32 -0700 Subject: [PATCH 2/3] Fixed the test failure --- .../instrumentation/aiohttp_client/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 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 ec720b5700..aa2a18986a 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 @@ -180,7 +180,11 @@ async def on_request_start( http_method = params.method.upper() request_span_name = f"HTTP {http_method}" - request_url = remove_url_credentials(trace_config_ctx.url_filter(params.url)) if callable(trace_config_ctx.url_filter) else remove_url_credentials(str(params.url)), + request_url = ( + remove_url_credentials(trace_config_ctx.url_filter(params.url)) + if callable(trace_config_ctx.url_filter) + else remove_url_credentials(str(params.url)), + ) span_attributes = { SpanAttributes.HTTP_METHOD: http_method, @@ -188,9 +192,7 @@ async def on_request_start( } trace_config_ctx.span = trace_config_ctx.tracer.start_span( - request_span_name, - kind=SpanKind.CLIENT, - attributes=span_attributes + request_span_name, kind=SpanKind.CLIENT, attributes=span_attributes ) if callable(request_hook): From 45b2b5c8f478b0168b4c5cce56c94d9983d1b8c1 Mon Sep 17 00:00:00 2001 From: Nina Stawski Date: Thu, 28 Apr 2022 11:47:32 -0700 Subject: [PATCH 3/3] Update instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py Co-authored-by: Srikanth Chekuri --- .../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 aa2a18986a..49652a2c11 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 @@ -183,7 +183,7 @@ async def on_request_start( request_url = ( remove_url_credentials(trace_config_ctx.url_filter(params.url)) if callable(trace_config_ctx.url_filter) - else remove_url_credentials(str(params.url)), + else remove_url_credentials(str(params.url)) ) span_attributes = {