Skip to content

Commit ba6cda7

Browse files
committed
feat: default telemetry off in non-prod envs
Disable telemetry in non-production environments by default.
1 parent 6d0225d commit ba6cda7

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

plain2code_telemetry.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Crash reporting via Sentry.
22
33
Only unexpected exceptions are reported (the caller decides which exceptions are
4-
expected; see EXPECTED_EXCEPTIONS in plain2code.py). Reporting is on by default
5-
and can be disabled by setting the CODEPLAIN_NO_TELEMETRY environment variable
6-
to any non-empty value.
4+
expected; see EXPECTED_EXCEPTIONS in plain2code.py). In production, reporting is
5+
on by default and can be disabled by setting CODEPLAIN_TELEMETRY to 0, false or
6+
off. In any other environment it is off unless CODEPLAIN_TELEMETRY is explicitly
7+
set to 1, true or on.
78
"""
89

910
import os
@@ -20,7 +21,7 @@
2021

2122
SENTRY_DSN = "https://64d0d86b50b34e2dede3e4eaf5142282@o4510793955934208.ingest.us.sentry.io/4511540621213696"
2223

23-
NO_TELEMETRY_ENV_VAR = "CODEPLAIN_NO_TELEMETRY"
24+
TELEMETRY_ENV_VAR = "CODEPLAIN_TELEMETRY"
2425

2526
FLUSH_TIMEOUT_SECONDS = 2
2627

@@ -48,10 +49,17 @@
4849

4950

5051
def telemetry_enabled() -> bool:
51-
"""Return True if crash reporting should be active."""
52-
if os.environ.get(NO_TELEMETRY_ENV_VAR):
53-
return False
54-
return True
52+
"""Return True if crash reporting should be active.
53+
54+
In production it is on unless CODEPLAIN_TELEMETRY disables it.
55+
Anywhere else it is off unless CODEPLAIN_TELEMETRY enables it.
56+
"""
57+
setting = os.environ.get(TELEMETRY_ENV_VAR, "").strip().lower()
58+
59+
if system_config.environment == "production":
60+
return setting not in {"0", "false", "off"}
61+
62+
return setting in {"1", "true", "on"}
5563

5664

5765
def initialize_telemetry(**init_overrides: Any) -> bool:

tests/test_telemetry.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import plain2code_telemetry
1111
from plain2code_state import RunState
12-
from plain2code_telemetry import NO_TELEMETRY_ENV_VAR, capture_crash, initialize_telemetry, telemetry_enabled
12+
from plain2code_telemetry import TELEMETRY_ENV_VAR, capture_crash, initialize_telemetry, telemetry_enabled
1313

1414

1515
class CaptureTransport(Transport):
@@ -47,7 +47,10 @@ def make_args(**overrides):
4747
@pytest.fixture(autouse=True)
4848
def clean_telemetry_env(monkeypatch):
4949
"""Ensure tests are not affected by the developer's environment and never send real events."""
50-
monkeypatch.delenv(NO_TELEMETRY_ENV_VAR, raising=False)
50+
monkeypatch.delenv(TELEMETRY_ENV_VAR, raising=False)
51+
# Tests run from a source checkout (a dev environment, where telemetry is
52+
# off by default); pretend to be production so the default path is covered.
53+
monkeypatch.setattr(plain2code_telemetry.system_config, "environment", "production")
5154
yield
5255
client = sentry_sdk.get_client()
5356
if client.is_active():
@@ -63,15 +66,33 @@ def init_with_transport(transport):
6366
assert initialize_telemetry(transport=transport)
6467

6568

66-
def test_no_telemetry_env_var_disables(monkeypatch, transport):
67-
monkeypatch.setenv(NO_TELEMETRY_ENV_VAR, "1")
69+
@pytest.mark.parametrize("value", ["0", "false", "off", "OFF", " False "])
70+
def test_telemetry_env_var_disables_in_production(monkeypatch, transport, value):
71+
monkeypatch.setenv(TELEMETRY_ENV_VAR, value)
6872

6973
assert not telemetry_enabled()
7074
assert not initialize_telemetry(transport=transport)
7175
assert not capture_crash(make_exc_info(KeyError("boom")), None, make_args())
7276
assert transport.events == []
7377

7478

79+
def test_telemetry_disabled_outside_production(monkeypatch, transport):
80+
monkeypatch.setattr(plain2code_telemetry.system_config, "environment", "development")
81+
82+
assert not telemetry_enabled()
83+
assert not initialize_telemetry(transport=transport)
84+
assert not capture_crash(make_exc_info(KeyError("boom")), None, make_args())
85+
assert transport.events == []
86+
87+
88+
@pytest.mark.parametrize("value", ["1", "true", "on", "ON"])
89+
def test_telemetry_env_var_enables_outside_production(monkeypatch, value):
90+
monkeypatch.setattr(plain2code_telemetry.system_config, "environment", "development")
91+
monkeypatch.setenv(TELEMETRY_ENV_VAR, value)
92+
93+
assert telemetry_enabled()
94+
95+
7596
def test_capture_crash_sends_event_with_tags(transport):
7697
init_with_transport(transport)
7798

@@ -198,6 +219,7 @@ def test_environment_comes_from_system_config(transport):
198219

199220
def test_environment_follows_system_config(monkeypatch, transport):
200221
monkeypatch.setattr(plain2code_telemetry.system_config, "environment", "staging")
222+
monkeypatch.setenv(TELEMETRY_ENV_VAR, "1")
201223
init_with_transport(transport)
202224
assert sentry_sdk.get_client().options["environment"] == "staging"
203225

0 commit comments

Comments
 (0)