Skip to content

Mitigate CPU spikes when sending lots of events with lots of data #2449

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 9 commits into from
Oct 17, 2023
1 change: 1 addition & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"profiler_mode": Optional[ProfilerMode],
"otel_powered_performance": Optional[bool],
"transport_zlib_compression_level": Optional[int],
"transport_num_pools": Optional[int],
"enable_metrics": Optional[bool],
"before_emit_metric": Optional[Callable[[str, MetricTags], bool]],
},
Expand Down
15 changes: 9 additions & 6 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def __init__(
) # type: DefaultDict[Tuple[str, str], int]
self._last_client_report_sent = time.time()

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

num_pools = options.get("_experiments", {}).get("transport_num_pools")
self._num_pools = 2 if num_pools is None else int(num_pools)

self._pool = self._make_pool(
self.parsed_dsn,
http_proxy=options["http_proxy"],
Expand All @@ -165,11 +173,6 @@ 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 @@ -439,7 +442,7 @@ def _send_envelope(
def _get_pool_options(self, ca_certs):
# type: (Optional[Any]) -> Dict[str, Any]
return {
"num_pools": 2,
"num_pools": self._num_pools,
"cert_reqs": "CERT_REQUIRED",
"ca_certs": ca_certs or certifi.where(),
}
Expand Down
19 changes: 19 additions & 0 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ def test_transport_works(
assert any("Sending event" in record.msg for record in caplog.records) == debug


@pytest.mark.parametrize(
"num_pools,expected_num_pools",
(
(None, 2),
(2, 2),
(10, 10),
),
)
def test_transport_num_pools(make_client, num_pools, expected_num_pools):
_experiments = {}
if num_pools is not None:
_experiments["transport_num_pools"] = num_pools

client = make_client(_experiments=_experiments)

options = client.transport._get_pool_options([])
assert options["num_pools"] == expected_num_pools


def test_transport_infinite_loop(capturing_server, request, make_client):
client = make_client(
debug=True,
Expand Down