Skip to content

Commit e267ebc

Browse files
authored
Feat: instrument aiohttp with trace_configs argument (#1079)
1 parent f7fd1e0 commit e267ebc

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424

2525

2626
### Added
27+
- `opentelemetry-instrumentation-aiohttp-client` Add support for optional custom trace_configs argument.
28+
([1079](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1079))
2729
- `opentelemetry-instrumentation-sqlalchemy` add support to instrument multiple engines
2830
([#1132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1132))
2931
- `opentelemetry-instrumentation-logging` add log hook support

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,24 @@ 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,
265266
):
266267
"""Enables tracing of all ClientSessions
267268
268269
When a ClientSession gets created a TraceConfig is automatically added to
269270
the session's trace_configs.
270271
"""
272+
273+
if trace_configs is None:
274+
trace_configs = []
275+
271276
# pylint:disable=unused-argument
272277
def instrumented_init(wrapped, instance, args, kwargs):
273278
if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
274279
return wrapped(*args, **kwargs)
275280

276-
trace_configs = list(kwargs.get("trace_configs") or ())
281+
if kwargs.get("trace_configs"):
282+
trace_configs.extend(kwargs.get("trace_configs"))
277283

278284
trace_config = create_trace_config(
279285
url_filter=url_filter,
@@ -328,12 +334,15 @@ def _instrument(self, **kwargs):
328334
such as API keys or user personal information.
329335
``request_hook``: An optional callback that is invoked right after a span is created.
330336
``response_hook``: An optional callback which is invoked right before the span is finished processing a response.
337+
``trace_configs``: An optional list of aiohttp.TraceConfig items, allowing customize enrichment of spans
338+
based on aiohttp events (see specification: https://docs.aiohttp.org/en/stable/tracing_reference.html)
331339
"""
332340
_instrument(
333341
tracer_provider=kwargs.get("tracer_provider"),
334342
url_filter=kwargs.get("url_filter"),
335343
request_hook=kwargs.get("request_hook"),
336344
response_hook=kwargs.get("response_hook"),
345+
trace_configs=kwargs.get("trace_configs"),
337346
)
338347

339348
def _uninstrument(self, **kwargs):

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,20 @@ def test_instrument(self):
386386
)
387387
self.assertEqual(200, span.attributes[SpanAttributes.HTTP_STATUS_CODE])
388388

389+
def test_instrument_with_custom_trace_config(self):
390+
AioHttpClientInstrumentor().uninstrument()
391+
AioHttpClientInstrumentor().instrument(
392+
trace_configs=[aiohttp_client.create_trace_config()]
393+
)
394+
395+
self.assert_spans(0)
396+
397+
run_with_test_server(
398+
self.get_default_request(), self.URL, self.default_handler
399+
)
400+
401+
self.assert_spans(2)
402+
389403
def test_instrument_with_existing_trace_config(self):
390404
trace_config = aiohttp.TraceConfig()
391405

@@ -432,7 +446,7 @@ async def uninstrument_request(server: aiohttp.test_utils.TestServer):
432446
run_with_test_server(
433447
self.get_default_request(), self.URL, self.default_handler
434448
)
435-
self.assert_spans(1)
449+
self.assert_spans(2)
436450

437451
def test_suppress_instrumentation(self):
438452
token = context.attach(

0 commit comments

Comments
 (0)