Skip to content
Draft
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
11 changes: 7 additions & 4 deletions sentry_sdk/_agent_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@

GIT_TIMEOUT_SECONDS = 2

NO_DSN_NOTE = "No DSN is set, so this run sends nothing to Sentry. When one is set:"

_exit_hook_registered = False
# What _write_coding_agent_hint can print at exit: one entry for each file that called
# init() with tracing or logs off.
_hints_at_exit: "List[Tuple[str, bool, bool]]" = []
_hints_at_exit: "List[Tuple[str, bool, bool, bool]]" = []


def _running_under_coding_agent() -> bool:
Expand Down Expand Up @@ -163,7 +165,7 @@ def register_coding_agent_hint_exit_hook() -> None:
def _write_coding_agent_hint() -> None:
with capture_internal_exceptions():
# One hint, for the first init() from a file that this user changed.
for init_file, tracing_on, logs_on in _hints_at_exit:
for init_file, tracing_on, logs_on, has_dsn in _hints_at_exit:
if _setup_in_progress(init_file):
break
else:
Expand Down Expand Up @@ -218,6 +220,8 @@ def _write_coding_agent_hint() -> None:
"checklist, included in the sentry-sdk package you installed: "
f"{SETUP_CHECKLIST_PATH}"
)
if not has_dsn:
hint = f"{NO_DSN_NOTE} {hint}"
sys.stdout.flush()
sys.stderr.write(f"[sentry] {hint} (hide with SENTRY_AGENT_HINTS=0)\n")
sys.stderr.flush()
Expand All @@ -232,7 +236,6 @@ def show_coding_agent_hint(client: "BaseClient", caller: "FrameType") -> None:
not _exit_hook_registered
or sys.version_info < (3, 9)
or sys.platform not in ("darwin", "linux")
or not options["dsn"]
or options["_experiments"].get("agent_setup_complete")
or (tracing_on and logs_on)
):
Expand All @@ -243,4 +246,4 @@ def show_coding_agent_hint(client: "BaseClient", caller: "FrameType") -> None:
return
# Agents often cut a command's output with `| tail`, so the hint goes last.
if all(init_file != seen[0] for seen in _hints_at_exit):
_hints_at_exit.append((init_file, tracing_on, logs_on))
_hints_at_exit.append((init_file, tracing_on, logs_on, bool(options["dsn"])))
29 changes: 27 additions & 2 deletions tests/test_agent_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def run_app(tmp_path, monkeypatch, capsys):
monkeypatch.setattr(sentry_sdk._agent_hint.atexit, "register", at_exit.append)
app = tmp_path / "app.py"

def run(env, init_args="", before_run=None, inits=1):
def run(env, init_args="", before_run=None, inits=1, dsn=AGENT_HINT_DSN):
for name, value in env.items():
monkeypatch.setenv(name, value)
init = f"sentry_sdk.init(dsn={AGENT_HINT_DSN!r}, {init_args})\n"
init = f"sentry_sdk.init(dsn={dsn!r}, {init_args})\n"
imports = (
"import sentry_sdk\n"
"from sentry_sdk.integrations.logging import LoggingIntegration\n"
Expand Down Expand Up @@ -125,6 +125,31 @@ def test_coding_agent_hint_shown_once(run_app):
assert printed.count(SETUP_CHECKLIST_PATH) == 1


@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Python < 3.9 hits an early return"
)
@pytest.mark.parametrize(
"env, hint",
[
(
{"CLAUDECODE": "1"},
"No DSN is set, so this run sends nothing to Sentry. When one is set: Sentry will get errors from this app but not the request trace around them, and none of its logs.",
),
(
{"CODEX_THREAD_ID": "1"},
"No DSN is set, so this run sends nothing to Sentry. When one is set: Tracing and logs are off, so Sentry only gets errors.",
),
],
)
def test_coding_agent_hint_without_a_dsn(run_app, monkeypatch, env, hint):
monkeypatch.delenv("SENTRY_DSN", raising=False)

printed = run_app(env, dsn=None)

assert f"[sentry] {hint}" in printed
assert SETUP_CHECKLIST_PATH in printed


def _commit(app, date=None):
env = {
**os.environ,
Expand Down
Loading