Skip to content

Commit 25525b3

Browse files
authored
Merge branch 'main' into rads-1996/redact-sensitive-params
2 parents 3e5a66a + ef2b546 commit 25525b3

File tree

3 files changed

+144
-149
lines changed

3 files changed

+144
-149
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
204204
from opentelemetry.instrumentation.fastapi.version import __version__
205205
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
206206
from opentelemetry.metrics import get_meter
207-
from opentelemetry.semconv.trace import SpanAttributes
207+
from opentelemetry.semconv.attributes.http_attributes import HTTP_ROUTE
208208
from opentelemetry.trace import get_tracer
209209
from opentelemetry.util.http import (
210210
get_excluded_urls,
@@ -450,7 +450,7 @@ def _get_default_span_details(scope):
450450
if method == "_OTHER":
451451
method = "HTTP"
452452
if route:
453-
attributes[SpanAttributes.HTTP_ROUTE] = route
453+
attributes[HTTP_ROUTE] = route
454454
if method and route: # http
455455
span_name = f"{method} {route}"
456456
elif route: # websocket

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

Lines changed: 82 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@
4646
NumberDataPoint,
4747
)
4848
from opentelemetry.sdk.resources import Resource
49+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
50+
HTTP_FLAVOR,
51+
HTTP_HOST,
52+
HTTP_METHOD,
53+
HTTP_SCHEME,
54+
HTTP_SERVER_NAME,
55+
HTTP_STATUS_CODE,
56+
HTTP_TARGET,
57+
HTTP_URL,
58+
)
59+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
60+
NET_HOST_PORT,
61+
)
4962
from opentelemetry.semconv.attributes.http_attributes import (
5063
HTTP_REQUEST_METHOD,
5164
HTTP_RESPONSE_STATUS_CODE,
@@ -55,7 +68,6 @@
5568
NETWORK_PROTOCOL_VERSION,
5669
)
5770
from opentelemetry.semconv.attributes.url_attributes import URL_SCHEME
58-
from opentelemetry.semconv.trace import SpanAttributes
5971
from opentelemetry.test.globals_test import reset_trace_globals
6072
from opentelemetry.test.test_base import TestBase
6173
from opentelemetry.util._importlib_metadata import entry_points
@@ -85,15 +97,15 @@
8597
"http.server.active_requests": _server_active_requests_count_attrs_old,
8698
"http.server.duration": {
8799
*_server_duration_attrs_old,
88-
SpanAttributes.HTTP_TARGET,
100+
HTTP_TARGET,
89101
},
90102
"http.server.response.size": {
91103
*_server_duration_attrs_old,
92-
SpanAttributes.HTTP_TARGET,
104+
HTTP_TARGET,
93105
},
94106
"http.server.request.size": {
95107
*_server_duration_attrs_old,
96-
SpanAttributes.HTTP_TARGET,
108+
HTTP_TARGET,
97109
},
98110
}
99111

@@ -244,23 +256,18 @@ def test_sub_app_fastapi_call(self):
244256
spans_with_http_attributes = [
245257
span
246258
for span in spans
247-
if (
248-
SpanAttributes.HTTP_URL in span.attributes
249-
or SpanAttributes.HTTP_TARGET in span.attributes
250-
)
259+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
251260
]
252261

253262
# We expect only one span to have the HTTP attributes set (the SERVER span from the app itself)
254263
# the sub app is not instrumented with manual instrumentation tests.
255264
self.assertEqual(1, len(spans_with_http_attributes))
256265

257266
for span in spans_with_http_attributes:
258-
self.assertEqual(
259-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
260-
)
267+
self.assertEqual("/sub/home", span.attributes[HTTP_TARGET])
261268
self.assertEqual(
262269
"https://testserver:443/sub/home",
263-
span.attributes[SpanAttributes.HTTP_URL],
270+
span.attributes[HTTP_URL],
264271
)
265272

266273

@@ -308,22 +315,17 @@ def test_sub_app_fastapi_call(self):
308315
spans_with_http_attributes = [
309316
span
310317
for span in spans
311-
if (
312-
SpanAttributes.HTTP_URL in span.attributes
313-
or SpanAttributes.HTTP_TARGET in span.attributes
314-
)
318+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
315319
]
316320

317321
# We now expect spans with attributes from both the app and its sub app
318322
self.assertEqual(2, len(spans_with_http_attributes))
319323

320324
for span in spans_with_http_attributes:
321-
self.assertEqual(
322-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
323-
)
325+
self.assertEqual("/sub/home", span.attributes[HTTP_TARGET])
324326
self.assertEqual(
325327
"https://testserver:443/sub/home",
326-
span.attributes[SpanAttributes.HTTP_URL],
328+
span.attributes[HTTP_URL],
327329
)
328330

329331

@@ -381,14 +383,10 @@ def test_fastapi_route_attribute_added(self):
381383
self.assertEqual(len(spans), 3)
382384
for span in spans:
383385
self.assertIn("GET /user/{username}", span.name)
384-
self.assertEqual(
385-
spans[-1].attributes[SpanAttributes.HTTP_ROUTE], "/user/{username}"
386-
)
386+
self.assertEqual(spans[-1].attributes[HTTP_ROUTE], "/user/{username}")
387387
# ensure that at least one attribute that is populated by
388388
# the asgi instrumentation is successfully feeding though.
389-
self.assertEqual(
390-
spans[-1].attributes[SpanAttributes.HTTP_FLAVOR], "1.1"
391-
)
389+
self.assertEqual(spans[-1].attributes[HTTP_FLAVOR], "1.1")
392390

393391
def test_fastapi_excluded_urls(self):
394392
"""Ensure that given fastapi routes are excluded."""
@@ -511,21 +509,21 @@ def test_basic_metric_success(self):
511509
self._client.get("/foobar")
512510
duration = max(round((default_timer() - start) * 1000), 0)
513511
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",
512+
HTTP_METHOD: "GET",
513+
HTTP_HOST: "testserver:443",
514+
HTTP_SCHEME: "https",
515+
HTTP_FLAVOR: "1.1",
516+
HTTP_SERVER_NAME: "testserver",
517+
NET_HOST_PORT: 443,
518+
HTTP_STATUS_CODE: 200,
519+
HTTP_TARGET: "/foobar",
522520
}
523521
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",
522+
HTTP_METHOD: "GET",
523+
HTTP_HOST: "testserver:443",
524+
HTTP_SCHEME: "https",
525+
HTTP_FLAVOR: "1.1",
526+
HTTP_SERVER_NAME: "testserver",
529527
}
530528
metrics_list = self.memory_metrics_reader.get_metrics_data()
531529
for metric in (
@@ -593,14 +591,14 @@ def test_basic_metric_success_both_semconv(self):
593591
duration = max(round((default_timer() - start) * 1000), 0)
594592
duration_s = max(default_timer() - start, 0)
595593
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",
594+
HTTP_METHOD: "GET",
595+
HTTP_HOST: "testserver:443",
596+
HTTP_SCHEME: "https",
597+
HTTP_FLAVOR: "1.1",
598+
HTTP_SERVER_NAME: "testserver",
599+
NET_HOST_PORT: 443,
600+
HTTP_STATUS_CODE: 200,
601+
HTTP_TARGET: "/foobar",
604602
}
605603
expected_duration_attributes_new = {
606604
HTTP_REQUEST_METHOD: "GET",
@@ -610,11 +608,11 @@ def test_basic_metric_success_both_semconv(self):
610608
HTTP_ROUTE: "/foobar",
611609
}
612610
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",
611+
HTTP_METHOD: "GET",
612+
HTTP_HOST: "testserver:443",
613+
HTTP_SCHEME: "https",
614+
HTTP_FLAVOR: "1.1",
615+
HTTP_SERVER_NAME: "testserver",
618616
HTTP_REQUEST_METHOD: "GET",
619617
URL_SCHEME: "https",
620618
}
@@ -676,21 +674,21 @@ def test_basic_metric_nonstandard_http_method_success(self):
676674
self._client.request("NONSTANDARD", "/foobar")
677675
duration = max(round((default_timer() - start) * 1000), 0)
678676
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",
677+
HTTP_METHOD: "_OTHER",
678+
HTTP_HOST: "testserver:443",
679+
HTTP_SCHEME: "https",
680+
HTTP_FLAVOR: "1.1",
681+
HTTP_SERVER_NAME: "testserver",
682+
NET_HOST_PORT: 443,
683+
HTTP_STATUS_CODE: 405,
684+
HTTP_TARGET: "/foobar",
687685
}
688686
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",
687+
HTTP_METHOD: "_OTHER",
688+
HTTP_HOST: "testserver:443",
689+
HTTP_SCHEME: "https",
690+
HTTP_FLAVOR: "1.1",
691+
HTTP_SERVER_NAME: "testserver",
694692
}
695693
metrics_list = self.memory_metrics_reader.get_metrics_data()
696694
for metric in (
@@ -758,14 +756,14 @@ def test_basic_metric_nonstandard_http_method_success_both_semconv(self):
758756
duration = max(round((default_timer() - start) * 1000), 0)
759757
duration_s = max(default_timer() - start, 0)
760758
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",
759+
HTTP_METHOD: "_OTHER",
760+
HTTP_HOST: "testserver:443",
761+
HTTP_SCHEME: "https",
762+
HTTP_FLAVOR: "1.1",
763+
HTTP_SERVER_NAME: "testserver",
764+
NET_HOST_PORT: 443,
765+
HTTP_STATUS_CODE: 405,
766+
HTTP_TARGET: "/foobar",
769767
}
770768
expected_duration_attributes_new = {
771769
HTTP_REQUEST_METHOD: "_OTHER",
@@ -775,11 +773,11 @@ def test_basic_metric_nonstandard_http_method_success_both_semconv(self):
775773
HTTP_ROUTE: "/foobar",
776774
}
777775
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",
776+
HTTP_METHOD: "_OTHER",
777+
HTTP_HOST: "testserver:443",
778+
HTTP_SCHEME: "https",
779+
HTTP_FLAVOR: "1.1",
780+
HTTP_SERVER_NAME: "testserver",
783781
HTTP_REQUEST_METHOD: "_OTHER",
784782
URL_SCHEME: "https",
785783
}
@@ -1203,22 +1201,17 @@ def test_sub_app_fastapi_call(self):
12031201
spans_with_http_attributes = [
12041202
span
12051203
for span in spans
1206-
if (
1207-
SpanAttributes.HTTP_URL in span.attributes
1208-
or SpanAttributes.HTTP_TARGET in span.attributes
1209-
)
1204+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
12101205
]
12111206

12121207
# We now expect spans with attributes from both the app and its sub app
12131208
self.assertEqual(2, len(spans_with_http_attributes))
12141209

12151210
for span in spans_with_http_attributes:
1216-
self.assertEqual(
1217-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
1218-
)
1211+
self.assertEqual("/sub/home", span.attributes[HTTP_TARGET])
12191212
self.assertEqual(
12201213
"https://testserver:443/sub/home",
1221-
span.attributes[SpanAttributes.HTTP_URL],
1214+
span.attributes[HTTP_URL],
12221215
)
12231216

12241217

@@ -1296,22 +1289,17 @@ def test_sub_app_fastapi_call(self):
12961289
spans_with_http_attributes = [
12971290
span
12981291
for span in spans
1299-
if (
1300-
SpanAttributes.HTTP_URL in span.attributes
1301-
or SpanAttributes.HTTP_TARGET in span.attributes
1302-
)
1292+
if (HTTP_URL in span.attributes or HTTP_TARGET in span.attributes)
13031293
]
13041294

13051295
# We now expect spans with attributes from both the app and its sub app
13061296
self.assertEqual(2, len(spans_with_http_attributes))
13071297

13081298
for span in spans_with_http_attributes:
1309-
self.assertEqual(
1310-
"/sub/home", span.attributes[SpanAttributes.HTTP_TARGET]
1311-
)
1299+
self.assertEqual("/sub/home", span.attributes[HTTP_TARGET])
13121300
self.assertEqual(
13131301
"https://testserver:443/sub/home",
1314-
span.attributes[SpanAttributes.HTTP_URL],
1302+
span.attributes[HTTP_URL],
13151303
)
13161304

13171305

0 commit comments

Comments
 (0)