diff --git a/CHANGELOG.md b/CHANGELOG.md index 629dc47018..ba9e92478a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) @@ -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. 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 b77e4db24b..c02ae50287 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py @@ -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) diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py index 10f93ccd6e..0a3b451b9c 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py @@ -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, }, }, ] @@ -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"