2323from opentelemetry .instrumentation import dbapi
2424from opentelemetry .instrumentation .utils import suppress_instrumentation
2525from opentelemetry .sdk import resources
26- from opentelemetry .semconv .trace import SpanAttributes
26+ from opentelemetry .semconv ._incubating .attributes import net_attributes
27+ from opentelemetry .semconv ._incubating .attributes .db_attributes import (
28+ DB_NAME ,
29+ DB_STATEMENT ,
30+ DB_SYSTEM ,
31+ DB_USER ,
32+ )
33+ from opentelemetry .semconv ._incubating .attributes .net_attributes import (
34+ NET_PEER_NAME ,
35+ NET_PEER_PORT ,
36+ )
2737from opentelemetry .test .test_base import TestBase
2838
2939
@@ -62,21 +72,13 @@ def test_span_succeeded(self):
6272 self .assertEqual (span .name , "Test" )
6373 self .assertIs (span .kind , trace_api .SpanKind .CLIENT )
6474
65- self .assertEqual (
66- span .attributes [SpanAttributes .DB_SYSTEM ], "testcomponent"
67- )
68- self .assertEqual (
69- span .attributes [SpanAttributes .DB_NAME ], "testdatabase"
70- )
71- self .assertEqual (
72- span .attributes [SpanAttributes .DB_STATEMENT ], "Test query"
73- )
75+ self .assertEqual (span .attributes [DB_SYSTEM ], "testcomponent" )
76+ self .assertEqual (span .attributes [DB_NAME ], "testdatabase" )
77+ self .assertEqual (span .attributes [DB_STATEMENT ], "Test query" )
7478 self .assertFalse ("db.statement.parameters" in span .attributes )
75- self .assertEqual (span .attributes [SpanAttributes .DB_USER ], "testuser" )
76- self .assertEqual (
77- span .attributes [SpanAttributes .NET_PEER_NAME ], "testhost"
78- )
79- self .assertEqual (span .attributes [SpanAttributes .NET_PEER_PORT ], 123 )
79+ self .assertEqual (span .attributes [DB_USER ], "testuser" )
80+ self .assertEqual (span .attributes [NET_PEER_NAME ], "testhost" )
81+ self .assertEqual (span .attributes [NET_PEER_PORT ], 123 )
8082 self .assertIs (span .status .status_code , trace_api .StatusCode .UNSET )
8183
8284 def test_span_name (self ):
@@ -136,24 +138,18 @@ def test_span_succeeded_with_capture_of_statement_parameters(self):
136138 self .assertEqual (span .name , "Test" )
137139 self .assertIs (span .kind , trace_api .SpanKind .CLIENT )
138140
139- self .assertEqual (
140- span .attributes [SpanAttributes .DB_SYSTEM ], "testcomponent"
141- )
142- self .assertEqual (
143- span .attributes [SpanAttributes .DB_NAME ], "testdatabase"
144- )
145- self .assertEqual (
146- span .attributes [SpanAttributes .DB_STATEMENT ], "Test query"
147- )
141+ self .assertEqual (span .attributes [DB_SYSTEM ], "testcomponent" )
142+ self .assertEqual (span .attributes [DB_NAME ], "testdatabase" )
143+ self .assertEqual (span .attributes [DB_STATEMENT ], "Test query" )
148144 self .assertEqual (
149145 span .attributes ["db.statement.parameters" ],
150146 "('param1Value', False)" ,
151147 )
152- self .assertEqual (span .attributes [SpanAttributes . DB_USER ], "testuser" )
148+ self .assertEqual (span .attributes [DB_USER ], "testuser" )
153149 self .assertEqual (
154- span .attributes [SpanAttributes .NET_PEER_NAME ], "testhost"
150+ span .attributes [net_attributes .NET_PEER_NAME ], "testhost"
155151 )
156- self .assertEqual (span .attributes [SpanAttributes .NET_PEER_PORT ], 123 )
152+ self .assertEqual (span .attributes [net_attributes .NET_PEER_PORT ], 123 )
157153 self .assertIs (span .status .status_code , trace_api .StatusCode .UNSET )
158154
159155 def test_span_not_recording (self ):
@@ -200,9 +196,7 @@ def test_span_failed(self):
200196 spans_list = self .memory_exporter .get_finished_spans ()
201197 self .assertEqual (len (spans_list ), 1 )
202198 span = spans_list [0 ]
203- self .assertEqual (
204- span .attributes [SpanAttributes .DB_STATEMENT ], "Test query"
205- )
199+ self .assertEqual (span .attributes [DB_STATEMENT ], "Test query" )
206200 self .assertIs (span .status .status_code , trace_api .StatusCode .ERROR )
207201 self .assertEqual (span .status .description , "Exception: Test Exception" )
208202
@@ -310,9 +304,7 @@ def test_executemany(self):
310304 spans_list = self .memory_exporter .get_finished_spans ()
311305 self .assertEqual (len (spans_list ), 1 )
312306 span = spans_list [0 ]
313- self .assertEqual (
314- span .attributes [SpanAttributes .DB_STATEMENT ], "Test query"
315- )
307+ self .assertEqual (span .attributes [DB_STATEMENT ], "Test query" )
316308
317309 def test_executemany_comment (self ):
318310 connect_module = mock .MagicMock ()
@@ -343,7 +335,7 @@ def test_executemany_comment(self):
343335 self .assertEqual (len (spans_list ), 1 )
344336 span = spans_list [0 ]
345337 self .assertEqual (
346- span .attributes [SpanAttributes . DB_STATEMENT ],
338+ span .attributes [DB_STATEMENT ],
347339 "Select 1;" ,
348340 )
349341
@@ -377,7 +369,7 @@ def test_executemany_comment_stmt_enabled(self):
377369 self .assertEqual (len (spans_list ), 1 )
378370 span = spans_list [0 ]
379371 self .assertRegex (
380- span .attributes [SpanAttributes . DB_STATEMENT ],
372+ span .attributes [DB_STATEMENT ],
381373 r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
382374 )
383375
@@ -413,7 +405,7 @@ def __getattr__(self, name):
413405 self .assertEqual (len (spans_list ), 1 )
414406 span = spans_list [0 ]
415407 self .assertEqual (
416- span .attributes [SpanAttributes . DB_STATEMENT ],
408+ span .attributes [DB_STATEMENT ],
417409 "Select 1;" ,
418410 )
419411
@@ -450,7 +442,7 @@ def __getattr__(self, name):
450442 self .assertEqual (len (spans_list ), 1 )
451443 span = spans_list [0 ]
452444 self .assertRegex (
453- span .attributes [SpanAttributes . DB_STATEMENT ],
445+ span .attributes [DB_STATEMENT ],
454446 r"Select 1 /\*dbapi_level='1.0',dbapi_threadsafety='unknown',driver_paramstyle='unknown',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
455447 )
456448
@@ -485,13 +477,13 @@ def test_executemany_comment_stmt_enabled_matches_db_statement_attribute(
485477 self .assertEqual (len (spans_list ), 1 )
486478 span = spans_list [0 ]
487479 self .assertRegex (
488- span .attributes [SpanAttributes . DB_STATEMENT ],
480+ span .attributes [DB_STATEMENT ],
489481 r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/" ,
490482 )
491483
492484 cursor_span_id = re .search (r"[a-zA-Z0-9_]{16}" , cursor .query ).group ()
493485 db_statement_span_id = re .search (
494- r"[a-zA-Z0-9_]{16}" , span .attributes [SpanAttributes . DB_STATEMENT ]
486+ r"[a-zA-Z0-9_]{16}" , span .attributes [DB_STATEMENT ]
495487 ).group ()
496488 self .assertEqual (cursor_span_id , db_statement_span_id )
497489
@@ -525,7 +517,7 @@ def test_compatible_build_version_psycopg2_libpq(self):
525517 self .assertEqual (len (spans_list ), 1 )
526518 span = spans_list [0 ]
527519 self .assertEqual (
528- span .attributes [SpanAttributes . DB_STATEMENT ],
520+ span .attributes [DB_STATEMENT ],
529521 "Select 1;" ,
530522 )
531523
@@ -558,7 +550,7 @@ def test_compatible_build_version_psycopg_libpq(self):
558550 self .assertEqual (len (spans_list ), 1 )
559551 span = spans_list [0 ]
560552 self .assertEqual (
561- span .attributes [SpanAttributes . DB_STATEMENT ],
553+ span .attributes [DB_STATEMENT ],
562554 "Select 1;" ,
563555 )
564556
@@ -595,7 +587,7 @@ def test_no_libpq_version_when_we_dont_instrument_the_root_module(
595587 self .assertEqual (len (spans_list ), 1 )
596588 span = spans_list [0 ]
597589 self .assertRegex (
598- span .attributes [SpanAttributes . DB_STATEMENT ],
590+ span .attributes [DB_STATEMENT ],
599591 r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
600592 )
601593
@@ -629,7 +621,7 @@ def test_executemany_psycopg2_integration_comment(self):
629621 self .assertEqual (len (spans_list ), 1 )
630622 span = spans_list [0 ]
631623 self .assertEqual (
632- span .attributes [SpanAttributes . DB_STATEMENT ],
624+ span .attributes [DB_STATEMENT ],
633625 "Select 1;" ,
634626 )
635627
@@ -664,7 +656,7 @@ def test_executemany_psycopg2_integration_comment_stmt_enabled(self):
664656 self .assertEqual (len (spans_list ), 1 )
665657 span = spans_list [0 ]
666658 self .assertRegex (
667- span .attributes [SpanAttributes . DB_STATEMENT ],
659+ span .attributes [DB_STATEMENT ],
668660 r"Select 1 /\*db_driver='psycopg2%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
669661 )
670662
@@ -697,7 +689,7 @@ def test_executemany_psycopg_integration_comment(self):
697689 self .assertEqual (len (spans_list ), 1 )
698690 span = spans_list [0 ]
699691 self .assertEqual (
700- span .attributes [SpanAttributes . DB_STATEMENT ],
692+ span .attributes [DB_STATEMENT ],
701693 "Select 1;" ,
702694 )
703695
@@ -731,7 +723,7 @@ def test_executemany_psycopg_integration_comment_stmt_enabled(self):
731723 self .assertEqual (len (spans_list ), 1 )
732724 span = spans_list [0 ]
733725 self .assertRegex (
734- span .attributes [SpanAttributes . DB_STATEMENT ],
726+ span .attributes [DB_STATEMENT ],
735727 r"Select 1 /\*db_driver='psycopg%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
736728 )
737729
@@ -764,7 +756,7 @@ def test_executemany_mysqlconnector_integration_comment(self):
764756 self .assertEqual (len (spans_list ), 1 )
765757 span = spans_list [0 ]
766758 self .assertEqual (
767- span .attributes [SpanAttributes . DB_STATEMENT ],
759+ span .attributes [DB_STATEMENT ],
768760 "Select 1;" ,
769761 )
770762
@@ -798,7 +790,7 @@ def test_executemany_mysqlconnector_integration_comment_stmt_enabled(self):
798790 self .assertEqual (len (spans_list ), 1 )
799791 span = spans_list [0 ]
800792 self .assertRegex (
801- span .attributes [SpanAttributes . DB_STATEMENT ],
793+ span .attributes [DB_STATEMENT ],
802794 r"Select 1 /\*db_driver='mysql.connector%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='1.2.3',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
803795 )
804796
@@ -840,7 +832,7 @@ def test_executemany_mysqlclient_integration_comment(
840832 self .assertEqual (len (spans_list ), 1 )
841833 span = spans_list [0 ]
842834 self .assertEqual (
843- span .attributes [SpanAttributes . DB_STATEMENT ],
835+ span .attributes [DB_STATEMENT ],
844836 "Select 1;" ,
845837 )
846838
@@ -883,7 +875,7 @@ def test_executemany_mysqlclient_integration_comment_stmt_enabled(
883875 self .assertEqual (len (spans_list ), 1 )
884876 span = spans_list [0 ]
885877 self .assertRegex (
886- span .attributes [SpanAttributes . DB_STATEMENT ],
878+ span .attributes [DB_STATEMENT ],
887879 r"Select 1 /\*db_driver='MySQLdb%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='123',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
888880 )
889881
@@ -917,7 +909,7 @@ def test_executemany_pymysql_integration_comment(self):
917909 self .assertEqual (len (spans_list ), 1 )
918910 span = spans_list [0 ]
919911 self .assertEqual (
920- span .attributes [SpanAttributes . DB_STATEMENT ],
912+ span .attributes [DB_STATEMENT ],
921913 "Select 1;" ,
922914 )
923915
@@ -952,7 +944,7 @@ def test_executemany_pymysql_integration_comment_stmt_enabled(self):
952944 self .assertEqual (len (spans_list ), 1 )
953945 span = spans_list [0 ]
954946 self .assertRegex (
955- span .attributes [SpanAttributes . DB_STATEMENT ],
947+ span .attributes [DB_STATEMENT ],
956948 r"Select 1 /\*db_driver='pymysql%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='123',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
957949 )
958950
@@ -990,7 +982,7 @@ def test_executemany_flask_integration_comment(self):
990982 self .assertEqual (len (spans_list ), 1 )
991983 span = spans_list [0 ]
992984 self .assertEqual (
993- span .attributes [SpanAttributes . DB_STATEMENT ],
985+ span .attributes [DB_STATEMENT ],
994986 "Select 1;" ,
995987 )
996988
@@ -1032,7 +1024,7 @@ def test_executemany_flask_integration_comment_stmt_enabled(self):
10321024 self .assertEqual (len (spans_list ), 1 )
10331025 span = spans_list [0 ]
10341026 self .assertRegex (
1035- span .attributes [SpanAttributes . DB_STATEMENT ],
1027+ span .attributes [DB_STATEMENT ],
10361028 r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',flask=1,libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
10371029 )
10381030
@@ -1051,7 +1043,7 @@ def test_callproc(self):
10511043 self .assertEqual (len (spans_list ), 1 )
10521044 span = spans_list [0 ]
10531045 self .assertEqual (
1054- span .attributes [SpanAttributes . DB_STATEMENT ],
1046+ span .attributes [DB_STATEMENT ],
10551047 "Test stored procedure" ,
10561048 )
10571049
0 commit comments