1212from opentelemetry .sdk .trace .export .in_memory_span_exporter import (
1313 InMemorySpanExporter ,
1414)
15- from opentelemetry .semconv .trace import SpanAttributes
15+ from opentelemetry .semconv ._incubating .attributes .db_attributes import (
16+ DB_NAME ,
17+ DB_STATEMENT ,
18+ DB_SYSTEM ,
19+ DB_USER ,
20+ )
21+ from opentelemetry .semconv ._incubating .attributes .net_attributes import (
22+ NET_PEER_NAME ,
23+ NET_PEER_PORT ,
24+ NET_TRANSPORT ,
25+ )
1626from opentelemetry .test .test_base import TestBase
1727from opentelemetry .trace import StatusCode
1828
@@ -31,19 +41,19 @@ def async_call(coro):
3141class CheckSpanMixin :
3242 def check_span (self , span , expected_db_name = POSTGRES_DB_NAME ):
3343 self .assertEqual (
34- span .attributes [SpanAttributes . DB_SYSTEM ], "postgresql"
44+ span .attributes [DB_SYSTEM ], "postgresql"
3545 )
3646 self .assertEqual (
37- span .attributes [SpanAttributes . DB_NAME ], expected_db_name
47+ span .attributes [DB_NAME ], expected_db_name
3848 )
3949 self .assertEqual (
40- span .attributes [SpanAttributes . DB_USER ], POSTGRES_USER
50+ span .attributes [DB_USER ], POSTGRES_USER
4151 )
4252 self .assertEqual (
43- span .attributes [SpanAttributes . NET_PEER_NAME ], POSTGRES_HOST
53+ span .attributes [NET_PEER_NAME ], POSTGRES_HOST
4454 )
4555 self .assertEqual (
46- span .attributes [SpanAttributes . NET_PEER_PORT ], POSTGRES_PORT
56+ span .attributes [NET_PEER_PORT ], POSTGRES_PORT
4757 )
4858
4959
@@ -75,7 +85,7 @@ def test_instrumented_execute_method_without_arguments(self, *_, **__):
7585 self .check_span (spans [0 ])
7686 self .assertEqual (spans [0 ].name , "SELECT" )
7787 self .assertEqual (
78- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT 42;"
88+ spans [0 ].attributes [DB_STATEMENT ], "SELECT 42;"
7989 )
8090
8191 def test_instrumented_execute_method_error (self , * _ , ** __ ):
@@ -87,7 +97,7 @@ def test_instrumented_execute_method_error(self, *_, **__):
8797 self .assertIs (StatusCode .ERROR , spans [0 ].status .status_code )
8898 self .check_span (spans [0 ])
8999 self .assertEqual (spans [0 ].name , POSTGRES_DB_NAME )
90- self .assertEqual (spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
100+ self .assertEqual (spans [0 ].attributes [DB_STATEMENT ], "" )
91101
92102 def test_instrumented_fetch_method_without_arguments (self , * _ , ** __ ):
93103 """Should create a span from fetch()."""
@@ -98,7 +108,7 @@ def test_instrumented_fetch_method_without_arguments(self, *_, **__):
98108 self .check_span (spans [0 ])
99109 self .assertEqual (spans [0 ].name , "SELECT" )
100110 self .assertEqual (
101- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT 42;"
111+ spans [0 ].attributes [DB_STATEMENT ], "SELECT 42;"
102112 )
103113
104114 def test_instrumented_fetch_method_empty_query (self , * _ , ** __ ):
@@ -109,7 +119,7 @@ def test_instrumented_fetch_method_empty_query(self, *_, **__):
109119 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
110120 self .check_span (spans [0 ])
111121 self .assertEqual (spans [0 ].name , POSTGRES_DB_NAME )
112- self .assertEqual (spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
122+ self .assertEqual (spans [0 ].attributes [DB_STATEMENT ], "" )
113123
114124 def test_instrumented_fetchval_method_without_arguments (self , * _ , ** __ ):
115125 """Should create a span for fetchval()."""
@@ -120,7 +130,7 @@ def test_instrumented_fetchval_method_without_arguments(self, *_, **__):
120130 self .check_span (spans [0 ])
121131 self .assertEqual (spans [0 ].name , "SELECT" )
122132 self .assertEqual (
123- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT 42;"
133+ spans [0 ].attributes [DB_STATEMENT ], "SELECT 42;"
124134 )
125135
126136 def test_instrumented_fetchval_method_empty_query (self , * _ , ** __ ):
@@ -131,7 +141,7 @@ def test_instrumented_fetchval_method_empty_query(self, *_, **__):
131141 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
132142 self .check_span (spans [0 ])
133143 self .assertEqual (spans [0 ].name , POSTGRES_DB_NAME )
134- self .assertEqual (spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
144+ self .assertEqual (spans [0 ].attributes [DB_STATEMENT ], "" )
135145
136146 def test_instrumented_fetchrow_method_without_arguments (self , * _ , ** __ ):
137147 """Should create a span for fetchrow()."""
@@ -142,7 +152,7 @@ def test_instrumented_fetchrow_method_without_arguments(self, *_, **__):
142152 self .check_span (spans [0 ])
143153 self .assertEqual (spans [0 ].name , "SELECT" )
144154 self .assertEqual (
145- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT 42;"
155+ spans [0 ].attributes [DB_STATEMENT ], "SELECT 42;"
146156 )
147157
148158 def test_instrumented_fetchrow_method_empty_query (self , * _ , ** __ ):
@@ -153,7 +163,7 @@ def test_instrumented_fetchrow_method_empty_query(self, *_, **__):
153163 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
154164 self .check_span (spans [0 ])
155165 self .assertEqual (spans [0 ].name , POSTGRES_DB_NAME )
156- self .assertEqual (spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
166+ self .assertEqual (spans [0 ].attributes [DB_STATEMENT ], "" )
157167
158168 def test_instrumented_cursor_execute_method_without_arguments (
159169 self , * _ , ** __
@@ -173,23 +183,23 @@ async def _cursor_execute():
173183 self .check_span (spans [0 ])
174184 self .assertEqual (spans [0 ].name , "BEGIN;" )
175185 self .assertEqual (
176- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "BEGIN;"
186+ spans [0 ].attributes [DB_STATEMENT ], "BEGIN;"
177187 )
178188 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
179189
180190 for span in spans [1 :- 1 ]:
181191 self .check_span (span )
182192 self .assertEqual (span .name , "CURSOR: SELECT" )
183193 self .assertEqual (
184- span .attributes [SpanAttributes . DB_STATEMENT ],
194+ span .attributes [DB_STATEMENT ],
185195 "SELECT generate_series(0, 5);" ,
186196 )
187197 self .assertIs (StatusCode .UNSET , span .status .status_code )
188198
189199 self .check_span (spans [- 1 ])
190200 self .assertEqual (spans [- 1 ].name , "COMMIT;" )
191201 self .assertEqual (
192- spans [- 1 ].attributes [SpanAttributes . DB_STATEMENT ], "COMMIT;"
202+ spans [- 1 ].attributes [DB_STATEMENT ], "COMMIT;"
193203 )
194204
195205 def test_instrumented_cursor_execute_method_empty_query (self , * _ , ** __ ):
@@ -206,18 +216,18 @@ async def _cursor_execute():
206216
207217 self .check_span (spans [0 ])
208218 self .assertEqual (
209- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "BEGIN;"
219+ spans [0 ].attributes [DB_STATEMENT ], "BEGIN;"
210220 )
211221 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
212222
213223 self .check_span (spans [1 ])
214224 self .assertEqual (spans [1 ].name , f"CURSOR: { POSTGRES_DB_NAME } " )
215- self .assertEqual (spans [1 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
225+ self .assertEqual (spans [1 ].attributes [DB_STATEMENT ], "" )
216226 self .assertIs (StatusCode .UNSET , spans [1 ].status .status_code )
217227
218228 self .check_span (spans [2 ])
219229 self .assertEqual (
220- spans [2 ].attributes [SpanAttributes . DB_STATEMENT ], "COMMIT;"
230+ spans [2 ].attributes [DB_STATEMENT ], "COMMIT;"
221231 )
222232
223233 def test_instrumented_remove_comments (self , * _ , ** __ ):
@@ -235,21 +245,21 @@ def test_instrumented_remove_comments(self, *_, **__):
235245 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
236246 self .assertEqual (spans [0 ].name , "SELECT" )
237247 self .assertEqual (
238- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ],
248+ spans [0 ].attributes [DB_STATEMENT ],
239249 "/* leading comment */ SELECT 42;" ,
240250 )
241251 self .check_span (spans [1 ])
242252 self .assertIs (StatusCode .UNSET , spans [1 ].status .status_code )
243253 self .assertEqual (spans [1 ].name , "SELECT" )
244254 self .assertEqual (
245- spans [1 ].attributes [SpanAttributes . DB_STATEMENT ],
255+ spans [1 ].attributes [DB_STATEMENT ],
246256 "/* leading comment */ SELECT 42; /* trailing comment */" ,
247257 )
248258 self .check_span (spans [2 ])
249259 self .assertIs (StatusCode .UNSET , spans [2 ].status .status_code )
250260 self .assertEqual (spans [2 ].name , "SELECT" )
251261 self .assertEqual (
252- spans [2 ].attributes [SpanAttributes . DB_STATEMENT ],
262+ spans [2 ].attributes [DB_STATEMENT ],
253263 "SELECT 42; /* trailing comment */" ,
254264 )
255265
@@ -266,19 +276,19 @@ async def _transaction_execute():
266276 self .assertEqual (3 , len (spans ))
267277 self .check_span (spans [0 ])
268278 self .assertEqual (
269- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "BEGIN;"
279+ spans [0 ].attributes [DB_STATEMENT ], "BEGIN;"
270280 )
271281 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
272282
273283 self .check_span (spans [1 ])
274284 self .assertEqual (
275- spans [1 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT 42;"
285+ spans [1 ].attributes [DB_STATEMENT ], "SELECT 42;"
276286 )
277287 self .assertIs (StatusCode .UNSET , spans [1 ].status .status_code )
278288
279289 self .check_span (spans [2 ])
280290 self .assertEqual (
281- spans [2 ].attributes [SpanAttributes . DB_STATEMENT ], "COMMIT;"
291+ spans [2 ].attributes [DB_STATEMENT ], "COMMIT;"
282292 )
283293 self .assertIs (StatusCode .UNSET , spans [2 ].status .status_code )
284294
@@ -297,20 +307,20 @@ async def _transaction_execute():
297307
298308 self .check_span (spans [0 ])
299309 self .assertEqual (
300- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "BEGIN;"
310+ spans [0 ].attributes [DB_STATEMENT ], "BEGIN;"
301311 )
302312 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
303313
304314 self .check_span (spans [1 ])
305315 self .assertEqual (
306- spans [1 ].attributes [SpanAttributes . DB_STATEMENT ],
316+ spans [1 ].attributes [DB_STATEMENT ],
307317 "SELECT 42::uuid;" ,
308318 )
309319 self .assertEqual (StatusCode .ERROR , spans [1 ].status .status_code )
310320
311321 self .check_span (spans [2 ])
312322 self .assertEqual (
313- spans [2 ].attributes [SpanAttributes . DB_STATEMENT ], "ROLLBACK;"
323+ spans [2 ].attributes [DB_STATEMENT ], "ROLLBACK;"
314324 )
315325 self .assertIs (StatusCode .UNSET , spans [2 ].status .status_code )
316326
@@ -322,7 +332,7 @@ def test_instrumented_method_doesnt_capture_parameters(self, *_, **__):
322332 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
323333 self .check_span (spans [0 ])
324334 self .assertEqual (
325- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT $1;"
335+ spans [0 ].attributes [DB_STATEMENT ], "SELECT $1;"
326336 )
327337
328338
@@ -357,7 +367,7 @@ def test_instrumented_execute_method_with_arguments(self, *_, **__):
357367 self .check_span (spans [0 ])
358368 self .assertEqual (spans [0 ].name , "SELECT" )
359369 self .assertEqual (
360- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT $1;"
370+ spans [0 ].attributes [DB_STATEMENT ], "SELECT $1;"
361371 )
362372 self .assertEqual (
363373 spans [0 ].attributes ["db.statement.parameters" ], "('1',)"
@@ -372,7 +382,7 @@ def test_instrumented_fetch_method_with_arguments(self, *_, **__):
372382 self .check_span (spans [0 ])
373383 self .assertEqual (spans [0 ].name , "SELECT" )
374384 self .assertEqual (
375- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT $1;"
385+ spans [0 ].attributes [DB_STATEMENT ], "SELECT $1;"
376386 )
377387 self .assertEqual (
378388 spans [0 ].attributes ["db.statement.parameters" ], "('1',)"
@@ -386,7 +396,7 @@ def test_instrumented_executemany_method_with_arguments(self, *_, **__):
386396 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
387397 self .check_span (spans [0 ])
388398 self .assertEqual (
389- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT $1;"
399+ spans [0 ].attributes [DB_STATEMENT ], "SELECT $1;"
390400 )
391401 self .assertEqual (
392402 spans [0 ].attributes ["db.statement.parameters" ], "([['1'], ['2']],)"
@@ -401,7 +411,7 @@ def test_instrumented_execute_interface_error_method(self, *_, **__):
401411 self .assertIs (StatusCode .ERROR , spans [0 ].status .status_code )
402412 self .check_span (spans [0 ])
403413 self .assertEqual (
404- spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "SELECT 42;"
414+ spans [0 ].attributes [DB_STATEMENT ], "SELECT 42;"
405415 )
406416 self .assertEqual (
407417 spans [0 ].attributes ["db.statement.parameters" ], "(1, 2, 3)"
@@ -415,7 +425,7 @@ def test_instrumented_executemany_method_empty_query(self, *_, **__):
415425 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
416426 self .check_span (spans [0 ])
417427 self .assertEqual (spans [0 ].name , POSTGRES_DB_NAME )
418- self .assertEqual (spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
428+ self .assertEqual (spans [0 ].attributes [DB_STATEMENT ], "" )
419429 self .assertEqual (
420430 spans [0 ].attributes ["db.statement.parameters" ], "([],)"
421431 )
@@ -430,7 +440,7 @@ def test_instrumented_fetch_method_broken_asyncpg(self, *_, **__):
430440 self .assertEqual (len (spans ), 1 )
431441 self .assertIs (StatusCode .UNSET , spans [0 ].status .status_code )
432442 self .assertEqual (spans [0 ].name , "postgresql" )
433- self .assertEqual (spans [0 ].attributes [SpanAttributes . DB_STATEMENT ], "" )
443+ self .assertEqual (spans [0 ].attributes [DB_STATEMENT ], "" )
434444
435445
436446class _FakeParams :
@@ -482,13 +492,13 @@ async def _fake_execute(*args, **kwargs):
482492 self .assertEqual (
483493 span .attributes ,
484494 {
485- SpanAttributes . DB_SYSTEM : "postgresql" ,
486- SpanAttributes . DB_NAME : "testdb" ,
487- SpanAttributes . DB_USER : "dbuser" ,
488- SpanAttributes . NET_PEER_NAME : "db.example.com" ,
489- SpanAttributes . NET_PEER_PORT : 5432 ,
490- SpanAttributes . NET_TRANSPORT : "ip_tcp" ,
491- SpanAttributes . DB_STATEMENT : "SELECT $1" ,
495+ DB_SYSTEM : "postgresql" ,
496+ DB_NAME : "testdb" ,
497+ DB_USER : "dbuser" ,
498+ NET_PEER_NAME : "db.example.com" ,
499+ NET_PEER_PORT : 5432 ,
500+ NET_TRANSPORT : "ip_tcp" ,
501+ DB_STATEMENT : "SELECT $1" ,
492502 "db.statement.parameters" : "('42',)" ,
493503 },
494504 )
@@ -516,13 +526,13 @@ async def _fake_cursor_execute(*args, **kwargs):
516526 self .assertEqual (
517527 span .attributes ,
518528 {
519- SpanAttributes . DB_SYSTEM : "postgresql" ,
520- SpanAttributes . DB_NAME : "testdb" ,
521- SpanAttributes . DB_USER : "dbuser" ,
522- SpanAttributes . NET_PEER_NAME : "db.example.com" ,
523- SpanAttributes . NET_PEER_PORT : 5432 ,
524- SpanAttributes . NET_TRANSPORT : "ip_tcp" ,
525- SpanAttributes . DB_STATEMENT : "SELECT $1" ,
529+ DB_SYSTEM : "postgresql" ,
530+ DB_NAME : "testdb" ,
531+ DB_USER : "dbuser" ,
532+ NET_PEER_NAME : "db.example.com" ,
533+ NET_PEER_PORT : 5432 ,
534+ NET_TRANSPORT : "ip_tcp" ,
535+ DB_STATEMENT : "SELECT $1" ,
526536 "db.statement.parameters" : "('99',)" ,
527537 },
528538 )
0 commit comments