Skip to content

feat: Allow configuring keep_alive via environment variable #4366

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
May 19, 2025
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
5 changes: 5 additions & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ def _get_options(*args, **kwargs):
)
rv["socket_options"] = None

if rv["keep_alive"] is None:
rv["keep_alive"] = (
env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE"), strict=True) or False
)

if rv["enable_tracing"] is not None:
warnings.warn(
"The `enable_tracing` parameter is deprecated. Please use `traces_sample_rate` instead.",
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def __init__(
ignore_errors=[], # type: Sequence[Union[type, str]] # noqa: B006
max_request_body_size="medium", # type: str
socket_options=None, # type: Optional[List[Tuple[int, int, int | bytes]]]
keep_alive=False, # type: bool
keep_alive=None, # type: Optional[bool]
before_send=None, # type: Optional[EventProcessor]
before_breadcrumb=None, # type: Optional[BreadcrumbProcessor]
debug=None, # type: Optional[bool]
Expand Down
64 changes: 64 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
import json
import subprocess
Expand Down Expand Up @@ -1496,3 +1497,66 @@ def test_dropped_transaction(sentry_init, capture_record_lost_event_calls, test_
def test_enable_tracing_deprecated(sentry_init, enable_tracing):
with pytest.warns(DeprecationWarning):
sentry_init(enable_tracing=enable_tracing)


def make_options_transport_cls():
"""Make an options transport class that captures the options passed to it."""
# We need a unique class for each test so that the options are not
# shared between tests.

class OptionsTransport(Transport):
"""Transport that captures the options passed to it."""

def __init__(self, options):
super().__init__(options)
type(self).options = options

def capture_envelope(self, _):
pass

return OptionsTransport


@contextlib.contextmanager
def clear_env_var(name):
"""Helper to clear the a given environment variable,
and restore it to its original value on exit."""
old_value = os.environ.pop(name, None)

try:
yield
finally:
if old_value is not None:
os.environ[name] = old_value
elif name in os.environ:
del os.environ[name]


@pytest.mark.parametrize(
("env_value", "arg_value", "expected_value"),
[
(None, None, False), # default
("0", None, False), # env var false
("1", None, True), # env var true
(None, False, False), # arg false
(None, True, True), # arg true
# Argument overrides environment variable
("0", True, True), # env false, arg true
("1", False, False), # env true, arg false
],
)
def test_keep_alive(env_value, arg_value, expected_value):
transport_cls = make_options_transport_cls()
keep_alive_kwarg = {} if arg_value is None else {"keep_alive": arg_value}

with clear_env_var("SENTRY_KEEP_ALIVE"):
if env_value is not None:
os.environ["SENTRY_KEEP_ALIVE"] = env_value

sentry_sdk.init(
dsn="http://[email protected]/123",
transport=transport_cls,
**keep_alive_kwarg,
)

assert transport_cls.options["keep_alive"] is expected_value
Loading