Skip to content

Commit 614d14c

Browse files
refactor: simplify before_send sampling hook
Collapse _collect_messages generator into a single inline expression and replace nested loops with any(). Same behavior, half the lines.
1 parent dde7d0f commit 614d14c

1 file changed

Lines changed: 7 additions & 19 deletions

File tree

codecov-cli/codecov_cli/opentelemetry.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,14 @@
1111
_SAMPLE_RATE = 100
1212

1313

14-
def _collect_messages(event):
15-
msg = (event.get("logentry") or {}).get("message", "")
16-
if msg:
17-
yield msg
18-
msg = event.get("message", "")
19-
if msg:
20-
yield msg
21-
for exc in (event.get("exception") or {}).get("values", []):
22-
val = exc.get("value", "")
23-
if val:
24-
yield val
25-
26-
2714
def _before_send(event, hint):
28-
for message in _collect_messages(event):
29-
for pattern in _SAMPLED_MESSAGES:
30-
if pattern in message:
31-
if random.randint(1, _SAMPLE_RATE) != 1:
32-
return None
33-
return event
15+
text = " ".join(filter(None, [
16+
(event.get("logentry") or {}).get("message"),
17+
event.get("message"),
18+
*[e.get("value") for e in (event.get("exception") or {}).get("values", [])],
19+
]))
20+
if any(p in text for p in _SAMPLED_MESSAGES) and random.randint(1, _SAMPLE_RATE) != 1:
21+
return None
3422
return event
3523

3624

0 commit comments

Comments
 (0)