Skip to content

Commit 3d8a372

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

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

sentry_sdk/opentelemetry/utils.py

Lines changed: 23 additions & 25 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,22 +129,17 @@ 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

148144
# TODO status cleanup
149145
(status, http_status) = extract_span_status(span)
@@ -226,15 +222,14 @@ def extract_span_status(span: ReadableSpan) -> tuple[Optional[str], Optional[int
226222
on the description if it is a valid status for Sentry.
227223
In the final UNSET case, we try to infer HTTP/GRPC.
228224
"""
229-
span_attributes = span.attributes or {}
230225
status = span.status
231-
http_status = get_http_status_code(span_attributes)
226+
http_status = get_http_status_code(span.attributes)
232227
final_status = None
233228

234229
if status.status_code == StatusCode.OK:
235230
final_status = SPANSTATUS.OK
236231
elif status.status_code == StatusCode.ERROR:
237-
inferred_status = infer_status_from_attributes(span_attributes, http_status)
232+
inferred_status = infer_status_from_attributes(span.attributes, http_status)
238233

239234
if inferred_status is not None:
240235
final_status = inferred_status
@@ -247,14 +242,17 @@ def extract_span_status(span: ReadableSpan) -> tuple[Optional[str], Optional[int
247242
final_status = SPANSTATUS.UNKNOWN_ERROR
248243
else:
249244
# UNSET case
250-
final_status = infer_status_from_attributes(span_attributes, http_status)
245+
final_status = infer_status_from_attributes(span.attributes, http_status)
251246

252247
return (final_status, http_status)
253248

254249

255250
def infer_status_from_attributes(
256-
span_attributes: Mapping[str, Any], http_status: Optional[int]
251+
span_attributes: Attributes, http_status: Optional[int]
257252
) -> Optional[str]:
253+
if span_attributes is None:
254+
return None
255+
258256
if http_status:
259257
return get_span_status_from_http_code(http_status)
260258

@@ -265,7 +263,7 @@ def infer_status_from_attributes(
265263
return None
266264

267265

268-
def get_http_status_code(span_attributes: Mapping[str, Any]) -> Optional[int]:
266+
def get_http_status_code(span_attributes: Attributes) -> Optional[int]:
269267
try:
270268
http_status = get_typed_attribute(
271269
span_attributes, SpanAttributes.HTTP_RESPONSE_STATUS_CODE, int
@@ -458,12 +456,12 @@ def get_profile_context(span: ReadableSpan) -> Optional[dict[str, str]]:
458456
return {"profiler_id": profiler_id}
459457

460458

461-
def get_typed_attribute(
462-
attributes: Mapping[str, Any], key: str, type: Type[T]
463-
) -> Optional[T]:
459+
def get_typed_attribute(attributes: Attributes, key: str, type: Type[T]) -> Optional[T]:
464460
"""
465461
helper method to coerce types of attribute values
466462
"""
463+
if attributes is None:
464+
return None
467465
value = attributes.get(key)
468466
if value is not None and isinstance(value, type):
469467
return value

0 commit comments

Comments
 (0)