Skip to content

Commit 6c2aa7f

Browse files
authored
refactor(flask): replace SpanAttributes with semconv attributes (#3539)
* refactor(flask): replace SpanAttributes with semconv attributes * refactor(flask): replace SpanAttributes with semconv attributes (fix wrong import) * refactor(flask): replace SpanAttributes with semconv attributes (fix ruff linting)
1 parent 4afa1ee commit 6c2aa7f

File tree

2 files changed

+83
-57
lines changed

2 files changed

+83
-57
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,14 @@ def response_hook(span: Span, status: str, response_headers: List):
270270
)
271271
from opentelemetry.instrumentation.utils import _start_internal_or_server_span
272272
from opentelemetry.metrics import get_meter
273-
from opentelemetry.semconv.attributes.http_attributes import HTTP_ROUTE
273+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
274+
HTTP_ROUTE,
275+
HTTP_TARGET,
276+
)
274277
from opentelemetry.semconv.metrics import MetricInstruments
275278
from opentelemetry.semconv.metrics.http_metrics import (
276279
HTTP_SERVER_REQUEST_DURATION,
277280
)
278-
from opentelemetry.semconv.trace import SpanAttributes
279281
from opentelemetry.util._importlib_metadata import version
280282
from opentelemetry.util.http import (
281283
get_excluded_urls,
@@ -401,9 +403,7 @@ def _start_response(status, response_headers, *args, **kwargs):
401403

402404
if request_route:
403405
# http.target to be included in old semantic conventions
404-
duration_attrs_old[SpanAttributes.HTTP_TARGET] = str(
405-
request_route
406-
)
406+
duration_attrs_old[HTTP_TARGET] = str(request_route)
407407

408408
duration_histogram_old.record(
409409
max(round(duration_s * 1000), 0), duration_attrs_old
@@ -446,7 +446,7 @@ def _before_request():
446446
if flask.request.url_rule:
447447
# For 404 that result from no route found, etc, we
448448
# don't have a url_rule.
449-
attributes[SpanAttributes.HTTP_ROUTE] = flask.request.url_rule.rule
449+
attributes[HTTP_ROUTE] = flask.request.url_rule.rule
450450
span, token = _start_internal_or_server_span(
451451
tracer=tracer,
452452
span_name=span_name,

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,34 @@
4040
NumberDataPoint,
4141
)
4242
from opentelemetry.sdk.resources import Resource
43+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
44+
HTTP_FLAVOR,
45+
HTTP_HOST,
46+
HTTP_METHOD,
47+
HTTP_REQUEST_METHOD,
48+
HTTP_RESPONSE_STATUS_CODE,
49+
HTTP_ROUTE,
50+
HTTP_SCHEME,
51+
HTTP_SERVER_NAME,
52+
HTTP_STATUS_CODE,
53+
HTTP_TARGET,
54+
)
55+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
56+
NET_HOST_NAME,
57+
NET_HOST_PORT,
58+
)
59+
from opentelemetry.semconv._incubating.attributes.network_attributes import (
60+
NETWORK_PROTOCOL_VERSION,
61+
)
62+
from opentelemetry.semconv._incubating.attributes.server_attributes import (
63+
SERVER_ADDRESS,
64+
SERVER_PORT,
65+
)
66+
from opentelemetry.semconv._incubating.attributes.url_attributes import (
67+
URL_PATH,
68+
URL_SCHEME,
69+
)
4370
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
44-
from opentelemetry.semconv.trace import SpanAttributes
4571
from opentelemetry.test.wsgitestutil import WsgiTestBase
4672
from opentelemetry.util.http import (
4773
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
@@ -57,15 +83,15 @@
5783

5884
def expected_attributes(override_attributes):
5985
default_attributes = {
60-
SpanAttributes.HTTP_METHOD: "GET",
61-
SpanAttributes.HTTP_SERVER_NAME: "localhost",
62-
SpanAttributes.HTTP_SCHEME: "http",
63-
SpanAttributes.NET_HOST_PORT: 80,
64-
SpanAttributes.NET_HOST_NAME: "localhost",
65-
SpanAttributes.HTTP_HOST: "localhost",
66-
SpanAttributes.HTTP_TARGET: "/",
67-
SpanAttributes.HTTP_FLAVOR: "1.1",
68-
SpanAttributes.HTTP_STATUS_CODE: 200,
86+
HTTP_METHOD: "GET",
87+
HTTP_SERVER_NAME: "localhost",
88+
HTTP_SCHEME: "http",
89+
NET_HOST_PORT: 80,
90+
NET_HOST_NAME: "localhost",
91+
HTTP_HOST: "localhost",
92+
HTTP_TARGET: "/",
93+
HTTP_FLAVOR: "1.1",
94+
HTTP_STATUS_CODE: 200,
6995
}
7096
for key, val in override_attributes.items():
7197
default_attributes[key] = val
@@ -74,12 +100,12 @@ def expected_attributes(override_attributes):
74100

75101
def expected_attributes_new(override_attributes):
76102
default_attributes = {
77-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
78-
SpanAttributes.SERVER_PORT: 80,
79-
SpanAttributes.SERVER_ADDRESS: "localhost",
80-
SpanAttributes.URL_PATH: "/hello/123",
81-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
82-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
103+
HTTP_REQUEST_METHOD: "GET",
104+
SERVER_PORT: 80,
105+
SERVER_ADDRESS: "localhost",
106+
URL_PATH: "/hello/123",
107+
NETWORK_PROTOCOL_VERSION: "1.1",
108+
HTTP_RESPONSE_STATUS_CODE: 200,
83109
}
84110
for key, val in override_attributes.items():
85111
default_attributes[key] = val
@@ -220,8 +246,8 @@ def assert_environ():
220246
def test_simple(self):
221247
expected_attrs = expected_attributes(
222248
{
223-
SpanAttributes.HTTP_TARGET: "/hello/123",
224-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
249+
HTTP_TARGET: "/hello/123",
250+
HTTP_ROUTE: "/hello/<int:helloid>",
225251
}
226252
)
227253
self.client.get("/hello/123")
@@ -235,8 +261,8 @@ def test_simple(self):
235261
def test_simple_new_semconv(self):
236262
expected_attrs = expected_attributes_new(
237263
{
238-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
239-
SpanAttributes.URL_SCHEME: "http",
264+
HTTP_ROUTE: "/hello/<int:helloid>",
265+
URL_SCHEME: "http",
240266
}
241267
)
242268
self.client.get("/hello/123")
@@ -250,15 +276,15 @@ def test_simple_new_semconv(self):
250276
def test_simple_both_semconv(self):
251277
expected_attrs = expected_attributes(
252278
{
253-
SpanAttributes.HTTP_TARGET: "/hello/123",
254-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
279+
HTTP_TARGET: "/hello/123",
280+
HTTP_ROUTE: "/hello/<int:helloid>",
255281
}
256282
)
257283
expected_attrs.update(
258284
expected_attributes_new(
259285
{
260-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
261-
SpanAttributes.URL_SCHEME: "http",
286+
HTTP_ROUTE: "/hello/<int:helloid>",
287+
URL_SCHEME: "http",
262288
}
263289
)
264290
)
@@ -301,9 +327,9 @@ def test_not_recording(self):
301327
def test_404(self):
302328
expected_attrs = expected_attributes(
303329
{
304-
SpanAttributes.HTTP_METHOD: "POST",
305-
SpanAttributes.HTTP_TARGET: "/bye",
306-
SpanAttributes.HTTP_STATUS_CODE: 404,
330+
HTTP_METHOD: "POST",
331+
HTTP_TARGET: "/bye",
332+
HTTP_STATUS_CODE: 404,
307333
}
308334
)
309335

@@ -319,10 +345,10 @@ def test_404(self):
319345
def test_404_new_semconv(self):
320346
expected_attrs = expected_attributes_new(
321347
{
322-
SpanAttributes.HTTP_REQUEST_METHOD: "POST",
323-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 404,
324-
SpanAttributes.URL_PATH: "/bye",
325-
SpanAttributes.URL_SCHEME: "http",
348+
HTTP_REQUEST_METHOD: "POST",
349+
HTTP_RESPONSE_STATUS_CODE: 404,
350+
URL_PATH: "/bye",
351+
URL_SCHEME: "http",
326352
}
327353
)
328354

@@ -338,18 +364,18 @@ def test_404_new_semconv(self):
338364
def test_404_both_semconv(self):
339365
expected_attrs = expected_attributes(
340366
{
341-
SpanAttributes.HTTP_METHOD: "POST",
342-
SpanAttributes.HTTP_TARGET: "/bye",
343-
SpanAttributes.HTTP_STATUS_CODE: 404,
367+
HTTP_METHOD: "POST",
368+
HTTP_TARGET: "/bye",
369+
HTTP_STATUS_CODE: 404,
344370
}
345371
)
346372
expected_attrs.update(
347373
expected_attributes_new(
348374
{
349-
SpanAttributes.HTTP_REQUEST_METHOD: "POST",
350-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 404,
351-
SpanAttributes.URL_PATH: "/bye",
352-
SpanAttributes.URL_SCHEME: "http",
375+
HTTP_REQUEST_METHOD: "POST",
376+
HTTP_RESPONSE_STATUS_CODE: 404,
377+
URL_PATH: "/bye",
378+
URL_SCHEME: "http",
353379
}
354380
)
355381
)
@@ -366,9 +392,9 @@ def test_404_both_semconv(self):
366392
def test_internal_error(self):
367393
expected_attrs = expected_attributes(
368394
{
369-
SpanAttributes.HTTP_TARGET: "/hello/500",
370-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
371-
SpanAttributes.HTTP_STATUS_CODE: 500,
395+
HTTP_TARGET: "/hello/500",
396+
HTTP_ROUTE: "/hello/<int:helloid>",
397+
HTTP_STATUS_CODE: 500,
372398
}
373399
)
374400
resp = self.client.get("/hello/500")
@@ -383,11 +409,11 @@ def test_internal_error(self):
383409
def test_internal_error_new_semconv(self):
384410
expected_attrs = expected_attributes_new(
385411
{
386-
SpanAttributes.URL_PATH: "/hello/500",
387-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
388-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
412+
URL_PATH: "/hello/500",
413+
HTTP_ROUTE: "/hello/<int:helloid>",
414+
HTTP_RESPONSE_STATUS_CODE: 500,
389415
ERROR_TYPE: "500",
390-
SpanAttributes.URL_SCHEME: "http",
416+
URL_SCHEME: "http",
391417
}
392418
)
393419
resp = self.client.get("/hello/500")
@@ -402,18 +428,18 @@ def test_internal_error_new_semconv(self):
402428
def test_internal_error_both_semconv(self):
403429
expected_attrs = expected_attributes(
404430
{
405-
SpanAttributes.HTTP_TARGET: "/hello/500",
406-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
407-
SpanAttributes.HTTP_STATUS_CODE: 500,
431+
HTTP_TARGET: "/hello/500",
432+
HTTP_ROUTE: "/hello/<int:helloid>",
433+
HTTP_STATUS_CODE: 500,
408434
}
409435
)
410436
expected_attrs.update(
411437
expected_attributes_new(
412438
{
413-
SpanAttributes.URL_PATH: "/hello/500",
414-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
439+
URL_PATH: "/hello/500",
440+
HTTP_RESPONSE_STATUS_CODE: 500,
415441
ERROR_TYPE: "500",
416-
SpanAttributes.URL_SCHEME: "http",
442+
URL_SCHEME: "http",
417443
}
418444
)
419445
)

0 commit comments

Comments
 (0)