Skip to content

Commit

Permalink
Feat: instrument aiohttp with trace_configs argument (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
nemoshlag authored Jun 26, 2022
1 parent f7fd1e0 commit e267ebc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e267ebc

Please sign in to comment.