Skip to content

Commit 7d482b5

Browse files
authored
feat: Use most compact JSON encoding (getsentry#746)
This shrinks event sizes a bit, even when gzip'ed. The compact representation is documented in the json module. Alternatively, we can also look into using a custom encoder (that could also handle datetime objects, instead of the current manual serialization of those). In the absence of proper benchmark data, consider a random transaction event t: >>> len(json.dumps(t)), len(json.dumps(t, separators=(',', ':'))) (82174, 78516) That is 95.5% of the original size. With gzip compression: >>> len(gzips(json.dumps(t))), len(gzips(json.dumps(t, separators=(',', ':')))) (13093, 12988) That is 99.2% of the original size.
1 parent ab3da08 commit 7d482b5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

sentry_sdk/envelope.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sentry_sdk._compat import text_type
77
from sentry_sdk._types import MYPY
88
from sentry_sdk.sessions import Session
9+
from sentry_sdk.utils import json_dumps
910

1011
if MYPY:
1112
from typing import Any
@@ -86,7 +87,7 @@ def serialize_into(
8687
self, f # type: Any
8788
):
8889
# type: (...) -> None
89-
f.write(json.dumps(self.headers, allow_nan=False).encode("utf-8"))
90+
f.write(json_dumps(self.headers))
9091
f.write(b"\n")
9192
for item in self.items:
9293
item.serialize_into(f)
@@ -142,7 +143,7 @@ def get_bytes(self):
142143
with open(self.path, "rb") as f:
143144
self.bytes = f.read()
144145
elif self.json is not None:
145-
self.bytes = json.dumps(self.json, allow_nan=False).encode("utf-8")
146+
self.bytes = json_dumps(self.json)
146147
else:
147148
self.bytes = b""
148149
return self.bytes
@@ -256,7 +257,7 @@ def serialize_into(
256257
headers = dict(self.headers)
257258
length, writer = self.payload._prepare_serialize()
258259
headers["length"] = length
259-
f.write(json.dumps(headers, allow_nan=False).encode("utf-8"))
260+
f.write(json_dumps(headers))
260261
f.write(b"\n")
261262
writer(f)
262263
f.write(b"\n")

sentry_sdk/transport.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from __future__ import print_function
22

3-
import json
43
import io
54
import urllib3 # type: ignore
65
import certifi
76
import gzip
87

98
from datetime import datetime, timedelta
109

11-
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions
10+
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions, json_dumps
1211
from sentry_sdk.worker import BackgroundWorker
1312
from sentry_sdk.envelope import Envelope, get_event_data_category
1413

@@ -214,7 +213,7 @@ def _send_event(
214213

215214
body = io.BytesIO()
216215
with gzip.GzipFile(fileobj=body, mode="w") as f:
217-
f.write(json.dumps(event, allow_nan=False).encode("utf-8"))
216+
f.write(json_dumps(event))
218217

219218
assert self.parsed_dsn is not None
220219
logger.debug(

sentry_sdk/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import os
2-
import sys
1+
import json
32
import linecache
43
import logging
4+
import os
5+
import sys
56

67
from datetime import datetime
78

@@ -37,6 +38,12 @@
3738
MAX_FORMAT_PARAM_LENGTH = 128
3839

3940

41+
def json_dumps(data):
42+
# type: (Any) -> bytes
43+
"""Serialize data into a compact JSON representation encoded as UTF-8."""
44+
return json.dumps(data, allow_nan=False, separators=(",", ":")).encode("utf-8")
45+
46+
4047
def _get_debug_hub():
4148
# type: () -> Optional[sentry_sdk.Hub]
4249
# This function is replaced by debug.py

0 commit comments

Comments
 (0)