Skip to content

Commit 7fbec0f

Browse files
committed
refactor(flask): replace SpanAttributes with semconv attributes
1 parent c1a6895 commit 7fbec0f

File tree

2 files changed

+86
-54
lines changed

2 files changed

+86
-54
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ def response_hook(span: Span, status: str, response_headers: List):
278278
from opentelemetry.semconv.metrics.http_metrics import (
279279
HTTP_SERVER_REQUEST_DURATION,
280280
)
281-
from opentelemetry.semconv.trace import SpanAttributes
281+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
282+
HTTP_TARGET,
283+
HTTP_ROUTE
284+
)
282285
from opentelemetry.util._importlib_metadata import version
283286
from opentelemetry.util.http import (
284287
get_excluded_urls,
@@ -404,7 +407,7 @@ def _start_response(status, response_headers, *args, **kwargs):
404407

405408
if request_route:
406409
# http.target to be included in old semantic conventions
407-
duration_attrs_old[SpanAttributes.HTTP_TARGET] = str(
410+
duration_attrs_old[HTTP_TARGET] = str(
408411
request_route
409412
)
410413

@@ -449,7 +452,7 @@ def _before_request():
449452
if flask.request.url_rule:
450453
# For 404 that result from no route found, etc, we
451454
# don't have a url_rule.
452-
attributes[SpanAttributes.HTTP_ROUTE] = flask.request.url_rule.rule
455+
attributes[HTTP_ROUTE] = flask.request.url_rule.rule
453456
span, token = _start_internal_or_server_span(
454457
tracer=tracer,
455458
span_name=span_name,

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

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,36 @@
4141
)
4242
from opentelemetry.sdk.resources import Resource
4343
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
44-
from opentelemetry.semconv.trace import SpanAttributes
44+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
45+
HTTP_METHOD,
46+
HTTP_SERVER_NAME,
47+
HTTP_SCHEME,
48+
HTTP_HOST,
49+
HTTP_TARGET,
50+
HTTP_FLAVOR,
51+
HTTP_STATUS_CODE,
52+
HTTP_REQUEST_METHOD,
53+
HTTP_RESPONSE_STATUS_CODE,
54+
HTTP_ROUTE,
55+
56+
57+
)
58+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
59+
NET_HOST_PORT,
60+
NET_HOST_NAME,
61+
62+
)
63+
from opentelemetry.semconv._incubating.attributes.server_attributes import (
64+
SERVER_PORT,
65+
SERVER_ADDRESS,
66+
)
67+
from opentelemetry.semconv._incubating.attributes.url_attributes import (
68+
URL_PATH,
69+
URL_SCHEME,
70+
)
71+
from opentelemetry.semconv._incubating.attributes.network_attributes import (
72+
NETWORK_PROTOCOL_VERSION
73+
)
4574
from opentelemetry.test.wsgitestutil import WsgiTestBase
4675
from opentelemetry.util.http import (
4776
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
@@ -57,15 +86,15 @@
5786

5887
def expected_attributes(override_attributes):
5988
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,
89+
HTTP_METHOD: "GET",
90+
HTTP_SERVER_NAME: "localhost",
91+
HTTP_SCHEME: "http",
92+
NET_HOST_PORT: 80,
93+
NET_HOST_NAME: "localhost",
94+
HTTP_HOST: "localhost",
95+
HTTP_TARGET: "/",
96+
HTTP_FLAVOR: "1.1",
97+
HTTP_STATUS_CODE: 200,
6998
}
7099
for key, val in override_attributes.items():
71100
default_attributes[key] = val
@@ -74,12 +103,12 @@ def expected_attributes(override_attributes):
74103

75104
def expected_attributes_new(override_attributes):
76105
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,
106+
HTTP_REQUEST_METHOD: "GET",
107+
SERVER_PORT: 80,
108+
SERVER_ADDRESS: "localhost",
109+
URL_PATH: "/hello/123",
110+
NETWORK_PROTOCOL_VERSION: "1.1",
111+
HTTP_RESPONSE_STATUS_CODE: 200,
83112
}
84113
for key, val in override_attributes.items():
85114
default_attributes[key] = val
@@ -220,8 +249,8 @@ def assert_environ():
220249
def test_simple(self):
221250
expected_attrs = expected_attributes(
222251
{
223-
SpanAttributes.HTTP_TARGET: "/hello/123",
224-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
252+
HTTP_TARGET: "/hello/123",
253+
HTTP_ROUTE: "/hello/<int:helloid>",
225254
}
226255
)
227256
self.client.get("/hello/123")
@@ -235,8 +264,8 @@ def test_simple(self):
235264
def test_simple_new_semconv(self):
236265
expected_attrs = expected_attributes_new(
237266
{
238-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
239-
SpanAttributes.URL_SCHEME: "http",
267+
HTTP_ROUTE: "/hello/<int:helloid>",
268+
URL_SCHEME: "http",
240269
}
241270
)
242271
self.client.get("/hello/123")
@@ -250,15 +279,15 @@ def test_simple_new_semconv(self):
250279
def test_simple_both_semconv(self):
251280
expected_attrs = expected_attributes(
252281
{
253-
SpanAttributes.HTTP_TARGET: "/hello/123",
254-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
282+
HTTP_TARGET: "/hello/123",
283+
HTTP_ROUTE: "/hello/<int:helloid>",
255284
}
256285
)
257286
expected_attrs.update(
258287
expected_attributes_new(
259288
{
260-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
261-
SpanAttributes.URL_SCHEME: "http",
289+
HTTP_ROUTE: "/hello/<int:helloid>",
290+
URL_SCHEME: "http",
262291
}
263292
)
264293
)
@@ -301,9 +330,9 @@ def test_not_recording(self):
301330
def test_404(self):
302331
expected_attrs = expected_attributes(
303332
{
304-
SpanAttributes.HTTP_METHOD: "POST",
305-
SpanAttributes.HTTP_TARGET: "/bye",
306-
SpanAttributes.HTTP_STATUS_CODE: 404,
333+
HTTP_METHOD: "POST",
334+
HTTP_TARGET: "/bye",
335+
HTTP_STATUS_CODE: 404,
307336
}
308337
)
309338

@@ -319,10 +348,10 @@ def test_404(self):
319348
def test_404_new_semconv(self):
320349
expected_attrs = expected_attributes_new(
321350
{
322-
SpanAttributes.HTTP_REQUEST_METHOD: "POST",
323-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 404,
324-
SpanAttributes.URL_PATH: "/bye",
325-
SpanAttributes.URL_SCHEME: "http",
351+
HTTP_REQUEST_METHOD: "POST",
352+
HTTP_RESPONSE_STATUS_CODE: 404,
353+
URL_PATH: "/bye",
354+
URL_SCHEME: "http",
326355
}
327356
)
328357

@@ -338,18 +367,18 @@ def test_404_new_semconv(self):
338367
def test_404_both_semconv(self):
339368
expected_attrs = expected_attributes(
340369
{
341-
SpanAttributes.HTTP_METHOD: "POST",
342-
SpanAttributes.HTTP_TARGET: "/bye",
343-
SpanAttributes.HTTP_STATUS_CODE: 404,
370+
HTTP_METHOD: "POST",
371+
HTTP_TARGET: "/bye",
372+
HTTP_STATUS_CODE: 404,
344373
}
345374
)
346375
expected_attrs.update(
347376
expected_attributes_new(
348377
{
349-
SpanAttributes.HTTP_REQUEST_METHOD: "POST",
350-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 404,
351-
SpanAttributes.URL_PATH: "/bye",
352-
SpanAttributes.URL_SCHEME: "http",
378+
HTTP_REQUEST_METHOD: "POST",
379+
HTTP_RESPONSE_STATUS_CODE: 404,
380+
URL_PATH: "/bye",
381+
URL_SCHEME: "http",
353382
}
354383
)
355384
)
@@ -366,9 +395,9 @@ def test_404_both_semconv(self):
366395
def test_internal_error(self):
367396
expected_attrs = expected_attributes(
368397
{
369-
SpanAttributes.HTTP_TARGET: "/hello/500",
370-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
371-
SpanAttributes.HTTP_STATUS_CODE: 500,
398+
HTTP_TARGET: "/hello/500",
399+
HTTP_ROUTE: "/hello/<int:helloid>",
400+
HTTP_STATUS_CODE: 500,
372401
}
373402
)
374403
resp = self.client.get("/hello/500")
@@ -383,11 +412,11 @@ def test_internal_error(self):
383412
def test_internal_error_new_semconv(self):
384413
expected_attrs = expected_attributes_new(
385414
{
386-
SpanAttributes.URL_PATH: "/hello/500",
387-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
388-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
415+
URL_PATH: "/hello/500",
416+
HTTP_ROUTE: "/hello/<int:helloid>",
417+
HTTP_RESPONSE_STATUS_CODE: 500,
389418
ERROR_TYPE: "500",
390-
SpanAttributes.URL_SCHEME: "http",
419+
URL_SCHEME: "http",
391420
}
392421
)
393422
resp = self.client.get("/hello/500")
@@ -402,18 +431,18 @@ def test_internal_error_new_semconv(self):
402431
def test_internal_error_both_semconv(self):
403432
expected_attrs = expected_attributes(
404433
{
405-
SpanAttributes.HTTP_TARGET: "/hello/500",
406-
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
407-
SpanAttributes.HTTP_STATUS_CODE: 500,
434+
HTTP_TARGET: "/hello/500",
435+
HTTP_ROUTE: "/hello/<int:helloid>",
436+
HTTP_STATUS_CODE: 500,
408437
}
409438
)
410439
expected_attrs.update(
411440
expected_attributes_new(
412441
{
413-
SpanAttributes.URL_PATH: "/hello/500",
414-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
442+
URL_PATH: "/hello/500",
443+
HTTP_RESPONSE_STATUS_CODE: 500,
415444
ERROR_TYPE: "500",
416-
SpanAttributes.URL_SCHEME: "http",
445+
URL_SCHEME: "http",
417446
}
418447
)
419448
)

0 commit comments

Comments
 (0)