Skip to content

feat: Use most compact JSON encoding #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sentry_sdk/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sentry_sdk._compat import text_type
from sentry_sdk._types import MYPY
from sentry_sdk.sessions import Session
from sentry_sdk.utils import json_dumps

if MYPY:
from typing import Any
Expand Down Expand Up @@ -86,7 +87,7 @@ def serialize_into(
self, f # type: Any
):
# type: (...) -> None
f.write(json.dumps(self.headers, allow_nan=False).encode("utf-8"))
f.write(json_dumps(self.headers))
f.write(b"\n")
for item in self.items:
item.serialize_into(f)
Expand Down Expand Up @@ -142,7 +143,7 @@ def get_bytes(self):
with open(self.path, "rb") as f:
self.bytes = f.read()
elif self.json is not None:
self.bytes = json.dumps(self.json, allow_nan=False).encode("utf-8")
self.bytes = json_dumps(self.json)
else:
self.bytes = b""
return self.bytes
Expand Down Expand Up @@ -256,7 +257,7 @@ def serialize_into(
headers = dict(self.headers)
length, writer = self.payload._prepare_serialize()
headers["length"] = length
f.write(json.dumps(headers, allow_nan=False).encode("utf-8"))
f.write(json_dumps(headers))
f.write(b"\n")
writer(f)
f.write(b"\n")
Expand Down
5 changes: 2 additions & 3 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from __future__ import print_function

import json
import io
import urllib3 # type: ignore
import certifi
import gzip

from datetime import datetime, timedelta

from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions, json_dumps
from sentry_sdk.worker import BackgroundWorker
from sentry_sdk.envelope import Envelope, get_event_data_category

Expand Down Expand Up @@ -214,7 +213,7 @@ def _send_event(

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

assert self.parsed_dsn is not None
logger.debug(
Expand Down
11 changes: 9 additions & 2 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import sys
import json
import linecache
import logging
import os
import sys

from datetime import datetime

Expand Down Expand Up @@ -37,6 +38,12 @@
MAX_FORMAT_PARAM_LENGTH = 128


def json_dumps(data):
# type: (Any) -> bytes
"""Serialize data into a compact JSON representation encoded as UTF-8."""
return json.dumps(data, allow_nan=False, separators=(",", ":")).encode("utf-8")


def _get_debug_hub():
# type: () -> Optional[sentry_sdk.Hub]
# This function is replaced by debug.py
Expand Down