22
33import pytest
44from opentelemetry .trace import SpanKind , Status , StatusCode
5+ from opentelemetry .version import __version__ as OTEL_VERSION
56
67from sentry_sdk .opentelemetry .utils import (
78 extract_span_data ,
89 extract_span_status ,
910 span_data_for_db_query ,
1011 span_data_for_http_method ,
1112)
13+ from sentry_sdk .utils import parse_version
14+
15+ OTEL_VERSION = parse_version (OTEL_VERSION )
1216
1317
1418@pytest .mark .parametrize (
@@ -276,6 +280,9 @@ def test_span_data_for_db_query():
276280 {
277281 "status" : "unavailable" ,
278282 "http_status_code" : 503 ,
283+ # old otel versions won't take the new attribute into account
284+ "status_old" : "internal_error" ,
285+ "http_status_code_old" : 502 ,
279286 },
280287 ),
281288 (
@@ -290,6 +297,9 @@ def test_span_data_for_db_query():
290297 {
291298 "status" : "unavailable" ,
292299 "http_status_code" : 503 ,
300+ # old otel versions won't take the new attribute into account
301+ "status_old" : "internal_error" ,
302+ "http_status_code_old" : 502 ,
293303 },
294304 ),
295305 (
@@ -311,6 +321,7 @@ def test_span_data_for_db_query():
311321 "http.method" : "POST" ,
312322 "http.route" : "/some/route" ,
313323 "http.response.status_code" : 200 ,
324+ "http.status_code" : 200 ,
314325 },
315326 {
316327 "status" : "ok" ,
@@ -326,6 +337,7 @@ def test_span_data_for_db_query():
326337 "http.method" : "POST" ,
327338 "http.route" : "/some/route" ,
328339 "http.response.status_code" : 401 ,
340+ "http.status_code" : 401 ,
329341 },
330342 {
331343 "status" : "unauthenticated" ,
@@ -339,6 +351,7 @@ def test_span_data_for_db_query():
339351 "http.method" : "POST" ,
340352 "http.route" : "/some/route" ,
341353 "http.response.status_code" : 418 ,
354+ "http.status_code" : 418 ,
342355 },
343356 {
344357 "status" : "invalid_argument" ,
@@ -372,4 +385,20 @@ def test_extract_span_status(kind, status, attributes, expected):
372385 "status" : status ,
373386 "http_status_code" : http_status_code ,
374387 }
388+
389+ if (
390+ OTEL_VERSION < (1 , 21 )
391+ and "status_old" in expected
392+ and "http_status_code_old" in expected
393+ ):
394+ expected = {
395+ "status" : expected ["status_old" ],
396+ "http_status_code" : expected ["http_status_code_old" ],
397+ }
398+ else :
399+ expected = {
400+ "status" : expected ["status" ],
401+ "http_status_code" : expected ["http_status_code" ],
402+ }
403+
375404 assert result == expected
0 commit comments