Skip to content

Commit 5142fdc

Browse files
committed
more stuff
1 parent 6e5be2e commit 5142fdc

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

sentry_sdk/integrations/asyncpg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22
import contextlib
3-
import json
43
from typing import Any, TypeVar, Callable, Awaitable, Iterator
54

65
import sentry_sdk
@@ -9,6 +8,7 @@
98
from sentry_sdk.tracing import Span
109
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries
1110
from sentry_sdk.utils import (
11+
_serialize_span_attribute,
1212
ensure_integration_enabled,
1313
parse_version,
1414
capture_internal_exceptions,
@@ -148,7 +148,7 @@ def _inner(*args: Any, **kwargs: Any) -> T: # noqa: N807
148148
) as span:
149149
_set_db_data(span, args[0])
150150
res = f(*args, **kwargs)
151-
span.set_attribute("db.cursor", json.dumps(res))
151+
span.set_attribute("db.cursor", _serialize_span_attribute(res))
152152

153153
return res
154154

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import json
2-
31
import sentry_sdk
42
from sentry_sdk.consts import OP, SPANDATA
53
from sentry_sdk.integrations import Integration, DidNotEnable
64
from sentry_sdk.tracing import Span
75
from sentry_sdk.scope import should_send_default_pii
8-
from sentry_sdk.utils import capture_internal_exceptions, ensure_integration_enabled
6+
from sentry_sdk.utils import (
7+
_serialize_span_attribute,
8+
capture_internal_exceptions,
9+
ensure_integration_enabled,
10+
)
911

1012
from typing import TYPE_CHECKING, TypeVar
1113

@@ -101,7 +103,7 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
101103

102104
if params and should_send_default_pii():
103105
connection._sentry_db_params = params
104-
span.set_attribute("db.params", json.dumps(params))
106+
span.set_attribute("db.params", _serialize_span_attribute(params))
105107

106108
# run the original code
107109
ret = f(*args, **kwargs)
@@ -119,7 +121,7 @@ def _inner_end(*args: P.args, **kwargs: P.kwargs) -> T:
119121

120122
if span is not None:
121123
if res is not None and should_send_default_pii():
122-
span.set_attribute("db.result", json.dumps(res))
124+
span.set_attribute("db.result", _serialize_span_attribute(res))
123125

124126
with capture_internal_exceptions():
125127
query = span.get_attribute("db.query.text")
@@ -161,7 +163,7 @@ def _inner_send_data(*args: P.args, **kwargs: P.kwargs) -> T:
161163
getattr(instance.connection, "_sentry_db_params", None) or []
162164
)
163165
db_params.extend(data)
164-
span.set_attribute("db.params", json.dumps(db_params))
166+
span.set_attribute("db.params", _serialize_span_attribute(db_params))
165167
try:
166168
del instance.connection._sentry_db_params
167169
except AttributeError:

sentry_sdk/tracing_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import contextlib
22
import inspect
3-
import json
43
import os
54
import re
65
import sys
@@ -24,6 +23,7 @@
2423
_is_external_source,
2524
_is_in_project_root,
2625
_module_in_list,
26+
_serialize_span_attribute,
2727
)
2828

2929
from typing import TYPE_CHECKING
@@ -134,13 +134,13 @@ def record_sql_queries(
134134

135135
data = {}
136136
if params_list is not None:
137-
data["db.params"] = json.dumps(params_list)
137+
data["db.params"] = _serialize_span_attribute(params_list)
138138
if paramstyle is not None:
139-
data["db.paramstyle"] = json.dumps(paramstyle)
139+
data["db.paramstyle"] = _serialize_span_attribute(paramstyle)
140140
if executemany:
141141
data["db.executemany"] = True
142142
if record_cursor_repr and cursor is not None:
143-
data["db.cursor"] = json.dumps(cursor)
143+
data["db.cursor"] = _serialize_span_attribute(cursor)
144144

145145
with capture_internal_exceptions():
146146
sentry_sdk.add_breadcrumb(message=query, category="query", data=data)

sentry_sdk/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,10 +1843,10 @@ def _serialize_span_attribute(value):
18431843
return value
18441844

18451845
# lists are allowed too, as long as they don't mix types
1846-
if isinstance(value, list):
1846+
if isinstance(value, (list, tuple)):
18471847
for type_ in (int, str, float, bool):
18481848
if all(isinstance(item, type_) for item in value):
1849-
return value
1849+
return list(value)
18501850

18511851
# if this is anything else, just try to coerce to string
18521852
# we prefer json.dumps since this makes things like dictionaries display

tests/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
_get_installed_modules,
3131
_generate_installed_modules,
3232
ensure_integration_enabled,
33+
_serialize_span_attribute,
3334
)
3435

3536

@@ -901,3 +902,28 @@ def test_format_timestamp_naive():
901902
# Ensure that some timestamp is returned, without error. We currently treat these as local time, but this is an
902903
# implementation detail which we should not assert here.
903904
assert re.fullmatch(timestamp_regex, format_timestamp(datetime_object))
905+
906+
907+
@pytest.mark.parametrize(
908+
("value", "result"),
909+
(
910+
("meow", "meow"),
911+
(1, 1),
912+
(47.0, 47.0),
913+
(True, True),
914+
(["meow", "bark"], ["meow", "bark"]),
915+
([True, False], [True, False]),
916+
([1, 2, 3], [1, 2, 3]),
917+
([46.5, 47.0, 47.5], [46.5, 47.0, 47.5]),
918+
(["meow", 47], '["meow", 47]'), # mixed types not allowed in a list
919+
(None, "null"),
920+
(
921+
{"cat": "meow", "dog": ["bark", "woof"]},
922+
'{"cat": "meow", "dog": ["bark", "woof"]}',
923+
),
924+
(datetime(2024, 1, 1), "2024-01-01 00:00:00"),
925+
(("meow", "purr"), ["meow", "purr"]),
926+
),
927+
)
928+
def test_serialize_span_attribute(value, result):
929+
assert _serialize_span_attribute(value) == result

0 commit comments

Comments
 (0)