Skip to content

Commit

Permalink
Use is_recording flag in asgi, pyramid, aiohttp instrumentation (#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Sep 22, 2020
1 parent ee1805d commit 2b37136
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,19 @@ async def on_request_start(
request_span_name = str(trace_config_ctx.span_name)

trace_config_ctx.span = trace_config_ctx.tracer.start_span(
request_span_name,
kind=SpanKind.CLIENT,
attributes={
request_span_name, kind=SpanKind.CLIENT,
)

if trace_config_ctx.span.is_recording():
attributes = {
"component": "http",
"http.method": http_method,
"http.url": trace_config_ctx.url_filter(params.url)
if callable(trace_config_ctx.url_filter)
else 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)
Expand All @@ -155,37 +158,41 @@ async def on_request_end(
trace_config_ctx: types.SimpleNamespace,
params: aiohttp.TraceRequestEndParams,
):
trace_config_ctx.span.set_status(
Status(http_status_to_canonical_code(int(params.response.status)))
)
trace_config_ctx.span.set_attribute(
"http.status_code", params.response.status
)
trace_config_ctx.span.set_attribute(
"http.status_text", params.response.reason
)
if trace_config_ctx.span.is_recording():
trace_config_ctx.span.set_status(
Status(
http_status_to_canonical_code(int(params.response.status))
)
)
trace_config_ctx.span.set_attribute(
"http.status_code", params.response.status
)
trace_config_ctx.span.set_attribute(
"http.status_text", params.response.reason
)
_end_trace(trace_config_ctx)

async def on_request_exception(
unused_session: aiohttp.ClientSession,
trace_config_ctx: types.SimpleNamespace,
params: aiohttp.TraceRequestExceptionParams,
):
if isinstance(
params.exception,
(aiohttp.ServerTimeoutError, aiohttp.TooManyRedirects),
):
status = StatusCanonicalCode.DEADLINE_EXCEEDED
# Assume any getaddrinfo error is a DNS failure.
elif isinstance(
params.exception, aiohttp.ClientConnectorError
) and isinstance(params.exception.os_error, socket.gaierror):
# DNS resolution failed
status = StatusCanonicalCode.UNKNOWN
else:
status = StatusCanonicalCode.UNAVAILABLE

trace_config_ctx.span.set_status(Status(status))
if trace_config_ctx.span.is_recording():
if isinstance(
params.exception,
(aiohttp.ServerTimeoutError, aiohttp.TooManyRedirects),
):
status = StatusCanonicalCode.DEADLINE_EXCEEDED
# Assume any getaddrinfo error is a DNS failure.
elif isinstance(
params.exception, aiohttp.ClientConnectorError
) and isinstance(params.exception.os_error, socket.gaierror):
# DNS resolution failed
status = StatusCanonicalCode.UNKNOWN
else:
status = StatusCanonicalCode.UNAVAILABLE

trace_config_ctx.span.set_status(Status(status))
_end_trace(trace_config_ctx)

def _trace_config_ctx_factory(**kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import typing
import urllib.parse
from http import HTTPStatus
from unittest import mock

import aiohttp
import aiohttp.test_utils
Expand Down Expand Up @@ -135,6 +136,22 @@ def test_status_codes(self):

self.memory_exporter.clear()

def test_not_recording(self):
mock_tracer = mock.Mock()
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
with mock.patch("opentelemetry.trace.get_tracer"):
# pylint: disable=W0612
host, port = self._http_request(
trace_config=opentelemetry.instrumentation.aiohttp_client.create_trace_config(),
url="/test-path?query=param#foobar",
)
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

def test_span_name_option(self):
for span_name, method, path, expected in (
("static", "POST", "/static-span-name", "static"),
Expand Down

0 comments on commit 2b37136

Please sign in to comment.