Skip to content

Commit 117a6c9

Browse files
.
1 parent d1aa07c commit 117a6c9

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

sentry_sdk/client.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from importlib import import_module
88
from typing import TYPE_CHECKING, List, Dict, cast, overload
99
import warnings
10+
import json
1011

1112
from sentry_sdk._compat import check_uwsgi_thread_support
1213
from sentry_sdk._metrics_batcher import MetricsBatcher
@@ -27,10 +28,10 @@
2728
get_before_send_metric,
2829
has_logs_enabled,
2930
has_metrics_enabled,
30-
serialize_attribute,
3131
)
3232
from sentry_sdk.serializer import serialize
3333
from sentry_sdk.tracing import trace
34+
from sentry_sdk.traces import SpanStatus
3435
from sentry_sdk.tracing_utils import has_span_streaming_enabled
3536
from sentry_sdk.transport import (
3637
HttpTransportCore,
@@ -39,6 +40,7 @@
3940
)
4041
from sentry_sdk.consts import (
4142
SPANDATA,
43+
SPANSTATUS,
4244
DEFAULT_MAX_VALUE_LENGTH,
4345
DEFAULT_OPTIONS,
4446
INSTRUMENTER,
@@ -97,7 +99,7 @@ def _serialized_v1_span_to_serialized_v2_span(
9799
) -> "dict[str, Any]":
98100
# See SpanBatcher._to_transport_format() for analogous population of all entries except "attributes".
99101
res: "dict[str, Any]" = {
100-
"status": "ok",
102+
"status": SpanStatus.OK.value,
101103
"is_segment": False,
102104
}
103105

@@ -133,7 +135,7 @@ def _serialized_v1_span_to_serialized_v2_span(
133135
if "parent_span_id" in span:
134136
res["parent_span_id"] = span["parent_span_id"]
135137

136-
if "status" in span and span["status"] != "ok":
138+
if "status" in span and span["status"] != SPANSTATUS.OK:
137139
res["status"] = "error"
138140

139141
attributes: "Dict[str, Any]" = {}
@@ -180,8 +182,58 @@ def _serialized_v1_span_to_serialized_v2_span(
180182
if "version" in sdk_info:
181183
attributes["sentry.sdk.version"] = sdk_info["version"]
182184

183-
if attributes:
184-
res["attributes"] = {k: serialize_attribute(v) for k, v in attributes.items()}
185+
for key, value in attributes.items():
186+
serialized_value = serialize(value)
187+
if isinstance(serialized_value, bool):
188+
res.setdefault("attributes", {})[key] = {
189+
"value": serialized_value,
190+
"type": "boolean",
191+
}
192+
continue
193+
194+
if isinstance(serialized_value, int):
195+
res.setdefault("attributes", {})[key] = {
196+
"value": serialized_value,
197+
"type": "integer",
198+
}
199+
continue
200+
201+
if isinstance(serialized_value, float):
202+
res.setdefault("attributes", {})[key] = {
203+
"value": serialized_value,
204+
"type": "double",
205+
}
206+
continue
207+
208+
if isinstance(serialized_value, str):
209+
res.setdefault("attributes", {})[key] = {
210+
"value": serialized_value,
211+
"type": "string",
212+
}
213+
continue
214+
215+
if isinstance(serialized_value, list):
216+
if not serialized_value:
217+
res.setdefault("attributes", {})[key] = {"value": [], "type": "array"}
218+
219+
ty = type(serialized_value[0])
220+
if ty in (int, str, bool, float) and all(
221+
type(v) is ty for v in serialized_value
222+
):
223+
res.setdefault("attributes", {})[key] = {
224+
"value": serialized_value,
225+
"type": "array",
226+
}
227+
228+
continue
229+
230+
# Types returned when the serializer for V1 span attributes recurses into some container types.
231+
if isinstance(serialized_value, (dict, list)):
232+
res.setdefault("attributes", {})[key] = {
233+
"value": json.dumps(serialized_value),
234+
"type": "string",
235+
}
236+
continue
185237

186238
return res
187239

0 commit comments

Comments
 (0)