37
37
NumberDataPoint ,
38
38
)
39
39
from opentelemetry .sdk .resources import Resource
40
+ from opentelemetry .semconv ._incubating .attributes .http_attributes import (
41
+ HTTP_FLAVOR ,
42
+ HTTP_HOST ,
43
+ HTTP_METHOD ,
44
+ HTTP_SCHEME ,
45
+ HTTP_SERVER_NAME ,
46
+ HTTP_STATUS_CODE ,
47
+ HTTP_TARGET ,
48
+ HTTP_URL ,
49
+ HTTP_USER_AGENT ,
50
+ )
51
+ from opentelemetry .semconv ._incubating .attributes .net_attributes import (
52
+ NET_HOST_NAME ,
53
+ NET_HOST_PORT ,
54
+ )
40
55
from opentelemetry .semconv .attributes .http_attributes import (
41
56
HTTP_REQUEST_METHOD ,
42
57
HTTP_RESPONSE_STATUS_CODE ,
54
69
URL_QUERY ,
55
70
URL_SCHEME ,
56
71
)
57
- from opentelemetry .semconv .trace import SpanAttributes
72
+ from opentelemetry .semconv .attributes .user_agent_attributes import (
73
+ USER_AGENT_ORIGINAL ,
74
+ )
58
75
from opentelemetry .test .test_base import TestBase
59
76
from opentelemetry .test .wsgitestutil import WsgiTestBase
60
77
from opentelemetry .trace import StatusCode
@@ -245,14 +262,14 @@ def validate_response(
245
262
self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
246
263
expected_attributes = {}
247
264
expected_attributes_old = {
248
- SpanAttributes . HTTP_SERVER_NAME : "127.0.0.1" ,
249
- SpanAttributes . HTTP_SCHEME : "http" ,
250
- SpanAttributes . NET_HOST_PORT : 80 ,
251
- SpanAttributes . HTTP_HOST : "127.0.0.1" ,
252
- SpanAttributes . HTTP_FLAVOR : "1.0" ,
253
- SpanAttributes . HTTP_URL : "http://127.0.0.1/" ,
254
- SpanAttributes . HTTP_STATUS_CODE : 200 ,
255
- SpanAttributes . NET_HOST_NAME : "127.0.0.1" ,
265
+ HTTP_SERVER_NAME : "127.0.0.1" ,
266
+ HTTP_SCHEME : "http" ,
267
+ NET_HOST_PORT : 80 ,
268
+ HTTP_HOST : "127.0.0.1" ,
269
+ HTTP_FLAVOR : "1.0" ,
270
+ HTTP_URL : "http://127.0.0.1/" ,
271
+ HTTP_STATUS_CODE : 200 ,
272
+ NET_HOST_NAME : "127.0.0.1" ,
256
273
}
257
274
expected_attributes_new = {
258
275
SERVER_PORT : 80 ,
@@ -269,7 +286,7 @@ def validate_response(
269
286
expected_attributes .update (span_attributes or {})
270
287
if http_method is not None :
271
288
if old_sem_conv :
272
- expected_attributes [SpanAttributes . HTTP_METHOD ] = http_method
289
+ expected_attributes [HTTP_METHOD ] = http_method
273
290
if new_sem_conv :
274
291
expected_attributes [HTTP_REQUEST_METHOD ] = http_method
275
292
self .assertEqual (span_list [0 ].attributes , expected_attributes )
@@ -523,14 +540,14 @@ def test_request_attributes(self):
523
540
self .assertDictEqual (
524
541
attrs ,
525
542
{
526
- SpanAttributes . HTTP_METHOD : "GET" ,
527
- SpanAttributes . HTTP_HOST : "127.0.0.1" ,
528
- SpanAttributes . HTTP_URL : "http://127.0.0.1/?foo=bar" ,
529
- SpanAttributes . NET_HOST_PORT : 80 ,
530
- SpanAttributes . HTTP_SCHEME : "http" ,
531
- SpanAttributes . HTTP_SERVER_NAME : "127.0.0.1" ,
532
- SpanAttributes . HTTP_FLAVOR : "1.0" ,
533
- SpanAttributes . NET_HOST_NAME : "127.0.0.1" ,
543
+ HTTP_METHOD : "GET" ,
544
+ HTTP_HOST : "127.0.0.1" ,
545
+ HTTP_URL : "http://127.0.0.1/?foo=bar" ,
546
+ NET_HOST_PORT : 80 ,
547
+ HTTP_SCHEME : "http" ,
548
+ HTTP_SERVER_NAME : "127.0.0.1" ,
549
+ HTTP_FLAVOR : "1.0" ,
550
+ NET_HOST_NAME : "127.0.0.1" ,
534
551
},
535
552
)
536
553
@@ -565,10 +582,10 @@ def validate_url(
565
582
):
566
583
parts = urlsplit (expected_url )
567
584
expected_old = {
568
- SpanAttributes . HTTP_SCHEME : parts .scheme ,
569
- SpanAttributes . NET_HOST_PORT : parts .port
585
+ HTTP_SCHEME : parts .scheme ,
586
+ NET_HOST_PORT : parts .port
570
587
or (80 if parts .scheme == "http" else 443 ),
571
- SpanAttributes . HTTP_SERVER_NAME : parts .hostname , # Not true in the general case, but for all tests.
588
+ HTTP_SERVER_NAME : parts .hostname , # Not true in the general case, but for all tests.
572
589
}
573
590
expected_new = {
574
591
SERVER_PORT : parts .port or (80 if parts .scheme == "http" else 443 ),
@@ -578,13 +595,13 @@ def validate_url(
578
595
}
579
596
if old_semconv :
580
597
if raw :
581
- expected_old [SpanAttributes . HTTP_TARGET ] = expected_url .split (
598
+ expected_old [HTTP_TARGET ] = expected_url .split (
582
599
parts .netloc , 1
583
600
)[1 ]
584
601
else :
585
- expected_old [SpanAttributes . HTTP_URL ] = expected_url
602
+ expected_old [HTTP_URL ] = expected_url
586
603
if has_host :
587
- expected_old [SpanAttributes . HTTP_HOST ] = parts .hostname
604
+ expected_old [HTTP_HOST ] = parts .hostname
588
605
if new_semconv :
589
606
if raw :
590
607
expected_new [URL_PATH ] = expected_url .split (parts .path , 1 )[1 ]
@@ -713,9 +730,9 @@ def test_request_attributes_with_conflicting_nonstandard_port(self):
713
730
":8080" # Note that we do not correct SERVER_PORT
714
731
)
715
732
expected = {
716
- SpanAttributes . HTTP_HOST : "127.0.0.1:8080" ,
717
- SpanAttributes . HTTP_URL : "http://127.0.0.1:8080/" ,
718
- SpanAttributes . NET_HOST_PORT : 80 ,
733
+ HTTP_HOST : "127.0.0.1:8080" ,
734
+ HTTP_URL : "http://127.0.0.1:8080/" ,
735
+ NET_HOST_PORT : 80 ,
719
736
}
720
737
self .assertGreaterEqual (
721
738
otel_wsgi .collect_request_attributes (self .environ ).items (),
@@ -729,9 +746,7 @@ def test_request_attributes_with_faux_scheme_relative_raw_uri(self):
729
746
def test_request_attributes_pathless (self ):
730
747
self .environ ["RAW_URI" ] = ""
731
748
self .assertIsNone (
732
- otel_wsgi .collect_request_attributes (self .environ ).get (
733
- SpanAttributes .HTTP_TARGET
734
- )
749
+ otel_wsgi .collect_request_attributes (self .environ ).get (HTTP_TARGET )
735
750
)
736
751
737
752
def test_request_attributes_with_full_request_uri (self ):
@@ -741,8 +756,8 @@ def test_request_attributes_with_full_request_uri(self):
741
756
"http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing" # Might happen in a CONNECT request
742
757
)
743
758
expected_old = {
744
- SpanAttributes . HTTP_HOST : "127.0.0.1:8080" ,
745
- SpanAttributes . HTTP_TARGET : "http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing" ,
759
+ HTTP_HOST : "127.0.0.1:8080" ,
760
+ HTTP_TARGET : "http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing" ,
746
761
}
747
762
expected_new = {
748
763
URL_PATH : "/3/library/urllib.parse.html" ,
@@ -762,8 +777,8 @@ def test_request_attributes_with_full_request_uri(self):
762
777
763
778
def test_http_user_agent_attribute (self ):
764
779
self .environ ["HTTP_USER_AGENT" ] = "test-useragent"
765
- expected = {SpanAttributes . HTTP_USER_AGENT : "test-useragent" }
766
- expected_new = {SpanAttributes . USER_AGENT_ORIGINAL : "test-useragent" }
780
+ expected = {HTTP_USER_AGENT : "test-useragent" }
781
+ expected_new = {USER_AGENT_ORIGINAL : "test-useragent" }
767
782
self .assertGreaterEqual (
768
783
otel_wsgi .collect_request_attributes (self .environ ).items (),
769
784
expected .items (),
@@ -784,10 +799,8 @@ def test_response_attributes(self):
784
799
{},
785
800
sem_conv_opt_in_mode = _StabilityMode .HTTP ,
786
801
)
787
- expected = (mock .call (SpanAttributes .HTTP_STATUS_CODE , 404 ),)
788
- expected_new = (
789
- mock .call (SpanAttributes .HTTP_RESPONSE_STATUS_CODE , 404 ),
790
- )
802
+ expected = (mock .call (HTTP_STATUS_CODE , 404 ),)
803
+ expected_new = (mock .call (HTTP_RESPONSE_STATUS_CODE , 404 ),)
791
804
self .assertEqual (self .span .set_attribute .call_count , 2 )
792
805
self .span .set_attribute .assert_has_calls (expected , any_order = True )
793
806
self .span .set_attribute .assert_has_calls (expected_new , any_order = True )
@@ -803,7 +816,7 @@ def test_response_attributes_noop(self):
803
816
804
817
self .assertEqual (mock_span .set_attribute .call_count , 0 )
805
818
self .assertEqual (mock_span .is_recording .call_count , 2 )
806
- self .assertEqual (attrs [SpanAttributes . HTTP_STATUS_CODE ], 404 )
819
+ self .assertEqual (attrs [HTTP_STATUS_CODE ], 404 )
807
820
808
821
def test_remove_sensitive_params (self ):
809
822
self .environ ["HTTP_HOST" ] = "username:password@mock"
0 commit comments