Skip to content

Commit b6ed679

Browse files
authored
Set status code on ASGI server span (#478)
1 parent f4a2b61 commit b6ed679

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.2.0-0.21b0...HEAD)
88

9+
### Changed
10+
- `opentelemetry-instrumentation-asgi` Set the response status code on the server span
11+
([#478](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/478))
912
- Fixed cases where description was used with non-error status code when creating Status objects.
1013
([#504](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/504))
1114

@@ -15,8 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1518
([#458](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/458))
1619

1720
## [0.21b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11
18-
### Changed
1921

22+
### Changed
2023
- `opentelemetry-propagator-ot-trace` Use `TraceFlags` object in `extract`
2124
([#472](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/472))
2225
- Set the `traced_request_attrs` of FalconInstrumentor by an argument correctly.

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ async def wrapped_send(message):
233233
if send_span.is_recording():
234234
if message["type"] == "http.response.start":
235235
status_code = message["status"]
236+
set_status_code(span, status_code)
236237
set_status_code(send_span, status_code)
237238
elif message["type"] == "websocket.send":
239+
set_status_code(span, 200)
238240
set_status_code(send_span, 200)
239241
send_span.set_attribute("type", message["type"])
240242
await send(message)

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

+51-8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
146146
SpanAttributes.HTTP_URL: "http://127.0.0.1/",
147147
SpanAttributes.NET_PEER_IP: "127.0.0.1",
148148
SpanAttributes.NET_PEER_PORT: 32767,
149+
SpanAttributes.HTTP_STATUS_CODE: 200,
149150
},
150151
},
151152
]
@@ -303,15 +304,57 @@ def test_websocket(self):
303304
span_list = self.memory_exporter.get_finished_spans()
304305
self.assertEqual(len(span_list), 6)
305306
expected = [
306-
"/ asgi.websocket.receive",
307-
"/ asgi.websocket.send",
308-
"/ asgi.websocket.receive",
309-
"/ asgi.websocket.send",
310-
"/ asgi.websocket.receive",
311-
"/ asgi",
307+
{
308+
"name": "/ asgi.websocket.receive",
309+
"kind": trace_api.SpanKind.INTERNAL,
310+
"attributes": {"type": "websocket.connect"},
311+
},
312+
{
313+
"name": "/ asgi.websocket.send",
314+
"kind": trace_api.SpanKind.INTERNAL,
315+
"attributes": {"type": "websocket.accept"},
316+
},
317+
{
318+
"name": "/ asgi.websocket.receive",
319+
"kind": trace_api.SpanKind.INTERNAL,
320+
"attributes": {
321+
"type": "websocket.receive",
322+
SpanAttributes.HTTP_STATUS_CODE: 200,
323+
},
324+
},
325+
{
326+
"name": "/ asgi.websocket.send",
327+
"kind": trace_api.SpanKind.INTERNAL,
328+
"attributes": {
329+
"type": "websocket.send",
330+
SpanAttributes.HTTP_STATUS_CODE: 200,
331+
},
332+
},
333+
{
334+
"name": "/ asgi.websocket.receive",
335+
"kind": trace_api.SpanKind.INTERNAL,
336+
"attributes": {"type": "websocket.disconnect"},
337+
},
338+
{
339+
"name": "/ asgi",
340+
"kind": trace_api.SpanKind.SERVER,
341+
"attributes": {
342+
SpanAttributes.HTTP_SCHEME: self.scope["scheme"],
343+
SpanAttributes.NET_HOST_PORT: self.scope["server"][1],
344+
SpanAttributes.HTTP_HOST: self.scope["server"][0],
345+
SpanAttributes.HTTP_FLAVOR: self.scope["http_version"],
346+
SpanAttributes.HTTP_TARGET: self.scope["path"],
347+
SpanAttributes.HTTP_URL: f'{self.scope["scheme"]}://{self.scope["server"][0]}{self.scope["path"]}',
348+
SpanAttributes.NET_PEER_IP: self.scope["client"][0],
349+
SpanAttributes.NET_PEER_PORT: self.scope["client"][1],
350+
SpanAttributes.HTTP_STATUS_CODE: 200,
351+
},
352+
},
312353
]
313-
actual = [span.name for span in span_list]
314-
self.assertListEqual(actual, expected)
354+
for span, expected in zip(span_list, expected):
355+
self.assertEqual(span.name, expected["name"])
356+
self.assertEqual(span.kind, expected["kind"])
357+
self.assertDictEqual(dict(span.attributes), expected["attributes"])
315358

316359
def test_lifespan(self):
317360
self.scope["type"] = "lifespan"

0 commit comments

Comments
 (0)