Skip to content

Commit 3bb28ab

Browse files
authored
Fix ASGI instrumentation default span name (open-telemetry#418)
1 parent daa7238 commit 3bb28ab

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `opentelemetry-instrumentation-tornado` Fixed cases where description was used with non-
1313
error status code when creating Status objects.
1414
([#504](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/504))
15+
- `opentelemetry-instrumentation-asgi` Fix instrumentation default span name.
16+
([#418](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/418))
1517

1618
### Added
1719
- `opentelemetry-instrumentation-botocore` now supports
18-
context propagation for lambda invoke via Payload embedded headers.
20+
context propagation for lambda invoke via Payload embedded headers.
1921
([#458](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/458))
20-
22+
2123
## [0.21b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11
2224

2325
### Changed
@@ -84,7 +86,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8486
([#436](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/436))
8587
- `opentelemetry-instrumentation-grpc` Keep client interceptor in sync with grpc client interceptors.
8688
([#442](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/442))
87-
89+
8890
### Removed
8991
- Remove `http.status_text` from span attributes
9092
([#406](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/406))

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,13 @@ def get_default_span_details(scope: dict) -> Tuple[str, dict]:
146146
scope: the asgi scope dictionary
147147
148148
Returns:
149-
a tuple of the span, and any attributes to attach to the
150-
span.
149+
a tuple of the span name, and any attributes to attach to the span.
151150
"""
152-
method_or_path = scope.get("method") or scope.get("path")
151+
span_name = scope.get("path", "").strip() or "HTTP {}".format(
152+
scope.get("method", "").strip()
153+
)
153154

154-
return method_or_path, {}
155+
return span_name, {}
155156

156157

157158
class OpenTelemetryMiddleware:
@@ -204,7 +205,7 @@ async def __call__(self, scope, receive, send):
204205

205206
try:
206207
with self.tracer.start_as_current_span(
207-
span_name + " asgi", kind=trace.SpanKind.SERVER,
208+
span_name, kind=trace.SpanKind.SERVER,
208209
) as span:
209210
if span.is_recording():
210211
attributes = collect_request_attributes(scope)
@@ -215,7 +216,7 @@ async def __call__(self, scope, receive, send):
215216
@wraps(receive)
216217
async def wrapped_receive():
217218
with self.tracer.start_as_current_span(
218-
span_name + " asgi." + scope["type"] + ".receive"
219+
" ".join((span_name, scope["type"], "receive"))
219220
) as receive_span:
220221
message = await receive()
221222
if receive_span.is_recording():
@@ -227,7 +228,7 @@ async def wrapped_receive():
227228
@wraps(send)
228229
async def wrapped_send(message):
229230
with self.tracer.start_as_current_span(
230-
span_name + " asgi." + scope["type"] + ".send"
231+
" ".join((span_name, scope["type"], "send"))
231232
) as send_span:
232233
if send_span.is_recording():
233234
if message["type"] == "http.response.start":

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,25 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
116116
self.assertEqual(len(span_list), 4)
117117
expected = [
118118
{
119-
"name": "GET asgi.http.receive",
119+
"name": "/ http receive",
120120
"kind": trace_api.SpanKind.INTERNAL,
121121
"attributes": {"type": "http.request"},
122122
},
123123
{
124-
"name": "GET asgi.http.send",
124+
"name": "/ http send",
125125
"kind": trace_api.SpanKind.INTERNAL,
126126
"attributes": {
127127
SpanAttributes.HTTP_STATUS_CODE: 200,
128128
"type": "http.response.start",
129129
},
130130
},
131131
{
132-
"name": "GET asgi.http.send",
132+
"name": "/ http send",
133133
"kind": trace_api.SpanKind.INTERNAL,
134134
"attributes": {"type": "http.response.body"},
135135
},
136136
{
137-
"name": "GET asgi",
137+
"name": "/",
138138
"kind": trace_api.SpanKind.SERVER,
139139
"attributes": {
140140
SpanAttributes.HTTP_METHOD: "GET",
@@ -201,9 +201,12 @@ def get_predefined_span_details(_):
201201

202202
def update_expected_span_name(expected):
203203
for entry in expected:
204-
entry["name"] = " ".join(
205-
[span_name] + entry["name"].split(" ")[-1:]
206-
)
204+
if entry["kind"] == trace_api.SpanKind.SERVER:
205+
entry["name"] = span_name
206+
else:
207+
entry["name"] = " ".join(
208+
[span_name] + entry["name"].split(" ")[1:]
209+
)
207210
return expected
208211

209212
app = otel_asgi.OpenTelemetryMiddleware(
@@ -305,38 +308,38 @@ def test_websocket(self):
305308
self.assertEqual(len(span_list), 6)
306309
expected = [
307310
{
308-
"name": "/ asgi.websocket.receive",
311+
"name": "/ websocket receive",
309312
"kind": trace_api.SpanKind.INTERNAL,
310313
"attributes": {"type": "websocket.connect"},
311314
},
312315
{
313-
"name": "/ asgi.websocket.send",
316+
"name": "/ websocket send",
314317
"kind": trace_api.SpanKind.INTERNAL,
315318
"attributes": {"type": "websocket.accept"},
316319
},
317320
{
318-
"name": "/ asgi.websocket.receive",
321+
"name": "/ websocket receive",
319322
"kind": trace_api.SpanKind.INTERNAL,
320323
"attributes": {
321324
"type": "websocket.receive",
322325
SpanAttributes.HTTP_STATUS_CODE: 200,
323326
},
324327
},
325328
{
326-
"name": "/ asgi.websocket.send",
329+
"name": "/ websocket send",
327330
"kind": trace_api.SpanKind.INTERNAL,
328331
"attributes": {
329332
"type": "websocket.send",
330333
SpanAttributes.HTTP_STATUS_CODE: 200,
331334
},
332335
},
333336
{
334-
"name": "/ asgi.websocket.receive",
337+
"name": "/ websocket receive",
335338
"kind": trace_api.SpanKind.INTERNAL,
336339
"attributes": {"type": "websocket.disconnect"},
337340
},
338341
{
339-
"name": "/ asgi",
342+
"name": "/",
340343
"kind": trace_api.SpanKind.SERVER,
341344
"attributes": {
342345
SpanAttributes.HTTP_SCHEME: self.scope["scheme"],

0 commit comments

Comments
 (0)