Skip to content

Commit 130cb06

Browse files
fix: sample high-volume "Token required" Sentry errors at 1% (#116)
## Summary - Adds a `before_send` hook to Sentry SDK init that samples "Token required" error events at 1% (1 in 100), dropping the rest - Issue [CLI-S](https://codecov.sentry.io/issues/CLI-S) has ~21M occurrences flooding Sentry; this reduces volume while still capturing representative samples - The sampling pattern list (`_SAMPLED_MESSAGES`) is extensible for future high-volume errors ## Test plan - [ ] Verify existing tests pass (`pytest tests/helpers/test_args.py`) - [ ] Confirm other Sentry events (non-"Token required") are unaffected and always sent - [ ] After deploy, monitor CLI-S event volume in Sentry to confirm ~99% reduction
1 parent c1401cf commit 130cb06

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

codecov-cli/codecov_cli/opentelemetry.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
import os
2+
import random
23

34
import sentry_sdk
45

56
from codecov_cli import __version__
67

8+
_SAMPLED_MESSAGES = [
9+
"Token required",
10+
]
11+
_SAMPLE_RATE = 100
712
_SKIP_TAG_KEYS = {"branch", "flags", "commit_sha", "env_vars"}
813

914

15+
def _before_send(event, hint):
16+
messages = []
17+
if "message" in event and event.get("message") is not None:
18+
messages.append(event.get("message"))
19+
if "logentry" in event and "message" in event.get("logentry", {}) and event.get("logentry", {}).get("message") is not None:
20+
messages.append(event.get("logentry", {}).get("message"))
21+
if "exception" in event and event.get("exception") is not None:
22+
for exc in event.get("exception", {}).get("values", []):
23+
if "value" in exc:
24+
messages.append(exc.get("value"))
25+
26+
matched = False
27+
for message in messages:
28+
for pattern in _SAMPLED_MESSAGES:
29+
if pattern in message:
30+
matched = True
31+
break
32+
33+
if matched and random.randint(1, _SAMPLE_RATE) != 1:
34+
return None
35+
return event
36+
37+
1038
def init_telem(ctx):
1139
if ctx["disable_telem"]:
1240
return
@@ -20,6 +48,7 @@ def init_telem(ctx):
2048
enable_tracing=True,
2149
environment=os.getenv("CODECOV_ENV", "production"),
2250
release=f"cli@{__version__}",
51+
before_send=_before_send,
2352
)
2453

2554

0 commit comments

Comments
 (0)