Skip to content

Commit fc442d1

Browse files
EzzioMoreiraxrmx
andauthored
feat(starlette): stop using SpanAttributes (#3655)
Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent a39c6c1 commit fc442d1

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
192192
from opentelemetry.instrumentation.starlette.package import _instruments
193193
from opentelemetry.instrumentation.starlette.version import __version__
194194
from opentelemetry.metrics import MeterProvider, get_meter
195-
from opentelemetry.semconv.trace import SpanAttributes
195+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
196+
HTTP_ROUTE,
197+
)
196198
from opentelemetry.trace import TracerProvider, get_tracer
197199
from opentelemetry.util.http import get_excluded_urls
198200

@@ -374,7 +376,7 @@ def _get_default_span_details(
374376
method: str = scope.get("method", "")
375377
attributes: dict[str, Any] = {}
376378
if route:
377-
attributes[SpanAttributes.HTTP_ROUTE] = route
379+
attributes[HTTP_ROUTE] = route
378380
if method and route: # http
379381
span_name = f"{method} {route}"
380382
elif route: # websocket

instrumentation/opentelemetry-instrumentation-starlette/tests/test_starlette_instrumentation.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
NumberDataPoint,
2929
)
3030
from opentelemetry.sdk.resources import Resource
31-
from opentelemetry.semconv.trace import SpanAttributes
31+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
32+
HTTP_FLAVOR,
33+
HTTP_ROUTE,
34+
HTTP_TARGET,
35+
HTTP_URL,
36+
)
3237
from opentelemetry.test.globals_test import reset_trace_globals
3338
from opentelemetry.test.test_base import TestBase
3439
from opentelemetry.trace import (
@@ -122,22 +127,17 @@ def test_sub_app_starlette_call(self):
122127
spans_with_http_attributes = [
123128
span
124129
for span in spans
125-
if (
126-
SpanAttributes.HTTP_URL in span.attributes
127-
or SpanAttributes.HTTP_TARGET in span.attributes
128-
)
130+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
129131
]
130132

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

134136
for span in spans_with_http_attributes:
135-
self.assertEqual(
136-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
137-
)
137+
self.assertEqual("/sub/home", span.attributes[HTTP_TARGET])
138138
self.assertEqual(
139139
"http://testserver/sub/home",
140-
span.attributes[SpanAttributes.HTTP_URL],
140+
span.attributes[HTTP_URL],
141141
)
142142

143143
def test_starlette_route_attribute_added(self):
@@ -147,14 +147,10 @@ def test_starlette_route_attribute_added(self):
147147
self.assertEqual(len(spans), 3)
148148
for span in spans:
149149
self.assertIn("GET /user/{username}", span.name)
150-
self.assertEqual(
151-
spans[-1].attributes[SpanAttributes.HTTP_ROUTE], "/user/{username}"
152-
)
150+
self.assertEqual(spans[-1].attributes[HTTP_ROUTE], "/user/{username}")
153151
# ensure that at least one attribute that is populated by
154152
# the asgi instrumentation is successfully feeding though.
155-
self.assertEqual(
156-
spans[-1].attributes[SpanAttributes.HTTP_FLAVOR], "1.1"
157-
)
153+
self.assertEqual(spans[-1].attributes[HTTP_FLAVOR], "1.1")
158154

159155
def test_starlette_excluded_urls(self):
160156
"""Ensure that given starlette routes are excluded."""
@@ -456,10 +452,7 @@ def test_sub_app_starlette_call(self):
456452
spans_with_http_attributes = [
457453
span
458454
for span in spans
459-
if (
460-
SpanAttributes.HTTP_URL in span.attributes
461-
or SpanAttributes.HTTP_TARGET in span.attributes
462-
)
455+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
463456
]
464457

465458
# We now expect spans with attributes from both the app and its sub app
@@ -480,12 +473,10 @@ def test_sub_app_starlette_call(self):
480473
self.assertIsNotNone(server_span)
481474
# As soon as the bug is fixed for starlette, we can iterate over spans_with_http_attributes here
482475
# to verify the correctness of the attributes for the internal span as well
483-
self.assertEqual(
484-
"/sub/home", server_span.attributes[SpanAttributes.HTTP_TARGET]
485-
)
476+
self.assertEqual("/sub/home", server_span.attributes[HTTP_TARGET])
486477
self.assertEqual(
487478
"http://testserver/sub/home",
488-
server_span.attributes[SpanAttributes.HTTP_URL],
479+
server_span.attributes[HTTP_URL],
489480
)
490481

491482

@@ -542,10 +533,7 @@ def test_sub_app_starlette_call(self):
542533
spans_with_http_attributes = [
543534
span
544535
for span in spans
545-
if (
546-
SpanAttributes.HTTP_URL in span.attributes
547-
or SpanAttributes.HTTP_TARGET in span.attributes
548-
)
536+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
549537
]
550538

551539
# We now expect spans with attributes from both the app and its sub app
@@ -566,12 +554,10 @@ def test_sub_app_starlette_call(self):
566554
self.assertIsNotNone(server_span)
567555
# As soon as the bug is fixed for starlette, we can iterate over spans_with_http_attributes here
568556
# to verify the correctness of the attributes for the internal span as well
569-
self.assertEqual(
570-
"/sub/home", server_span.attributes[SpanAttributes.HTTP_TARGET]
571-
)
557+
self.assertEqual("/sub/home", server_span.attributes[HTTP_TARGET])
572558
self.assertEqual(
573559
"http://testserver/sub/home",
574-
server_span.attributes[SpanAttributes.HTTP_URL],
560+
server_span.attributes[HTTP_URL],
575561
)
576562

577563

0 commit comments

Comments
 (0)