Use is_recording flag in asgi, pyramid, aiohttp instrumentation (#1142)

This commit is contained in:
Leighton Chen 2020-09-22 10:30:39 -07:00 committed by alrex
parent e568eeaa35
commit 847418f397
2 changed files with 35 additions and 15 deletions

View File

@ -156,15 +156,16 @@ class OpenTelemetryMiddleware:
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 @@ class OpenTelemetryMiddleware:
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 @@ class OpenTelemetryMiddleware:
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)

View File

@ -164,6 +164,23 @@ class TestAsgiApplication(AsgiTestBase):
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)