Skip to content

Commit e968394

Browse files
committed
maybe fix
1 parent f10d404 commit e968394

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

sentry_sdk/opentelemetry/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ def get_http_status_code(span_attributes):
286286
http_status = span_attributes.get(SpanAttributes.HTTP_RESPONSE_STATUS_CODE)
287287
except AttributeError:
288288
# HTTP_RESPONSE_STATUS_CODE was added in 1.21, so if we're on an older
289-
# OTel version this will not work
289+
# OTel version SpanAttributes.HTTP_RESPONSE_STATUS_CODE will throw an
290+
# AttributeError
290291
http_status = None
291292

292293
if http_status is None:

tests/opentelemetry/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import pytest
44
from opentelemetry.trace import SpanKind, Status, StatusCode
5+
from opentelemetry.version import __version__ as OTEL_VERSION
56

67
from 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

Comments
 (0)