Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
from opentelemetry.instrumentation.starlette.package import _instruments
from opentelemetry.instrumentation.starlette.version import __version__
from opentelemetry.metrics import MeterProvider, get_meter
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.http_attributes import (
HTTP_ROUTE,
)
from opentelemetry.trace import TracerProvider, get_tracer
from opentelemetry.util.http import get_excluded_urls

Expand Down Expand Up @@ -374,7 +376,7 @@ def _get_default_span_details(
method: str = scope.get("method", "")
attributes: dict[str, Any] = {}
if route:
attributes[SpanAttributes.HTTP_ROUTE] = route
attributes[HTTP_ROUTE] = route
if method and route: # http
span_name = f"{method} {route}"
elif route: # websocket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
NumberDataPoint,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.http_attributes import (
HTTP_FLAVOR,
HTTP_ROUTE,
HTTP_TARGET,
HTTP_URL,
)
from opentelemetry.test.globals_test import reset_trace_globals
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import (
Expand Down Expand Up @@ -122,22 +127,17 @@ def test_sub_app_starlette_call(self):
spans_with_http_attributes = [
span
for span in spans
if (
SpanAttributes.HTTP_URL in span.attributes
or SpanAttributes.HTTP_TARGET in span.attributes
)
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
]

# expect only one span to have the attributes
self.assertEqual(1, len(spans_with_http_attributes))

for span in spans_with_http_attributes:
self.assertEqual(
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
)
self.assertEqual("/sub/home", span.attributes[HTTP_TARGET])
self.assertEqual(
"http://testserver/sub/home",
span.attributes[SpanAttributes.HTTP_URL],
span.attributes[HTTP_URL],
)

def test_starlette_route_attribute_added(self):
Expand All @@ -147,14 +147,10 @@ def test_starlette_route_attribute_added(self):
self.assertEqual(len(spans), 3)
for span in spans:
self.assertIn("GET /user/{username}", span.name)
self.assertEqual(
spans[-1].attributes[SpanAttributes.HTTP_ROUTE], "/user/{username}"
)
self.assertEqual(spans[-1].attributes[HTTP_ROUTE], "/user/{username}")
# ensure that at least one attribute that is populated by
# the asgi instrumentation is successfully feeding though.
self.assertEqual(
spans[-1].attributes[SpanAttributes.HTTP_FLAVOR], "1.1"
)
self.assertEqual(spans[-1].attributes[HTTP_FLAVOR], "1.1")

def test_starlette_excluded_urls(self):
"""Ensure that given starlette routes are excluded."""
Expand Down Expand Up @@ -456,10 +452,7 @@ def test_sub_app_starlette_call(self):
spans_with_http_attributes = [
span
for span in spans
if (
SpanAttributes.HTTP_URL in span.attributes
or SpanAttributes.HTTP_TARGET in span.attributes
)
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
]

# We now expect spans with attributes from both the app and its sub app
Expand All @@ -480,12 +473,10 @@ def test_sub_app_starlette_call(self):
self.assertIsNotNone(server_span)
# As soon as the bug is fixed for starlette, we can iterate over spans_with_http_attributes here
# to verify the correctness of the attributes for the internal span as well
self.assertEqual(
"/sub/home", server_span.attributes[SpanAttributes.HTTP_TARGET]
)
self.assertEqual("/sub/home", server_span.attributes[HTTP_TARGET])
self.assertEqual(
"http://testserver/sub/home",
server_span.attributes[SpanAttributes.HTTP_URL],
server_span.attributes[HTTP_URL],
)


Expand Down Expand Up @@ -542,10 +533,7 @@ def test_sub_app_starlette_call(self):
spans_with_http_attributes = [
span
for span in spans
if (
SpanAttributes.HTTP_URL in span.attributes
or SpanAttributes.HTTP_TARGET in span.attributes
)
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
]

# We now expect spans with attributes from both the app and its sub app
Expand All @@ -566,12 +554,10 @@ def test_sub_app_starlette_call(self):
self.assertIsNotNone(server_span)
# As soon as the bug is fixed for starlette, we can iterate over spans_with_http_attributes here
# to verify the correctness of the attributes for the internal span as well
self.assertEqual(
"/sub/home", server_span.attributes[SpanAttributes.HTTP_TARGET]
)
self.assertEqual("/sub/home", server_span.attributes[HTTP_TARGET])
self.assertEqual(
"http://testserver/sub/home",
server_span.attributes[SpanAttributes.HTTP_URL],
server_span.attributes[HTTP_URL],
)


Expand Down