Skip to content

Commit f52eda7

Browse files
committed
refactor(fastapi): refactor tests
1 parent b088bcb commit f52eda7

File tree

1 file changed

+85
-74
lines changed

1 file changed

+85
-74
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 85 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@
5555
NETWORK_PROTOCOL_VERSION,
5656
)
5757
from opentelemetry.semconv.attributes.url_attributes import URL_SCHEME
58-
from opentelemetry.semconv.trace import SpanAttributes
58+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
59+
HTTP_FLAVOR,
60+
HTTP_HOST,
61+
HTTP_METHOD,
62+
HTTP_ROUTE,
63+
HTTP_SCHEME,
64+
HTTP_SERVER_NAME,
65+
HTTP_STATUS_CODE,
66+
HTTP_TARGET,
67+
HTTP_URL,
68+
)
69+
from opentelemetry.semconv._incubating.attributes.net_attributes import NET_HOST_PORT
5970
from opentelemetry.test.globals_test import reset_trace_globals
6071
from opentelemetry.test.test_base import TestBase
6172
from opentelemetry.util._importlib_metadata import entry_points
@@ -85,15 +96,15 @@
8596
"http.server.active_requests": _server_active_requests_count_attrs_old,
8697
"http.server.duration": {
8798
*_server_duration_attrs_old,
88-
SpanAttributes.HTTP_TARGET,
99+
HTTP_TARGET,
89100
},
90101
"http.server.response.size": {
91102
*_server_duration_attrs_old,
92-
SpanAttributes.HTTP_TARGET,
103+
HTTP_TARGET,
93104
},
94105
"http.server.request.size": {
95106
*_server_duration_attrs_old,
96-
SpanAttributes.HTTP_TARGET,
107+
HTTP_TARGET,
97108
},
98109
}
99110

@@ -245,8 +256,8 @@ def test_sub_app_fastapi_call(self):
245256
span
246257
for span in spans
247258
if (
248-
SpanAttributes.HTTP_URL in span.attributes
249-
or SpanAttributes.HTTP_TARGET in span.attributes
259+
HTTP_URL in span.attributes
260+
or HTTP_TARGET in span.attributes
250261
)
251262
]
252263

@@ -256,11 +267,11 @@ def test_sub_app_fastapi_call(self):
256267

257268
for span in spans_with_http_attributes:
258269
self.assertEqual(
259-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
270+
"/sub/home", span.attributes[HTTP_TARGET]
260271
)
261272
self.assertEqual(
262273
"https://testserver:443/sub/home",
263-
span.attributes[SpanAttributes.HTTP_URL],
274+
span.attributes[HTTP_URL],
264275
)
265276

266277

@@ -309,8 +320,8 @@ def test_sub_app_fastapi_call(self):
309320
span
310321
for span in spans
311322
if (
312-
SpanAttributes.HTTP_URL in span.attributes
313-
or SpanAttributes.HTTP_TARGET in span.attributes
323+
HTTP_URL in span.attributes
324+
or HTTP_TARGET in span.attributes
314325
)
315326
]
316327

@@ -319,11 +330,11 @@ def test_sub_app_fastapi_call(self):
319330

320331
for span in spans_with_http_attributes:
321332
self.assertEqual(
322-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
333+
"/sub/home", span.attributes[HTTP_TARGET]
323334
)
324335
self.assertEqual(
325336
"https://testserver:443/sub/home",
326-
span.attributes[SpanAttributes.HTTP_URL],
337+
span.attributes[HTTP_URL],
327338
)
328339

329340

@@ -382,12 +393,12 @@ def test_fastapi_route_attribute_added(self):
382393
for span in spans:
383394
self.assertIn("GET /user/{username}", span.name)
384395
self.assertEqual(
385-
spans[-1].attributes[SpanAttributes.HTTP_ROUTE], "/user/{username}"
396+
spans[-1].attributes[HTTP_ROUTE], "/user/{username}"
386397
)
387398
# ensure that at least one attribute that is populated by
388399
# the asgi instrumentation is successfully feeding though.
389400
self.assertEqual(
390-
spans[-1].attributes[SpanAttributes.HTTP_FLAVOR], "1.1"
401+
spans[-1].attributes[HTTP_FLAVOR], "1.1"
391402
)
392403

393404
def test_fastapi_excluded_urls(self):
@@ -511,21 +522,21 @@ def test_basic_metric_success(self):
511522
self._client.get("/foobar")
512523
duration = max(round((default_timer() - start) * 1000), 0)
513524
expected_duration_attributes = {
514-
SpanAttributes.HTTP_METHOD: "GET",
515-
SpanAttributes.HTTP_HOST: "testserver:443",
516-
SpanAttributes.HTTP_SCHEME: "https",
517-
SpanAttributes.HTTP_FLAVOR: "1.1",
518-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
519-
SpanAttributes.NET_HOST_PORT: 443,
520-
SpanAttributes.HTTP_STATUS_CODE: 200,
521-
SpanAttributes.HTTP_TARGET: "/foobar",
525+
HTTP_METHOD: "GET",
526+
HTTP_HOST: "testserver:443",
527+
HTTP_SCHEME: "https",
528+
HTTP_FLAVOR: "1.1",
529+
HTTP_SERVER_NAME: "testserver",
530+
NET_HOST_PORT: 443,
531+
HTTP_STATUS_CODE: 200,
532+
HTTP_TARGET: "/foobar",
522533
}
523534
expected_requests_count_attributes = {
524-
SpanAttributes.HTTP_METHOD: "GET",
525-
SpanAttributes.HTTP_HOST: "testserver:443",
526-
SpanAttributes.HTTP_SCHEME: "https",
527-
SpanAttributes.HTTP_FLAVOR: "1.1",
528-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
535+
HTTP_METHOD: "GET",
536+
HTTP_HOST: "testserver:443",
537+
HTTP_SCHEME: "https",
538+
HTTP_FLAVOR: "1.1",
539+
HTTP_SERVER_NAME: "testserver",
529540
}
530541
metrics_list = self.memory_metrics_reader.get_metrics_data()
531542
for metric in (
@@ -593,14 +604,14 @@ def test_basic_metric_success_both_semconv(self):
593604
duration = max(round((default_timer() - start) * 1000), 0)
594605
duration_s = max(default_timer() - start, 0)
595606
expected_duration_attributes_old = {
596-
SpanAttributes.HTTP_METHOD: "GET",
597-
SpanAttributes.HTTP_HOST: "testserver:443",
598-
SpanAttributes.HTTP_SCHEME: "https",
599-
SpanAttributes.HTTP_FLAVOR: "1.1",
600-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
601-
SpanAttributes.NET_HOST_PORT: 443,
602-
SpanAttributes.HTTP_STATUS_CODE: 200,
603-
SpanAttributes.HTTP_TARGET: "/foobar",
607+
HTTP_METHOD: "GET",
608+
HTTP_HOST: "testserver:443",
609+
HTTP_SCHEME: "https",
610+
HTTP_FLAVOR: "1.1",
611+
HTTP_SERVER_NAME: "testserver",
612+
NET_HOST_PORT: 443,
613+
HTTP_STATUS_CODE: 200,
614+
HTTP_TARGET: "/foobar",
604615
}
605616
expected_duration_attributes_new = {
606617
HTTP_REQUEST_METHOD: "GET",
@@ -610,11 +621,11 @@ def test_basic_metric_success_both_semconv(self):
610621
HTTP_ROUTE: "/foobar",
611622
}
612623
expected_requests_count_attributes = {
613-
SpanAttributes.HTTP_METHOD: "GET",
614-
SpanAttributes.HTTP_HOST: "testserver:443",
615-
SpanAttributes.HTTP_SCHEME: "https",
616-
SpanAttributes.HTTP_FLAVOR: "1.1",
617-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
624+
HTTP_METHOD: "GET",
625+
HTTP_HOST: "testserver:443",
626+
HTTP_SCHEME: "https",
627+
HTTP_FLAVOR: "1.1",
628+
HTTP_SERVER_NAME: "testserver",
618629
HTTP_REQUEST_METHOD: "GET",
619630
URL_SCHEME: "https",
620631
}
@@ -676,21 +687,21 @@ def test_basic_metric_nonstandard_http_method_success(self):
676687
self._client.request("NONSTANDARD", "/foobar")
677688
duration = max(round((default_timer() - start) * 1000), 0)
678689
expected_duration_attributes = {
679-
SpanAttributes.HTTP_METHOD: "_OTHER",
680-
SpanAttributes.HTTP_HOST: "testserver:443",
681-
SpanAttributes.HTTP_SCHEME: "https",
682-
SpanAttributes.HTTP_FLAVOR: "1.1",
683-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
684-
SpanAttributes.NET_HOST_PORT: 443,
685-
SpanAttributes.HTTP_STATUS_CODE: 405,
686-
SpanAttributes.HTTP_TARGET: "/foobar",
690+
HTTP_METHOD: "_OTHER",
691+
HTTP_HOST: "testserver:443",
692+
HTTP_SCHEME: "https",
693+
HTTP_FLAVOR: "1.1",
694+
HTTP_SERVER_NAME: "testserver",
695+
NET_HOST_PORT: 443,
696+
HTTP_STATUS_CODE: 405,
697+
HTTP_TARGET: "/foobar",
687698
}
688699
expected_requests_count_attributes = {
689-
SpanAttributes.HTTP_METHOD: "_OTHER",
690-
SpanAttributes.HTTP_HOST: "testserver:443",
691-
SpanAttributes.HTTP_SCHEME: "https",
692-
SpanAttributes.HTTP_FLAVOR: "1.1",
693-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
700+
HTTP_METHOD: "_OTHER",
701+
HTTP_HOST: "testserver:443",
702+
HTTP_SCHEME: "https",
703+
HTTP_FLAVOR: "1.1",
704+
HTTP_SERVER_NAME: "testserver",
694705
}
695706
metrics_list = self.memory_metrics_reader.get_metrics_data()
696707
for metric in (
@@ -758,14 +769,14 @@ def test_basic_metric_nonstandard_http_method_success_both_semconv(self):
758769
duration = max(round((default_timer() - start) * 1000), 0)
759770
duration_s = max(default_timer() - start, 0)
760771
expected_duration_attributes_old = {
761-
SpanAttributes.HTTP_METHOD: "_OTHER",
762-
SpanAttributes.HTTP_HOST: "testserver:443",
763-
SpanAttributes.HTTP_SCHEME: "https",
764-
SpanAttributes.HTTP_FLAVOR: "1.1",
765-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
766-
SpanAttributes.NET_HOST_PORT: 443,
767-
SpanAttributes.HTTP_STATUS_CODE: 405,
768-
SpanAttributes.HTTP_TARGET: "/foobar",
772+
HTTP_METHOD: "_OTHER",
773+
HTTP_HOST: "testserver:443",
774+
HTTP_SCHEME: "https",
775+
HTTP_FLAVOR: "1.1",
776+
HTTP_SERVER_NAME: "testserver",
777+
NET_HOST_PORT: 443,
778+
HTTP_STATUS_CODE: 405,
779+
HTTP_TARGET: "/foobar",
769780
}
770781
expected_duration_attributes_new = {
771782
HTTP_REQUEST_METHOD: "_OTHER",
@@ -775,11 +786,11 @@ def test_basic_metric_nonstandard_http_method_success_both_semconv(self):
775786
HTTP_ROUTE: "/foobar",
776787
}
777788
expected_requests_count_attributes = {
778-
SpanAttributes.HTTP_METHOD: "_OTHER",
779-
SpanAttributes.HTTP_HOST: "testserver:443",
780-
SpanAttributes.HTTP_SCHEME: "https",
781-
SpanAttributes.HTTP_FLAVOR: "1.1",
782-
SpanAttributes.HTTP_SERVER_NAME: "testserver",
789+
HTTP_METHOD: "_OTHER",
790+
HTTP_HOST: "testserver:443",
791+
HTTP_SCHEME: "https",
792+
HTTP_FLAVOR: "1.1",
793+
HTTP_SERVER_NAME: "testserver",
783794
HTTP_REQUEST_METHOD: "_OTHER",
784795
URL_SCHEME: "https",
785796
}
@@ -1204,8 +1215,8 @@ def test_sub_app_fastapi_call(self):
12041215
span
12051216
for span in spans
12061217
if (
1207-
SpanAttributes.HTTP_URL in span.attributes
1208-
or SpanAttributes.HTTP_TARGET in span.attributes
1218+
HTTP_URL in span.attributes
1219+
or HTTP_TARGET in span.attributes
12091220
)
12101221
]
12111222

@@ -1214,11 +1225,11 @@ def test_sub_app_fastapi_call(self):
12141225

12151226
for span in spans_with_http_attributes:
12161227
self.assertEqual(
1217-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
1228+
"/sub/home", span.attributes[HTTP_TARGET]
12181229
)
12191230
self.assertEqual(
12201231
"https://testserver:443/sub/home",
1221-
span.attributes[SpanAttributes.HTTP_URL],
1232+
span.attributes[HTTP_URL],
12221233
)
12231234

12241235

@@ -1297,8 +1308,8 @@ def test_sub_app_fastapi_call(self):
12971308
span
12981309
for span in spans
12991310
if (
1300-
SpanAttributes.HTTP_URL in span.attributes
1301-
or SpanAttributes.HTTP_TARGET in span.attributes
1311+
HTTP_URL in span.attributes
1312+
or HTTP_TARGET in span.attributes
13021313
)
13031314
]
13041315

@@ -1307,11 +1318,11 @@ def test_sub_app_fastapi_call(self):
13071318

13081319
for span in spans_with_http_attributes:
13091320
self.assertEqual(
1310-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
1321+
"/sub/home", span.attributes[HTTP_TARGET]
13111322
)
13121323
self.assertEqual(
13131324
"https://testserver:443/sub/home",
1314-
span.attributes[SpanAttributes.HTTP_URL],
1325+
span.attributes[HTTP_URL],
13151326
)
13161327

13171328

0 commit comments

Comments
 (0)