Skip to content

Commit 934af7e

Browse files
authoredAug 27, 2022
aiohttp-client: Fix producing additional spans with each newly created ClientSession (#1246)
1 parent 03d97ff commit 934af7e

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed
 

Diff for: ‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- `opentelemetry-instrumentation-boto3sqs` Make propagation compatible with other SQS instrumentations, add 'messaging.url' span attribute, and fix missing package dependencies.
1313
([#1234](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1234))
14-
1514
- restoring metrics in django framework
1615
([#1208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1208))
16+
- `opentelemetry-instrumentation-aiohttp-client` Fix producing additional spans with each newly created ClientSession
17+
- ([#1246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1246))
1718

1819
## [1.12.0-0.33b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0-0.33b0) - 2022-08-08
1920

Diff for: ‎instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,25 @@ def _instrument(
262262
url_filter: _UrlFilterT = None,
263263
request_hook: _RequestHookT = None,
264264
response_hook: _ResponseHookT = None,
265-
trace_configs: typing.Optional[aiohttp.TraceConfig] = None,
265+
trace_configs: typing.Optional[
266+
typing.Sequence[aiohttp.TraceConfig]
267+
] = None,
266268
):
267269
"""Enables tracing of all ClientSessions
268270
269271
When a ClientSession gets created a TraceConfig is automatically added to
270272
the session's trace_configs.
271273
"""
272274

273-
if trace_configs is None:
274-
trace_configs = []
275+
trace_configs = trace_configs or ()
275276

276277
# pylint:disable=unused-argument
277278
def instrumented_init(wrapped, instance, args, kwargs):
278279
if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
279280
return wrapped(*args, **kwargs)
280281

281-
if kwargs.get("trace_configs"):
282-
trace_configs.extend(kwargs.get("trace_configs"))
282+
client_trace_configs = list(kwargs.get("trace_configs", ()))
283+
client_trace_configs.extend(trace_configs)
283284

284285
trace_config = create_trace_config(
285286
url_filter=url_filter,
@@ -288,9 +289,9 @@ def instrumented_init(wrapped, instance, args, kwargs):
288289
tracer_provider=tracer_provider,
289290
)
290291
trace_config._is_instrumented_by_opentelemetry = True
291-
trace_configs.append(trace_config)
292+
client_trace_configs.append(trace_config)
292293

293-
kwargs["trace_configs"] = trace_configs
294+
kwargs["trace_configs"] = client_trace_configs
294295
return wrapped(*args, **kwargs)
295296

296297
wrapt.wrap_function_wrapper(

Diff for: ‎instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,35 @@ def test_instrument(self):
387387
self.assertEqual(200, span.attributes[SpanAttributes.HTTP_STATUS_CODE])
388388

389389
def test_instrument_with_custom_trace_config(self):
390+
trace_config = aiohttp.TraceConfig()
391+
390392
AioHttpClientInstrumentor().uninstrument()
391-
AioHttpClientInstrumentor().instrument(
392-
trace_configs=[aiohttp_client.create_trace_config()]
393-
)
393+
AioHttpClientInstrumentor().instrument(trace_configs=[trace_config])
394394

395-
self.assert_spans(0)
395+
async def make_request(server: aiohttp.test_utils.TestServer):
396+
async with aiohttp.test_utils.TestClient(server) as client:
397+
trace_configs = client.session._trace_configs
398+
self.assertEqual(2, len(trace_configs))
399+
self.assertTrue(trace_config in trace_configs)
400+
async with client as session:
401+
await session.get(TestAioHttpClientInstrumentor.URL)
396402

397-
run_with_test_server(
398-
self.get_default_request(), self.URL, self.default_handler
399-
)
403+
run_with_test_server(make_request, self.URL, self.default_handler)
404+
self.assert_spans(1)
405+
406+
def test_every_request_by_new_session_creates_one_span(self):
407+
async def make_request(server: aiohttp.test_utils.TestServer):
408+
async with aiohttp.test_utils.TestClient(server) as client:
409+
async with client as session:
410+
await session.get(TestAioHttpClientInstrumentor.URL)
400411

401-
self.assert_spans(2)
412+
for request_no in range(3):
413+
self.memory_exporter.clear()
414+
with self.subTest(request_no=request_no):
415+
run_with_test_server(
416+
make_request, self.URL, self.default_handler
417+
)
418+
self.assert_spans(1)
402419

403420
def test_instrument_with_existing_trace_config(self):
404421
trace_config = aiohttp.TraceConfig()
@@ -446,7 +463,7 @@ async def uninstrument_request(server: aiohttp.test_utils.TestServer):
446463
run_with_test_server(
447464
self.get_default_request(), self.URL, self.default_handler
448465
)
449-
self.assert_spans(2)
466+
self.assert_spans(1)
450467

451468
def test_suppress_instrumentation(self):
452469
token = context.attach(

0 commit comments

Comments
 (0)
Please sign in to comment.