Skip to content

Commit eaf30e5

Browse files
committed
Simplify attributes typing and logic
1 parent eaf10fd commit eaf30e5

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

sentry_sdk/opentelemetry/utils.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from sentry_sdk._types import TYPE_CHECKING
3232

3333
if TYPE_CHECKING:
34-
from typing import Any, Optional, Mapping, Union, Type, TypeVar
34+
from typing import Any, Optional, Union, Type, TypeVar
35+
from opentelemetry.util.types import Attributes
3536

3637
T = TypeVar("T")
3738

@@ -128,24 +129,18 @@ def extract_span_data(span: ReadableSpan) -> ExtractedSpanData:
128129
Priority is given first to attributes explicitly defined by us via the SDK.
129130
Otherwise we try to infer sane values from other attributes.
130131
"""
131-
op = None
132-
description = None
133-
origin = None
134-
135-
if span.attributes is not None:
136-
op = get_typed_attribute(
137-
span.attributes, SentrySpanAttribute.OP, str
138-
) or infer_op(span)
139-
140-
description = (
141-
get_typed_attribute(span.attributes, SentrySpanAttribute.DESCRIPTION, str)
142-
or get_typed_attribute(span.attributes, SentrySpanAttribute.NAME, str)
143-
or infer_description(span)
144-
)
132+
op = get_typed_attribute(span.attributes, SentrySpanAttribute.OP, str) or infer_op(
133+
span
134+
)
135+
136+
description = (
137+
get_typed_attribute(span.attributes, SentrySpanAttribute.DESCRIPTION, str)
138+
or get_typed_attribute(span.attributes, SentrySpanAttribute.NAME, str)
139+
or infer_description(span)
140+
)
145141

146-
origin = get_typed_attribute(span.attributes, SentrySpanAttribute.ORIGIN, str)
142+
origin = get_typed_attribute(span.attributes, SentrySpanAttribute.ORIGIN, str)
147143

148-
# TODO status cleanup
149144
(status, http_status) = extract_span_status(span)
150145

151146
return ExtractedSpanData(
@@ -226,15 +221,14 @@ def extract_span_status(span: ReadableSpan) -> tuple[Optional[str], Optional[int
226221
on the description if it is a valid status for Sentry.
227222
In the final UNSET case, we try to infer HTTP/GRPC.
228223
"""
229-
span_attributes = span.attributes or {}
230224
status = span.status
231-
http_status = get_http_status_code(span_attributes)
225+
http_status = get_http_status_code(span.attributes)
232226
final_status = None
233227

234228
if status.status_code == StatusCode.OK:
235229
final_status = SPANSTATUS.OK
236230
elif status.status_code == StatusCode.ERROR:
237-
inferred_status = infer_status_from_attributes(span_attributes, http_status)
231+
inferred_status = infer_status_from_attributes(span.attributes, http_status)
238232

239233
if inferred_status is not None:
240234
final_status = inferred_status
@@ -247,14 +241,17 @@ def extract_span_status(span: ReadableSpan) -> tuple[Optional[str], Optional[int
247241
final_status = SPANSTATUS.UNKNOWN_ERROR
248242
else:
249243
# UNSET case
250-
final_status = infer_status_from_attributes(span_attributes, http_status)
244+
final_status = infer_status_from_attributes(span.attributes, http_status)
251245

252246
return (final_status, http_status)
253247

254248

255249
def infer_status_from_attributes(
256-
span_attributes: Mapping[str, Any], http_status: Optional[int]
250+
span_attributes: Attributes, http_status: Optional[int]
257251
) -> Optional[str]:
252+
if span_attributes is None:
253+
return None
254+
258255
if http_status:
259256
return get_span_status_from_http_code(http_status)
260257

@@ -265,7 +262,7 @@ def infer_status_from_attributes(
265262
return None
266263

267264

268-
def get_http_status_code(span_attributes: Mapping[str, Any]) -> Optional[int]:
265+
def get_http_status_code(span_attributes: Attributes) -> Optional[int]:
269266
try:
270267
http_status = get_typed_attribute(
271268
span_attributes, SpanAttributes.HTTP_RESPONSE_STATUS_CODE, int
@@ -458,12 +455,12 @@ def get_profile_context(span: ReadableSpan) -> Optional[dict[str, str]]:
458455
return {"profiler_id": profiler_id}
459456

460457

461-
def get_typed_attribute(
462-
attributes: Mapping[str, Any], key: str, type: Type[T]
463-
) -> Optional[T]:
458+
def get_typed_attribute(attributes: Attributes, key: str, type: Type[T]) -> Optional[T]:
464459
"""
465460
helper method to coerce types of attribute values
466461
"""
462+
if attributes is None:
463+
return None
467464
value = attributes.get(key)
468465
if value is not None and isinstance(value, type):
469466
return value

0 commit comments

Comments
 (0)