mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-02 02:52:18 +08:00
Use is_recording flag in asgi, pyramid, aiohttp instrumentation (#1142)
This commit is contained in:
@ -133,16 +133,19 @@ def create_trace_config(
|
|||||||
request_span_name = str(trace_config_ctx.span_name)
|
request_span_name = str(trace_config_ctx.span_name)
|
||||||
|
|
||||||
trace_config_ctx.span = trace_config_ctx.tracer.start_span(
|
trace_config_ctx.span = trace_config_ctx.tracer.start_span(
|
||||||
request_span_name,
|
request_span_name, kind=SpanKind.CLIENT,
|
||||||
kind=SpanKind.CLIENT,
|
)
|
||||||
attributes={
|
|
||||||
|
if trace_config_ctx.span.is_recording():
|
||||||
|
attributes = {
|
||||||
"component": "http",
|
"component": "http",
|
||||||
"http.method": http_method,
|
"http.method": http_method,
|
||||||
"http.url": trace_config_ctx.url_filter(params.url)
|
"http.url": trace_config_ctx.url_filter(params.url)
|
||||||
if callable(trace_config_ctx.url_filter)
|
if callable(trace_config_ctx.url_filter)
|
||||||
else str(params.url),
|
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_config_ctx.token = context_api.attach(
|
||||||
trace.set_span_in_context(trace_config_ctx.span)
|
trace.set_span_in_context(trace_config_ctx.span)
|
||||||
@ -155,8 +158,11 @@ def create_trace_config(
|
|||||||
trace_config_ctx: types.SimpleNamespace,
|
trace_config_ctx: types.SimpleNamespace,
|
||||||
params: aiohttp.TraceRequestEndParams,
|
params: aiohttp.TraceRequestEndParams,
|
||||||
):
|
):
|
||||||
|
if trace_config_ctx.span.is_recording():
|
||||||
trace_config_ctx.span.set_status(
|
trace_config_ctx.span.set_status(
|
||||||
Status(http_status_to_canonical_code(int(params.response.status)))
|
Status(
|
||||||
|
http_status_to_canonical_code(int(params.response.status))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
trace_config_ctx.span.set_attribute(
|
trace_config_ctx.span.set_attribute(
|
||||||
"http.status_code", params.response.status
|
"http.status_code", params.response.status
|
||||||
@ -171,6 +177,7 @@ def create_trace_config(
|
|||||||
trace_config_ctx: types.SimpleNamespace,
|
trace_config_ctx: types.SimpleNamespace,
|
||||||
params: aiohttp.TraceRequestExceptionParams,
|
params: aiohttp.TraceRequestExceptionParams,
|
||||||
):
|
):
|
||||||
|
if trace_config_ctx.span.is_recording():
|
||||||
if isinstance(
|
if isinstance(
|
||||||
params.exception,
|
params.exception,
|
||||||
(aiohttp.ServerTimeoutError, aiohttp.TooManyRedirects),
|
(aiohttp.ServerTimeoutError, aiohttp.TooManyRedirects),
|
||||||
|
@ -17,6 +17,7 @@ import contextlib
|
|||||||
import typing
|
import typing
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import aiohttp.test_utils
|
import aiohttp.test_utils
|
||||||
@ -135,6 +136,22 @@ class TestAioHttpIntegration(TestBase):
|
|||||||
|
|
||||||
self.memory_exporter.clear()
|
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):
|
def test_span_name_option(self):
|
||||||
for span_name, method, path, expected in (
|
for span_name, method, path, expected in (
|
||||||
("static", "POST", "/static-span-name", "static"),
|
("static", "POST", "/static-span-name", "static"),
|
||||||
|
Reference in New Issue
Block a user