Skip to content

Commit fee865c

Browse files
authored
Make debug option also configurable via environment (#2450)
Introducing new SENTRY_DEBUG environment variable that can be used to set the debug option in sentry_sdk.init().
1 parent f570a99 commit fee865c

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

sentry_sdk/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ def _get_options(*args, **kwargs):
109109
if rv["environment"] is None:
110110
rv["environment"] = os.environ.get("SENTRY_ENVIRONMENT") or "production"
111111

112+
if rv["debug"] is None:
113+
rv["debug"] = os.environ.get("SENTRY_DEBUG", "False").lower() in (
114+
"true",
115+
"1",
116+
"t",
117+
)
118+
112119
if rv["server_name"] is None and hasattr(socket, "gethostname"):
113120
rv["server_name"] = socket.gethostname()
114121

sentry_sdk/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def __init__(
233233
max_request_body_size="medium", # type: str
234234
before_send=None, # type: Optional[EventProcessor]
235235
before_breadcrumb=None, # type: Optional[BreadcrumbProcessor]
236-
debug=False, # type: bool
236+
debug=None, # type: Optional[bool]
237237
attach_stacktrace=False, # type: bool
238238
ca_certs=None, # type: Optional[str]
239239
propagate_traces=True, # type: bool

tests/test_client.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,3 +1136,63 @@ def test_max_value_length_option(
11361136
capture_message("a" * 2000)
11371137

11381138
assert len(events[0]["message"]) == expected_data_length
1139+
1140+
1141+
@pytest.mark.parametrize(
1142+
"client_option,env_var_value,debug_output_expected",
1143+
[
1144+
(None, "", False),
1145+
(None, "t", True),
1146+
(None, "1", True),
1147+
(None, "True", True),
1148+
(None, "true", True),
1149+
(None, "f", False),
1150+
(None, "0", False),
1151+
(None, "False", False),
1152+
(None, "false", False),
1153+
(None, "xxx", False),
1154+
(True, "", True),
1155+
(True, "t", True),
1156+
(True, "1", True),
1157+
(True, "True", True),
1158+
(True, "true", True),
1159+
(True, "f", True),
1160+
(True, "0", True),
1161+
(True, "False", True),
1162+
(True, "false", True),
1163+
(True, "xxx", True),
1164+
(False, "", False),
1165+
(False, "t", False),
1166+
(False, "1", False),
1167+
(False, "True", False),
1168+
(False, "true", False),
1169+
(False, "f", False),
1170+
(False, "0", False),
1171+
(False, "False", False),
1172+
(False, "false", False),
1173+
(False, "xxx", False),
1174+
],
1175+
)
1176+
@pytest.mark.tests_internal_exceptions
1177+
def test_debug_option(
1178+
sentry_init,
1179+
monkeypatch,
1180+
caplog,
1181+
client_option,
1182+
env_var_value,
1183+
debug_output_expected,
1184+
):
1185+
monkeypatch.setenv("SENTRY_DEBUG", env_var_value)
1186+
1187+
if client_option is None:
1188+
sentry_init()
1189+
else:
1190+
sentry_init(debug=client_option)
1191+
1192+
Hub.current._capture_internal_exception(
1193+
(ValueError, ValueError("something is wrong"), None)
1194+
)
1195+
if debug_output_expected:
1196+
assert "something is wrong" in caplog.text
1197+
else:
1198+
assert "something is wrong" not in caplog.text

0 commit comments

Comments
 (0)