Skip to content

Commit 5de29db

Browse files
committed
fix asgi instrumentation
1 parent 251b3c1 commit 5de29db

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,12 @@ def get_default_span_details(scope: dict) -> Tuple[str, dict]:
467467
Returns:
468468
a tuple of the span name, and any attributes to attach to the span.
469469
"""
470-
path = scope.get("path", "").strip()
471-
method = sanitize_method(scope.get("method", "").strip())
472-
if method == "_OTHER":
473-
method = "HTTP"
474-
if method and path: # http
475-
return f"{method} {path}", {}
476-
if path: # websocket
477-
return path, {}
478-
return method, {} # http with no path
479-
470+
if scope.get("type") == "http":
471+
method = sanitize_method(scope.get("method", "").strip())
472+
if method == "_OTHER":
473+
method = "HTTP"
474+
return method, {}
475+
return scope.get("type", ""), {}
480476

481477
def _collect_target_attribute(
482478
scope: typing.Dict[str, typing.Any]
@@ -797,7 +793,7 @@ def _get_otel_receive(self, server_span_name, scope, receive):
797793
@wraps(receive)
798794
async def otel_receive():
799795
with self.tracer.start_as_current_span(
800-
" ".join((server_span_name, scope["type"], "receive"))
796+
" ".join((server_span_name, "receive"))
801797
) as receive_span:
802798
message = await receive()
803799
if callable(self.client_request_hook):
@@ -831,7 +827,7 @@ def _get_otel_send(
831827
async def otel_send(message: dict[str, Any]):
832828
nonlocal expecting_trailers
833829
with self.tracer.start_as_current_span(
834-
" ".join((server_span_name, scope["type"], "send"))
830+
" ".join((server_span_name, "send"))
835831
) as send_span:
836832
if callable(self.client_response_hook):
837833
self.client_response_hook(send_span, scope, message)

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -394,20 +394,20 @@ def validate_outputs(
394394
span_list = self.memory_exporter.get_finished_spans()
395395
expected_old = [
396396
{
397-
"name": "GET http receive",
397+
"name": "GET receive",
398398
"kind": trace_api.SpanKind.INTERNAL,
399399
"attributes": {"asgi.event.type": "http.request"},
400400
},
401401
{
402-
"name": "GET http send",
402+
"name": "GET send",
403403
"kind": trace_api.SpanKind.INTERNAL,
404404
"attributes": {
405405
SpanAttributes.HTTP_STATUS_CODE: 200,
406406
"asgi.event.type": "http.response.start",
407407
},
408408
},
409409
{
410-
"name": "GET http send",
410+
"name": "GET send",
411411
"kind": trace_api.SpanKind.INTERNAL,
412412
"attributes": {"asgi.event.type": "http.response.body"},
413413
},
@@ -430,25 +430,25 @@ def validate_outputs(
430430
]
431431
expected_new = [
432432
{
433-
"name": "GET / http receive",
433+
"name": "GET receive",
434434
"kind": trace_api.SpanKind.INTERNAL,
435435
"attributes": {"asgi.event.type": "http.request"},
436436
},
437437
{
438-
"name": "GET / http send",
438+
"name": "GET send",
439439
"kind": trace_api.SpanKind.INTERNAL,
440440
"attributes": {
441441
HTTP_RESPONSE_STATUS_CODE: 200,
442442
"asgi.event.type": "http.response.start",
443443
},
444444
},
445445
{
446-
"name": "GET / http send",
446+
"name": "GET send",
447447
"kind": trace_api.SpanKind.INTERNAL,
448448
"attributes": {"asgi.event.type": "http.response.body"},
449449
},
450450
{
451-
"name": "GET /",
451+
"name": "GET",
452452
"kind": trace_api.SpanKind.SERVER,
453453
"attributes": {
454454
HTTP_REQUEST_METHOD: "GET",
@@ -466,12 +466,12 @@ def validate_outputs(
466466
]
467467
expected_both = [
468468
{
469-
"name": "GET / http receive",
469+
"name": "GET receive",
470470
"kind": trace_api.SpanKind.INTERNAL,
471471
"attributes": {"asgi.event.type": "http.request"},
472472
},
473473
{
474-
"name": "GET / http send",
474+
"name": "GET send",
475475
"kind": trace_api.SpanKind.INTERNAL,
476476
"attributes": {
477477
SpanAttributes.HTTP_STATUS_CODE: 200,
@@ -480,12 +480,12 @@ def validate_outputs(
480480
},
481481
},
482482
{
483-
"name": "GET / http send",
483+
"name": "GET send",
484484
"kind": trace_api.SpanKind.INTERNAL,
485485
"attributes": {"asgi.event.type": "http.response.body"},
486486
},
487487
{
488-
"name": "GET /",
488+
"name": "GET",
489489
"kind": trace_api.SpanKind.SERVER,
490490
"attributes": {
491491
HTTP_REQUEST_METHOD: "GET",
@@ -596,7 +596,7 @@ def test_long_response(self):
596596

597597
def add_more_body_spans(expected: list):
598598
more_body_span = {
599-
"name": "GET http send",
599+
"name": "GET send",
600600
"kind": trace_api.SpanKind.INTERNAL,
601601
"attributes": {"asgi.event.type": "http.response.body"},
602602
}
@@ -634,12 +634,12 @@ def test_trailers(self):
634634

635635
def add_body_and_trailer_span(expected: list):
636636
body_span = {
637-
"name": "GET http send",
637+
"name": "GET send",
638638
"kind": trace_api.SpanKind.INTERNAL,
639639
"attributes": {"asgi.event.type": "http.response.body"},
640640
}
641641
trailer_span = {
642-
"name": "GET http send",
642+
"name": "GET send",
643643
"kind": trace_api.SpanKind.INTERNAL,
644644
"attributes": {"asgi.event.type": "http.response.trailers"},
645645
}
@@ -943,38 +943,38 @@ def test_websocket(self):
943943
self.assertEqual(len(span_list), 6)
944944
expected = [
945945
{
946-
"name": "GET / websocket receive",
946+
"name": "websocket receive",
947947
"kind": trace_api.SpanKind.INTERNAL,
948948
"attributes": {"asgi.event.type": "websocket.connect"},
949949
},
950950
{
951-
"name": "GET / websocket send",
951+
"name": "websocket send",
952952
"kind": trace_api.SpanKind.INTERNAL,
953953
"attributes": {"asgi.event.type": "websocket.accept"},
954954
},
955955
{
956-
"name": "GET / websocket receive",
956+
"name": "websocket receive",
957957
"kind": trace_api.SpanKind.INTERNAL,
958958
"attributes": {
959959
"asgi.event.type": "websocket.receive",
960960
SpanAttributes.HTTP_STATUS_CODE: 200,
961961
},
962962
},
963963
{
964-
"name": "GET / websocket send",
964+
"name": "websocket send",
965965
"kind": trace_api.SpanKind.INTERNAL,
966966
"attributes": {
967967
"asgi.event.type": "websocket.send",
968968
SpanAttributes.HTTP_STATUS_CODE: 200,
969969
},
970970
},
971971
{
972-
"name": "GET / websocket receive",
972+
"name": "websocket receive",
973973
"kind": trace_api.SpanKind.INTERNAL,
974974
"attributes": {"asgi.event.type": "websocket.disconnect"},
975975
},
976976
{
977-
"name": "GET /",
977+
"name": "websocket",
978978
"kind": trace_api.SpanKind.SERVER,
979979
"attributes": {
980980
SpanAttributes.HTTP_SCHEME: self.scope["scheme"],
@@ -1017,38 +1017,38 @@ def test_websocket_new_semconv(self):
10171017
self.assertEqual(len(span_list), 6)
10181018
expected = [
10191019
{
1020-
"name": "GET / websocket receive",
1020+
"name": "websocket receive",
10211021
"kind": trace_api.SpanKind.INTERNAL,
10221022
"attributes": {"asgi.event.type": "websocket.connect"},
10231023
},
10241024
{
1025-
"name": "GET / websocket send",
1025+
"name": "websocket send",
10261026
"kind": trace_api.SpanKind.INTERNAL,
10271027
"attributes": {"asgi.event.type": "websocket.accept"},
10281028
},
10291029
{
1030-
"name": "GET / websocket receive",
1030+
"name": "websocket receive",
10311031
"kind": trace_api.SpanKind.INTERNAL,
10321032
"attributes": {
10331033
"asgi.event.type": "websocket.receive",
10341034
HTTP_RESPONSE_STATUS_CODE: 200,
10351035
},
10361036
},
10371037
{
1038-
"name": "GET / websocket send",
1038+
"name": "websocket send",
10391039
"kind": trace_api.SpanKind.INTERNAL,
10401040
"attributes": {
10411041
"asgi.event.type": "websocket.send",
10421042
HTTP_RESPONSE_STATUS_CODE: 200,
10431043
},
10441044
},
10451045
{
1046-
"name": "GET / websocket receive",
1046+
"name": "websocket receive",
10471047
"kind": trace_api.SpanKind.INTERNAL,
10481048
"attributes": {"asgi.event.type": "websocket.disconnect"},
10491049
},
10501050
{
1051-
"name": "GET /",
1051+
"name": "websocket",
10521052
"kind": trace_api.SpanKind.SERVER,
10531053
"attributes": {
10541054
URL_SCHEME: self.scope["scheme"],
@@ -1091,17 +1091,17 @@ def test_websocket_both_semconv(self):
10911091
self.assertEqual(len(span_list), 6)
10921092
expected = [
10931093
{
1094-
"name": "GET / websocket receive",
1094+
"name": "websocket receive",
10951095
"kind": trace_api.SpanKind.INTERNAL,
10961096
"attributes": {"asgi.event.type": "websocket.connect"},
10971097
},
10981098
{
1099-
"name": "GET / websocket send",
1099+
"name": "websocket send",
11001100
"kind": trace_api.SpanKind.INTERNAL,
11011101
"attributes": {"asgi.event.type": "websocket.accept"},
11021102
},
11031103
{
1104-
"name": "GET / websocket receive",
1104+
"name": "websocket receive",
11051105
"kind": trace_api.SpanKind.INTERNAL,
11061106
"attributes": {
11071107
"asgi.event.type": "websocket.receive",
@@ -1110,7 +1110,7 @@ def test_websocket_both_semconv(self):
11101110
},
11111111
},
11121112
{
1113-
"name": "GET / websocket send",
1113+
"name": "websocket send",
11141114
"kind": trace_api.SpanKind.INTERNAL,
11151115
"attributes": {
11161116
"asgi.event.type": "websocket.send",
@@ -1119,12 +1119,12 @@ def test_websocket_both_semconv(self):
11191119
},
11201120
},
11211121
{
1122-
"name": "GET / websocket receive",
1122+
"name": "websocket receive",
11231123
"kind": trace_api.SpanKind.INTERNAL,
11241124
"attributes": {"asgi.event.type": "websocket.disconnect"},
11251125
},
11261126
{
1127-
"name": "GET /",
1127+
"name": "websocket",
11281128
"kind": trace_api.SpanKind.SERVER,
11291129
"attributes": {
11301130
SpanAttributes.HTTP_SCHEME: self.scope["scheme"],
@@ -1217,9 +1217,9 @@ def update_expected_hook_results(expected):
12171217
for entry in expected:
12181218
if entry["kind"] == trace_api.SpanKind.SERVER:
12191219
entry["name"] = "name from server hook"
1220-
elif entry["name"] == "GET http receive":
1220+
elif entry["name"] == "GET receive":
12211221
entry["name"] = "name from client request hook"
1222-
elif entry["name"] == "GET http send":
1222+
elif entry["name"] == "GET send":
12231223
entry["attributes"].update({"attr-from-hook": "value"})
12241224
return expected
12251225

0 commit comments

Comments
 (0)