Skip to content

Commit

Permalink
Fix ASGI instrumentation default span name
Browse files Browse the repository at this point in the history
Fixes ASGI default span name for SERVER spans, to be `HTTP <method>`.

Fixes #146.
  • Loading branch information
adamantike committed Apr 6, 2021
1 parent e96a2cd commit 6bdc9e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#387](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/387))
- Update redis instrumentation to follow semantic conventions
([#403](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/403))
- Fix ASGI instrumentation default span name.
([#418](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/418))

### Added
- `opentelemetry-instrumentation-urllib3` Add urllib3 instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ def get_default_span_details(scope: dict) -> Tuple[str, dict]:
scope: the asgi scope dictionary
Returns:
a tuple of the span, and any attributes to attach to the
span.
a tuple of the span name, and any attributes to attach to the span.
"""
method_or_path = scope.get("method") or scope.get("path")
span_name = "HTTP {}".format(method_or_path.strip())

return method_or_path, {}
return span_name, {}


class OpenTelemetryMiddleware:
Expand Down Expand Up @@ -194,7 +194,7 @@ async def __call__(self, scope, receive, send):

try:
with self.tracer.start_as_current_span(
span_name + " asgi", kind=trace.SpanKind.SERVER,
span_name, kind=trace.SpanKind.SERVER,
) as span:
if span.is_recording():
attributes = collect_request_attributes(scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,25 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
self.assertEqual(len(span_list), 4)
expected = [
{
"name": "GET asgi.http.receive",
"name": "HTTP GET asgi.http.receive",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {"type": "http.request"},
},
{
"name": "GET asgi.http.send",
"name": "HTTP GET asgi.http.send",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {
"http.status_code": 200,
"type": "http.response.start",
},
},
{
"name": "GET asgi.http.send",
"name": "HTTP GET asgi.http.send",
"kind": trace_api.SpanKind.INTERNAL,
"attributes": {"type": "http.response.body"},
},
{
"name": "GET asgi",
"name": "HTTP GET",
"kind": trace_api.SpanKind.SERVER,
"attributes": {
"http.method": "GET",
Expand Down Expand Up @@ -197,9 +197,12 @@ def get_predefined_span_details(_):

def update_expected_span_name(expected):
for entry in expected:
entry["name"] = " ".join(
[span_name] + entry["name"].split(" ")[-1:]
)
if entry["kind"] == trace_api.SpanKind.SERVER:
entry["name"] = span_name
else:
entry["name"] = " ".join(
[span_name] + entry["name"].split(" ")[-1:]
)
return expected

app = otel_asgi.OpenTelemetryMiddleware(
Expand Down Expand Up @@ -284,12 +287,12 @@ def test_websocket(self):
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 6)
expected = [
"/ asgi.websocket.receive",
"/ asgi.websocket.send",
"/ asgi.websocket.receive",
"/ asgi.websocket.send",
"/ asgi.websocket.receive",
"/ asgi",
"HTTP / asgi.websocket.receive",
"HTTP / asgi.websocket.send",
"HTTP / asgi.websocket.receive",
"HTTP / asgi.websocket.send",
"HTTP / asgi.websocket.receive",
"HTTP /",
]
actual = [span.name for span in span_list]
self.assertListEqual(actual, expected)
Expand Down

0 comments on commit 6bdc9e6

Please sign in to comment.