Skip to content

Commit e67218a

Browse files
committed
Update ASGI implementation
1 parent d01c96f commit e67218a

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,12 @@ def get_default_span_details(scope: dict) -> Tuple[str, dict]:
421421
Returns:
422422
a tuple of the span name, and any attributes to attach to the span.
423423
"""
424+
if scope.get("type") == "websocket":
425+
return f"{scope.get('path', '').strip()}", {}
424426
span_name = (
425-
scope.get("path", "").strip()
426-
or f"HTTP {scope.get('method', '').strip()}"
427+
f"{scope.get('method', '').strip()} {scope.get('path', '').strip()}"
428+
if scope.get("path", "").strip()
429+
else f"{scope.get('method', '').strip()}"
427430
)
428431

429432
return span_name, {}

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,25 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
142142
self.assertEqual(len(span_list), 4)
143143
expected = [
144144
{
145-
"name": "/ http receive",
145+
"name": "GET / http receive",
146146
"kind": trace_api.SpanKind.INTERNAL,
147147
"attributes": {"type": "http.request"},
148148
},
149149
{
150-
"name": "/ http send",
150+
"name": "GET / http send",
151151
"kind": trace_api.SpanKind.INTERNAL,
152152
"attributes": {
153153
SpanAttributes.HTTP_STATUS_CODE: 200,
154154
"type": "http.response.start",
155155
},
156156
},
157157
{
158-
"name": "/ http send",
158+
"name": "GET / http send",
159159
"kind": trace_api.SpanKind.INTERNAL,
160160
"attributes": {"type": "http.response.body"},
161161
},
162162
{
163-
"name": "/",
163+
"name": "GET /",
164164
"kind": trace_api.SpanKind.SERVER,
165165
"attributes": {
166166
SpanAttributes.HTTP_METHOD: "GET",
@@ -231,7 +231,7 @@ def update_expected_span_name(expected):
231231
entry["name"] = span_name
232232
else:
233233
entry["name"] = " ".join(
234-
[span_name] + entry["name"].split(" ")[1:]
234+
[span_name] + entry["name"].split(" ")[2:]
235235
)
236236
return expected
237237

@@ -284,6 +284,7 @@ def update_expected_server(expected):
284284
SpanAttributes.HTTP_URL: "http://0.0.0.0/",
285285
}
286286
)
287+
# TODO:self possibly update span name to just method
287288
return expected
288289

289290
self.scope["server"] = None
@@ -493,9 +494,9 @@ def update_expected_hook_results(expected):
493494
for entry in expected:
494495
if entry["kind"] == trace_api.SpanKind.SERVER:
495496
entry["name"] = "name from server hook"
496-
elif entry["name"] == "/ http receive":
497+
elif entry["name"] == "GET / http receive":
497498
entry["name"] = "name from client request hook"
498-
elif entry["name"] == "/ http send":
499+
elif entry["name"] == "GET / http send":
499500
entry["attributes"].update({"attr-from-hook": "value"})
500501
return expected
501502

opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _start_internal_or_server_span(
9595
9696
Args:
9797
tracer : tracer in use by given instrumentation library
98-
name (string): name of the span
98+
span_name (string): name of the span
9999
start_time : start time of the span
100100
context_carrier : object which contains values that are
101101
used to construct a Context. This object

0 commit comments

Comments
 (0)