Skip to content

Commit 6e5be2e

Browse files
committed
add util
1 parent 35d2b7a commit 6e5be2e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

sentry_sdk/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# Python 3.10 and below
2525
BaseExceptionGroup = None # type: ignore
2626

27+
from opentelemetry.util.types import AttributeValue
28+
2729
import sentry_sdk
2830
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, EndpointType
2931

@@ -1831,3 +1833,25 @@ def get_current_thread_meta(thread=None):
18311833

18321834
# we've tried everything, time to give up
18331835
return None, None
1836+
1837+
1838+
def _serialize_span_attribute(value):
1839+
# type: (Any) -> AttributeValue
1840+
"""Serialize an object so that it's OTel-compatible and displays nicely in Sentry."""
1841+
# check for allowed primitives
1842+
if isinstance(value, (int, str, float, bool)):
1843+
return value
1844+
1845+
# lists are allowed too, as long as they don't mix types
1846+
if isinstance(value, list):
1847+
for type_ in (int, str, float, bool):
1848+
if all(isinstance(item, type_) for item in value):
1849+
return value
1850+
1851+
# if this is anything else, just try to coerce to string
1852+
# we prefer json.dumps since this makes things like dictionaries display
1853+
# nicely in the UI
1854+
try:
1855+
return json.dumps(value)
1856+
except TypeError:
1857+
return str(value)

0 commit comments

Comments
 (0)