Repository: open-telemetry/opentelemetry-python-contrib

Issue:
ASGI instrumentation creates a 'type' attribute
See https://github.com/open-telemetry/opentelemetry-python-contrib/blob/7166de673fa9dd7abc178bec3103d4adebb05e0d/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py#L681

This results in an attribute like `{'type': 'http.request.body'}`, which seems wrong. `type` is a _super_ generic key and I don't think leaking the details of the web framework spec (`http.request.body` is an ASGI specific string) is very helpful to users.

Can we get rid of this?


Discussion:
Any answer on this? I still see this apparently undocument `type` attribute everywhere and would love to understand why it's being included.
@open-telemetry/opentelemetry-python-contrib-approvers I think we should not use any generic word like "type" in our instrumentations.

Thanks @bogdandrutu, PR submitted #2145.

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py
@@ -672,7 +672,9 @@ async def otel_receive():
                 if receive_span.is_recording():
                     if message["type"] == "websocket.receive":
                         set_status_code(receive_span, 200)
-                    receive_span.set_attribute("type", message["type"])
+                    receive_span.set_attribute(
+                        "asgi.event.type", message["type"]
+                    )
             return message
 
         return otel_receive
@@ -703,7 +705,7 @@ async def otel_send(message: dict[str, Any]):
                     elif message["type"] == "websocket.send":
                         set_status_code(server_span, 200)
                         set_status_code(send_span, 200)
-                    send_span.set_attribute("type", message["type"])
+                    send_span.set_attribute("asgi.event.type", message["type"])
                     if (
                         server_span.is_recording()
                         and server_span.kind == trace.SpanKind.SERVER


Test patch:
diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py
--- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py
+++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py
@@ -268,20 +268,20 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
             {
                 "name": "GET / http receive",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "http.request"},
+                "attributes": {"asgi.event.type": "http.request"},
             },
             {
                 "name": "GET / http send",
                 "kind": trace_api.SpanKind.INTERNAL,
                 "attributes": {
                     SpanAttributes.HTTP_STATUS_CODE: 200,
-                    "type": "http.response.start",
+                    "asgi.event.type": "http.response.start",
                 },
             },
             {
                 "name": "GET / http send",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "http.response.body"},
+                "attributes": {"asgi.event.type": "http.response.body"},
             },
             {
                 "name": "GET /",
@@ -358,7 +358,7 @@ def add_more_body_spans(expected: list):
             more_body_span = {
                 "name": "GET / http send",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "http.response.body"},
+                "attributes": {"asgi.event.type": "http.response.body"},
             }
             extra_spans = [more_body_span] * 3
             expected[2:2] = extra_spans
@@ -396,12 +396,12 @@ def add_body_and_trailer_span(expected: list):
             body_span = {
                 "name": "GET / http send",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "http.response.body"},
+                "attributes": {"asgi.event.type": "http.response.body"},
             }
             trailer_span = {
                 "name": "GET / http send",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "http.response.trailers"},
+                "attributes": {"asgi.event.type": "http.response.trailers"},
             }
             expected[2:2] = [body_span]
             expected[4:4] = [trailer_span] * 2
@@ -582,18 +582,18 @@ def test_websocket(self):
             {
                 "name": "/ websocket receive",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "websocket.connect"},
+                "attributes": {"asgi.event.type": "websocket.connect"},
             },
             {
                 "name": "/ websocket send",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "websocket.accept"},
+                "attributes": {"asgi.event.type": "websocket.accept"},
             },
             {
                 "name": "/ websocket receive",
                 "kind": trace_api.SpanKind.INTERNAL,
                 "attributes": {
-                    "type": "websocket.receive",
+                    "asgi.event.type": "websocket.receive",
                     SpanAttributes.HTTP_STATUS_CODE: 200,
                 },
             },
@@ -601,14 +601,14 @@ def test_websocket(self):
                 "name": "/ websocket send",
                 "kind": trace_api.SpanKind.INTERNAL,
                 "attributes": {
-                    "type": "websocket.send",
+                    "asgi.event.type": "websocket.send",
                     SpanAttributes.HTTP_STATUS_CODE: 200,
                 },
             },
             {
                 "name": "/ websocket receive",
                 "kind": trace_api.SpanKind.INTERNAL,
-                "attributes": {"type": "websocket.disconnect"},
+                "attributes": {"asgi.event.type": "websocket.disconnect"},
             },
             {
                 "name": "/",