Skip to content

Commit 356c200

Browse files
committed
feat: Say what's off in the agent setup hint
Start the hint with what Sentry isn't getting (tracing, logs or both), so an agent that doesn't open the checklist can still tell the user what's missing.
1 parent 20e8b44 commit 356c200

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎sentry_sdk/client.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,19 @@ def _running_under_coding_agent() -> bool:
329329
return any(os.environ.get(name) for name in CODING_AGENT_ENV_VARS)
330330

331331

332-
def _write_coding_agent_hint() -> None:
332+
_MISSING_PRODUCTS = {
333+
(False, False): "Tracing and logs are off, so Sentry only gets errors.",
334+
(False, True): "Tracing is off, so Sentry gets errors and logs but no traces.",
335+
(True, False): "Logs are off, so Sentry gets errors and traces but no logs.",
336+
}
337+
338+
339+
def _write_coding_agent_hint(missing: str) -> None:
333340
sys.stdout.flush()
334341
sys.stderr.write(
335-
"[sentry] If you're setting up Sentry, it is strongly recommended to check "
336-
"your setup against Sentry's official setup checklist, included in the "
337-
f"sentry-sdk package you installed: {SETUP_CHECKLIST_PATH} "
342+
f"[sentry] {missing} If you're setting up Sentry, it is strongly recommended "
343+
"to check your setup against Sentry's official setup checklist, included in "
344+
f"the sentry-sdk package you installed: {SETUP_CHECKLIST_PATH} "
338345
"(hide with SENTRY_AGENT_HINTS=0)\n"
339346
)
340347
sys.stderr.flush()
@@ -346,18 +353,19 @@ def _show_coding_agent_hint(options: "Dict[str, Any]") -> None:
346353

347354
rate = options["traces_sample_rate"]
348355
tracing_on = options["traces_sampler"] is not None or rate not in (0, None)
356+
logs_on = bool(options["enable_logs"])
349357
if (
350358
_coding_agent_hint_shown
351359
or not options["dsn"]
352360
or options["agent_setup_complete"]
353-
or (tracing_on and options["enable_logs"])
361+
or (tracing_on and logs_on)
354362
or not _running_under_coding_agent()
355363
):
356364
return
357365

358366
_coding_agent_hint_shown = True
359367
# Agents often cut a command's output with `| tail`, so the hint goes last.
360-
atexit.register(_write_coding_agent_hint)
368+
atexit.register(_write_coding_agent_hint, _MISSING_PRODUCTS[(tracing_on, logs_on)])
361369

362370

363371
def _get_options(*args: "Optional[str]", **kwargs: "Any") -> "Dict[str, Any]":

‎tests/test_client.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,11 +1517,15 @@ def coding_agent_hint(monkeypatch, capsys):
15171517
monkeypatch.delenv(name, raising=False)
15181518
monkeypatch.setattr(sentry_sdk.client, "_coding_agent_hint_shown", False)
15191519
registered = []
1520-
monkeypatch.setattr(sentry_sdk.client.atexit, "register", registered.append)
1520+
monkeypatch.setattr(
1521+
sentry_sdk.client.atexit,
1522+
"register",
1523+
lambda hook, *args: registered.append((hook, args)),
1524+
)
15211525

15221526
def printed_at_exit():
1523-
for hook in registered:
1524-
hook()
1527+
for hook, args in registered:
1528+
hook(*args)
15251529
return capsys.readouterr().err
15261530

15271531
monkeypatch.printed_at_exit = printed_at_exit
@@ -1572,6 +1576,30 @@ def test_coding_agent_hint_not_shown(sentry_init, coding_agent_hint, env, option
15721576
assert "[sentry]" not in coding_agent_hint.printed_at_exit()
15731577

15741578

1579+
@pytest.mark.parametrize(
1580+
"options, missing",
1581+
[
1582+
({}, "Tracing and logs are off, so Sentry only gets errors."),
1583+
(
1584+
{"enable_logs": True},
1585+
"Tracing is off, so Sentry gets errors and logs but no traces.",
1586+
),
1587+
(
1588+
{"traces_sample_rate": 0.1},
1589+
"Logs are off, so Sentry gets errors and traces but no logs.",
1590+
),
1591+
],
1592+
)
1593+
def test_coding_agent_hint_names_what_is_off(
1594+
sentry_init, coding_agent_hint, options, missing
1595+
):
1596+
coding_agent_hint.setenv("CLAUDECODE", "1")
1597+
1598+
sentry_init(dsn=AGENT_HINT_DSN, **options)
1599+
1600+
assert coding_agent_hint.printed_at_exit().startswith(f"[sentry] {missing} ")
1601+
1602+
15751603
def test_coding_agent_hint_shown_once(sentry_init, coding_agent_hint):
15761604
coding_agent_hint.setenv("AI_AGENT", "claude-code")
15771605

0 commit comments

Comments
 (0)