Skip to content

Commit e94660a

Browse files
committed
Refactor ASGI and WSGI default span name handlers
1 parent 22422e8 commit e94660a

File tree

2 files changed

+24
-14
lines changed
  • instrumentation
    • opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi
    • opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi

2 files changed

+24
-14
lines changed

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -416,21 +416,22 @@ def set_status_code(span, status_code):
416416

417417
def get_default_span_details(scope: dict) -> Tuple[str, dict]:
418418
"""Default implementation for get_default_span_details
419+
https://github.com/open-telemetry/opentelemetry-specification/pull/3165
420+
https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name
421+
419422
Args:
420423
scope: the ASGI scope dictionary
421424
Returns:
422425
a tuple of the span name, and any attributes to attach to the span.
423426
"""
424-
if scope.get("type") == "websocket":
425-
return f"{scope.get('path', '').strip()}", {}
426-
span_name = (
427-
f"{scope.get('method', '').strip()} {scope.get('path', '').strip()}"
428-
if scope.get("path", "").strip()
429-
else f"{scope.get('method', '').strip()}"
430-
)
431-
432-
return span_name, {}
433-
427+
path = scope.get("path", "").strip()
428+
method = scope.get("method", "").strip()
429+
if method and path: # http
430+
return f"{method} {path}", {}
431+
if path: # websocket
432+
return path, {}
433+
return method, {} # http with no path
434+
434435

435436
def _collect_target_attribute(
436437
scope: typing.Dict[str, typing.Any]

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,20 @@ def add_response_attributes(
440440

441441

442442
def get_default_span_name(environ):
443-
"""Default implementation for name_callback, returns `{METHOD_NAME}`."""
443+
"""
444+
Default implementation for name_callback.
445+
https://github.com/open-telemetry/opentelemetry-specification/pull/3165
446+
https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name
447+
448+
Args:
449+
environ: The WSGI environ object.
450+
Returns:
451+
The span name.
452+
"""
444453
method = environ.get("REQUEST_METHOD", "").strip()
445-
route = environ.get("PATH_INFO", "").strip()
446-
if route:
447-
return f"{method} {route}"
454+
path = environ.get("PATH_INFO", "").strip()
455+
if method and path:
456+
return f"{method} {path}"
448457
return method
449458

450459

0 commit comments

Comments
 (0)