From 7718d88b198e4cb5baed28d95dba4b8b290eb28f Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 22 Sep 2020 10:30:39 -0700 Subject: [PATCH] Use is_recording flag in asgi, pyramid, aiohttp instrumentation (#1142) --- .../instrumentation/asgi/__init__.py | 33 ++++++++++--------- .../tests/test_asgi_middleware.py | 17 ++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py index 02aabfea95c1..71211907e083 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py @@ -156,15 +156,16 @@ async def __call__(self, scope, receive, send): propagators.extract(get_header_from_scope, scope) ) span_name, additional_attributes = self.span_details_callback(scope) - attributes = collect_request_attributes(scope) - attributes.update(additional_attributes) try: with self.tracer.start_as_current_span( - span_name + " asgi", - kind=trace.SpanKind.SERVER, - attributes=attributes, - ): + span_name + " asgi", kind=trace.SpanKind.SERVER, + ) as span: + if span.is_recording(): + attributes = collect_request_attributes(scope) + attributes.update(additional_attributes) + for key, value in attributes.items(): + span.set_attribute(key, value) @wraps(receive) async def wrapped_receive(): @@ -172,9 +173,10 @@ async def wrapped_receive(): span_name + " asgi." + scope["type"] + ".receive" ) as receive_span: message = await receive() - if message["type"] == "websocket.receive": - set_status_code(receive_span, 200) - receive_span.set_attribute("type", message["type"]) + if receive_span.is_recording(): + if message["type"] == "websocket.receive": + set_status_code(receive_span, 200) + receive_span.set_attribute("type", message["type"]) return message @wraps(send) @@ -182,12 +184,13 @@ async def wrapped_send(message): with self.tracer.start_as_current_span( span_name + " asgi." + scope["type"] + ".send" ) as send_span: - if message["type"] == "http.response.start": - status_code = message["status"] - set_status_code(send_span, status_code) - elif message["type"] == "websocket.send": - set_status_code(send_span, 200) - send_span.set_attribute("type", message["type"]) + if send_span.is_recording(): + if message["type"] == "http.response.start": + status_code = message["status"] + set_status_code(send_span, status_code) + elif message["type"] == "websocket.send": + set_status_code(send_span, 200) + send_span.set_attribute("type", message["type"]) await send(message) await self.app(scope, wrapped_receive, wrapped_send) diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py index f994e25966c5..cf8944ce6df4 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py @@ -164,6 +164,23 @@ def test_basic_asgi_call(self): outputs = self.get_all_output() self.validate_outputs(outputs) + def test_wsgi_not_recording(self): + mock_tracer = mock.Mock() + mock_span = mock.Mock() + mock_span.is_recording.return_value = False + mock_tracer.start_as_current_span.return_value = mock_span + mock_tracer.start_as_current_span.return_value.__enter__ = mock_span + mock_tracer.start_as_current_span.return_value.__exit__ = mock_span + with mock.patch("opentelemetry.trace.get_tracer") as tracer: + tracer.return_value = mock_tracer + app = otel_asgi.OpenTelemetryMiddleware(simple_asgi) + self.seed_app(app) + self.send_default_request() + 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_asgi_exc_info(self): """Test that exception information is emitted as expected.""" app = otel_asgi.OpenTelemetryMiddleware(error_asgi)