Skip to content

Commit f9cf4fb

Browse files
feat: Allow configuring keep_alive via environment variable
This commit enables the `keep_alive` option to be set via the `SENTRY_KEEP_ALIVE` environment variable. When both the environment variable and the argument are provided, the argument takes precedence. Closes #4354
1 parent 18a1104 commit f9cf4fb

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

sentry_sdk/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ def _get_options(*args, **kwargs):
142142
)
143143
rv["socket_options"] = None
144144

145+
if rv["keep_alive"] is None:
146+
rv["keep_alive"] = (
147+
env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE"), strict=True) or False
148+
)
149+
145150
if rv["enable_tracing"] is not None:
146151
warnings.warn(
147152
"The `enable_tracing` parameter is deprecated. Please use `traces_sample_rate` instead.",

sentry_sdk/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def __init__(
518518
ignore_errors=[], # type: Sequence[Union[type, str]] # noqa: B006
519519
max_request_body_size="medium", # type: str
520520
socket_options=None, # type: Optional[List[Tuple[int, int, int | bytes]]]
521-
keep_alive=False, # type: bool
521+
keep_alive=None, # type: Optional[bool]
522522
before_send=None, # type: Optional[EventProcessor]
523523
before_breadcrumb=None, # type: Optional[BreadcrumbProcessor]
524524
debug=None, # type: Optional[bool]

tests/test_client.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import os
23
import json
34
import subprocess
@@ -1496,3 +1497,66 @@ def test_dropped_transaction(sentry_init, capture_record_lost_event_calls, test_
14961497
def test_enable_tracing_deprecated(sentry_init, enable_tracing):
14971498
with pytest.warns(DeprecationWarning):
14981499
sentry_init(enable_tracing=enable_tracing)
1500+
1501+
1502+
def make_options_transport_cls():
1503+
"""Make an options transport class that captures the options passed to it."""
1504+
# We need a unique class for each test so that the options are not
1505+
# shared between tests.
1506+
1507+
class OptionsTransport(Transport):
1508+
"""Transport that captures the options passed to it."""
1509+
1510+
def __init__(self, options):
1511+
super().__init__(options)
1512+
type(self).options = options
1513+
1514+
def capture_envelope(self, _):
1515+
pass
1516+
1517+
return OptionsTransport
1518+
1519+
1520+
@contextlib.contextmanager
1521+
def clear_env_var(name):
1522+
"""Helper to clear the a given environment variable,
1523+
and restore it to its original value on exit."""
1524+
old_value = os.environ.pop(name, None)
1525+
1526+
try:
1527+
yield
1528+
finally:
1529+
if old_value is not None:
1530+
os.environ[name] = old_value
1531+
elif name in os.environ:
1532+
del os.environ[name]
1533+
1534+
1535+
@pytest.mark.parametrize(
1536+
("env_value", "arg_value", "expected_value"),
1537+
[
1538+
(None, None, False), # default
1539+
("0", None, False), # env var false
1540+
("1", None, True), # env var true
1541+
(None, False, False), # arg false
1542+
(None, True, True), # arg true
1543+
# Argument overrides environment variable
1544+
("0", True, True), # env false, arg true
1545+
("1", False, False), # env true, arg false
1546+
],
1547+
)
1548+
def test_keep_alive(env_value, arg_value, expected_value):
1549+
transport_cls = make_options_transport_cls()
1550+
keep_alive_kwarg = {} if arg_value is None else {"keep_alive": arg_value}
1551+
1552+
with clear_env_var("SENTRY_KEEP_ALIVE"):
1553+
if env_value is not None:
1554+
os.environ["SENTRY_KEEP_ALIVE"] = env_value
1555+
1556+
sentry_sdk.init(
1557+
dsn="http://[email protected]/123",
1558+
transport=transport_cls,
1559+
**keep_alive_kwarg,
1560+
)
1561+
1562+
assert transport_cls.options["keep_alive"] is expected_value

0 commit comments

Comments
 (0)