Skip to content

Commit

Permalink
Set status code on ASGI server span (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti authored May 25, 2021
1 parent f4a2b61 commit b6ed679
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

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

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

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

### Changed
- `opentelemetry-propagator-ot-trace` Use `TraceFlags` object in `extract`
([#472](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/472))
- Set the `traced_request_attrs` of FalconInstrumentor by an argument correctly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ async def wrapped_send(message):
if send_span.is_recording():
if message["type"] == "http.response.start":
status_code = message["status"]
set_status_code(span, status_code)
set_status_code(send_span, status_code)
elif message["type"] == "websocket.send":
set_status_code(span, 200)
set_status_code(send_span, 200)
send_span.set_attribute("type", message["type"])
await send(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
SpanAttributes.HTTP_URL: "http://127.0.0.1/",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.NET_PEER_PORT: 32767,
SpanAttributes.HTTP_STATUS_CODE: 200,
},
},
]
Expand Down Expand Up @@ -303,15 +304,57 @@ def test_websocket(self):
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 6)
expected = [
"/ asgi.websocket.receive",
"/ asgi.websocket.send",
"/ asgi.websocket.receive",
"/ asgi.websocket.send",
"/ asgi.websocket.receive",
"/ asgi",
{
"name": "/ asgi.websocket.receive",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {"type": "websocket.connect"},
},
{
"name": "/ asgi.websocket.send",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {"type": "websocket.accept"},
},
{
"name": "/ asgi.websocket.receive",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {
"type": "websocket.receive",
SpanAttributes.HTTP_STATUS_CODE: 200,
},
},
{
"name": "/ asgi.websocket.send",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {
"type": "websocket.send",
SpanAttributes.HTTP_STATUS_CODE: 200,
},
},
{
"name": "/ asgi.websocket.receive",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {"type": "websocket.disconnect"},
},
{
"name": "/ asgi",
"kind": trace_api.SpanKind.SERVER,
"attributes": {
SpanAttributes.HTTP_SCHEME: self.scope["scheme"],
SpanAttributes.NET_HOST_PORT: self.scope["server"][1],
SpanAttributes.HTTP_HOST: self.scope["server"][0],
SpanAttributes.HTTP_FLAVOR: self.scope["http_version"],
SpanAttributes.HTTP_TARGET: self.scope["path"],
SpanAttributes.HTTP_URL: f'{self.scope["scheme"]}://{self.scope["server"][0]}{self.scope["path"]}',
SpanAttributes.NET_PEER_IP: self.scope["client"][0],
SpanAttributes.NET_PEER_PORT: self.scope["client"][1],
SpanAttributes.HTTP_STATUS_CODE: 200,
},
},
]
actual = [span.name for span in span_list]
self.assertListEqual(actual, expected)
for span, expected in zip(span_list, expected):
self.assertEqual(span.name, expected["name"])
self.assertEqual(span.kind, expected["kind"])
self.assertDictEqual(dict(span.attributes), expected["attributes"])

def test_lifespan(self):
self.scope["type"] = "lifespan"
Expand Down

0 comments on commit b6ed679

Please sign in to comment.