Skip to content

feat(transport): Added configurable compression levels #2382

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 3 commits into from
Sep 19, 2023
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
1 change: 1 addition & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"profiles_sample_rate": Optional[float],
"profiler_mode": Optional[ProfilerMode],
"otel_powered_performance": Optional[bool],
"transport_zlib_compression_level": Optional[int],
},
total=False,
)
Expand Down
46 changes: 34 additions & 12 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def __init__(
proxy_headers=options["proxy_headers"],
)

compresslevel = options.get("_experiments", {}).get(
"transport_zlib_compression_level"
)
self._compresslevel = 9 if compresslevel is None else int(compresslevel)

from sentry_sdk import Hub

self.hub_cls = Hub
Expand Down Expand Up @@ -338,8 +343,13 @@ def _send_event(
return None

body = io.BytesIO()
with gzip.GzipFile(fileobj=body, mode="w") as f:
f.write(json_dumps(event))
if self._compresslevel == 0:
body.write(json_dumps(event))
else:
with gzip.GzipFile(
fileobj=body, mode="w", compresslevel=self._compresslevel
) as f:
f.write(json_dumps(event))

assert self.parsed_dsn is not None
logger.debug(
Expand All @@ -352,10 +362,14 @@ def _send_event(
self.parsed_dsn.host,
)
)
self._send_request(
body.getvalue(),
headers={"Content-Type": "application/json", "Content-Encoding": "gzip"},
)

headers = {
"Content-Type": "application/json",
}
if self._compresslevel > 0:
headers["Content-Encoding"] = "gzip"

self._send_request(body.getvalue(), headers=headers)
return None

def _send_envelope(
Expand Down Expand Up @@ -390,8 +404,13 @@ def _send_envelope(
envelope.items.append(client_report_item)

body = io.BytesIO()
with gzip.GzipFile(fileobj=body, mode="w") as f:
envelope.serialize_into(f)
if self._compresslevel == 0:
envelope.serialize_into(body)
else:
with gzip.GzipFile(
fileobj=body, mode="w", compresslevel=self._compresslevel
) as f:
envelope.serialize_into(f)

assert self.parsed_dsn is not None
logger.debug(
Expand All @@ -401,12 +420,15 @@ def _send_envelope(
self.parsed_dsn.host,
)

headers = {
"Content-Type": "application/x-sentry-envelope",
}
if self._compresslevel > 0:
headers["Content-Encoding"] = "gzip"

self._send_request(
body.getvalue(),
headers={
"Content-Type": "application/x-sentry-envelope",
"Content-Encoding": "gzip",
},
headers=headers,
endpoint_type="envelope",
envelope=envelope,
)
Expand Down
32 changes: 25 additions & 7 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from sentry_sdk.integrations.logging import LoggingIntegration


CapturedData = namedtuple("CapturedData", ["path", "event", "envelope"])
CapturedData = namedtuple("CapturedData", ["path", "event", "envelope", "compressed"])


class CapturingServer(WSGIServer):
Expand All @@ -42,15 +42,25 @@ def __call__(self, environ, start_response):
"""
request = Request(environ)
event = envelope = None
if request.headers.get("content-encoding") == "gzip":
rdr = gzip.GzipFile(fileobj=io.BytesIO(request.data))
compressed = True
else:
rdr = io.BytesIO(request.data)
compressed = False

if request.mimetype == "application/json":
event = parse_json(gzip.GzipFile(fileobj=io.BytesIO(request.data)).read())
event = parse_json(rdr.read())
else:
envelope = Envelope.deserialize_from(
gzip.GzipFile(fileobj=io.BytesIO(request.data))
)
envelope = Envelope.deserialize_from(rdr)

self.captured.append(
CapturedData(path=request.path, event=event, envelope=envelope)
CapturedData(
path=request.path,
event=event,
envelope=envelope,
compressed=compressed,
)
)

response = Response(status=self.code)
Expand Down Expand Up @@ -81,6 +91,7 @@ def inner(**kwargs):
@pytest.mark.parametrize("debug", (True, False))
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
@pytest.mark.parametrize("use_pickle", (True, False))
@pytest.mark.parametrize("compressionlevel", (0, 9))
def test_transport_works(
capturing_server,
request,
Expand All @@ -90,10 +101,16 @@ def test_transport_works(
make_client,
client_flush_method,
use_pickle,
compressionlevel,
maybe_monkeypatched_threading,
):
caplog.set_level(logging.DEBUG)
client = make_client(debug=debug)
client = make_client(
debug=debug,
_experiments={
"transport_zlib_compression_level": compressionlevel,
},
)

if use_pickle:
client = pickle.loads(pickle.dumps(client))
Expand All @@ -109,6 +126,7 @@ def test_transport_works(
out, err = capsys.readouterr()
assert not err and not out
assert capturing_server.captured
assert capturing_server.captured[0].compressed == (compressionlevel > 0)

assert any("Sending event" in record.msg for record in caplog.records) == debug

Expand Down