Skip to content

Commit 1e59e13

Browse files
committed
use helper
1 parent 39299bc commit 1e59e13

File tree

3 files changed

+34
-100
lines changed

3 files changed

+34
-100
lines changed

src/sentry/grouping/ingest/seer.py

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
filter_null_from_string,
2323
get_stacktrace_string_with_metrics,
2424
killswitch_enabled,
25+
record_did_call_seer_metric,
2526
)
2627
from sentry.utils import metrics
2728
from sentry.utils.circuit_breaker2 import CircuitBreaker
@@ -96,22 +97,14 @@ def _has_customized_fingerprint(event: Event, variants: Mapping[str, BaseVariant
9697

9798
# Hybrid fingerprinting ({{ default }} + some other value(s))
9899
else:
99-
metrics.incr(
100-
"grouping.similarity.did_call_seer",
101-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
102-
tags={"call_made": False, "blocker": "hybrid-fingerprint"},
103-
)
100+
record_did_call_seer_metric(call_made=False, blocker="hybrid-fingerprint")
104101
return True
105102

106103
# Fully customized fingerprint (from either us or the user)
107104
fingerprint_variant = variants.get("custom_fingerprint") or variants.get("built_in_fingerprint")
108105

109106
if fingerprint_variant:
110-
metrics.incr(
111-
"grouping.similarity.did_call_seer",
112-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
113-
tags={"call_made": False, "blocker": fingerprint_variant.type},
114-
)
107+
record_did_call_seer_metric(call_made=False, blocker=fingerprint_variant.type)
115108
return True
116109

117110
return False
@@ -133,12 +126,7 @@ def _ratelimiting_enabled(event: Event, project: Project) -> bool:
133126
if ratelimiter.backend.is_limited("seer:similarity:global-limit", **global_ratelimit):
134127
logger_extra["limit_per_sec"] = global_limit_per_sec
135128
logger.warning("should_call_seer_for_grouping.global_ratelimit_hit", extra=logger_extra)
136-
137-
metrics.incr(
138-
"grouping.similarity.did_call_seer",
139-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
140-
tags={"call_made": False, "blocker": "global-rate-limit"},
141-
)
129+
record_did_call_seer_metric(call_made=False, blocker="global-rate-limit")
142130

143131
return True
144132

@@ -147,12 +135,7 @@ def _ratelimiting_enabled(event: Event, project: Project) -> bool:
147135
):
148136
logger_extra["limit_per_sec"] = project_limit_per_sec
149137
logger.warning("should_call_seer_for_grouping.project_ratelimit_hit", extra=logger_extra)
150-
151-
metrics.incr(
152-
"grouping.similarity.did_call_seer",
153-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
154-
tags={"call_made": False, "blocker": "project-rate-limit"},
155-
)
138+
record_did_call_seer_metric(call_made=False, blocker="project-rate-limit")
156139

157140
return True
158141

@@ -173,11 +156,7 @@ def _circuit_breaker_broken(event: Event, project: Project) -> bool:
173156
**breaker_config,
174157
},
175158
)
176-
metrics.incr(
177-
"grouping.similarity.did_call_seer",
178-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
179-
tags={"call_made": False, "blocker": "circuit-breaker"},
180-
)
159+
record_did_call_seer_metric(call_made=False, blocker="circuit-breaker")
181160

182161
return circuit_broken
183162

@@ -188,14 +167,7 @@ def _has_empty_stacktrace_string(event: Event, variants: Mapping[str, BaseVarian
188167
)
189168
if not stacktrace_string:
190169
if stacktrace_string == "":
191-
metrics.incr(
192-
"grouping.similarity.did_call_seer",
193-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
194-
tags={
195-
"call_made": False,
196-
"blocker": "empty-stacktrace-string",
197-
},
198-
)
170+
record_did_call_seer_metric(call_made=False, blocker="empty-stacktrace-string")
199171
return True
200172
# Store the stacktrace string in the event so we only calculate it once. We need to pop it
201173
# later so it isn't stored in the database.
@@ -286,11 +258,7 @@ def maybe_check_seer_for_matching_grouphash(
286258
seer_matched_grouphash = None
287259

288260
if should_call_seer_for_grouping(event, variants):
289-
metrics.incr(
290-
"grouping.similarity.did_call_seer",
291-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
292-
tags={"call_made": True, "blocker": "none"},
293-
)
261+
record_did_call_seer_metric(call_made=True, blocker="none")
294262

295263
try:
296264
# If no matching group is found in Seer, we'll still get back result

src/sentry/seer/similarity/utils.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ def get_stacktrace_string_with_metrics(
291291
data: dict[str, Any], platform: str | None, referrer: ReferrerOptions
292292
) -> str | None:
293293
stacktrace_string = None
294-
key = "grouping.similarity.did_call_seer"
295294
sample_rate = options.get("seer.similarity.metrics_sample_rate")
296295
try:
297296
stacktrace_string = get_stacktrace_string(data, platform)
@@ -303,11 +302,7 @@ def get_stacktrace_string_with_metrics(
303302
tags={"platform": platform, "referrer": referrer},
304303
)
305304
if referrer == ReferrerOptions.INGEST:
306-
metrics.incr(
307-
key,
308-
sample_rate=sample_rate,
309-
tags={"call_made": False, "blocker": "over-threshold-frames"},
310-
)
305+
record_did_call_seer_metric(call_made=False, blocker="over-threshold-frames")
311306
except Exception:
312307
logger.exception("Unexpected exception in stacktrace string formatting")
313308

@@ -376,23 +371,17 @@ def killswitch_enabled(
376371
"should_call_seer_for_grouping.seer_global_killswitch_enabled",
377372
extra=logger_extra,
378373
)
379-
metrics.incr(
380-
"grouping.similarity.did_call_seer",
381-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
382-
tags={"call_made": False, "blocker": "global-killswitch"},
383-
)
374+
record_did_call_seer_metric(call_made=False, blocker="global-killswitch")
375+
384376
return True
385377

386378
if options.get("seer.similarity-killswitch.enabled"):
387379
logger.warning(
388380
"should_call_seer_for_grouping.seer_similarity_killswitch_enabled",
389381
extra=logger_extra,
390382
)
391-
metrics.incr(
392-
"grouping.similarity.did_call_seer",
393-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
394-
tags={"call_made": False, "blocker": "similarity-killswitch"},
395-
)
383+
record_did_call_seer_metric(call_made=False, blocker="similarity-killswitch")
384+
396385
return True
397386

398387
if killswitch_matches_context(
@@ -402,11 +391,8 @@ def killswitch_enabled(
402391
"should_call_seer_for_grouping.seer_similarity_project_killswitch_enabled",
403392
extra=logger_extra,
404393
)
405-
metrics.incr(
406-
"grouping.similarity.did_call_seer",
407-
sample_rate=options.get("seer.similarity.metrics_sample_rate"),
408-
tags={"call_made": False, "blocker": "project-killswitch"},
409-
)
394+
record_did_call_seer_metric(call_made=False, blocker="project-killswitch")
395+
410396
return True
411397

412398
return False

tests/sentry/grouping/ingest/test_seer.py

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ def test_obeys_customized_fingerprint_check(self) -> None:
176176
is expected_result
177177
), f'Case with fingerprint {event.data["fingerprint"]} failed.'
178178

179-
@patch("sentry.grouping.ingest.seer.metrics")
180-
def test_obeys_empty_stacktrace_string_check(self, mock_metrics: Mock) -> None:
179+
@patch("sentry.grouping.ingest.seer.record_did_call_seer_metric")
180+
def test_obeys_empty_stacktrace_string_check(self, mock_record_did_call_seer: Mock) -> None:
181181
self.project.update_option("sentry:similarity_backfill_completed", int(time()))
182182
new_event = Event(
183183
project_id=self.project.id,
@@ -190,14 +190,8 @@ def test_obeys_empty_stacktrace_string_check(self, mock_metrics: Mock) -> None:
190190
)
191191

192192
assert should_call_seer_for_grouping(new_event, new_event.get_grouping_variants()) is False
193-
sample_rate = options.get("seer.similarity.metrics_sample_rate")
194-
mock_metrics.incr.assert_any_call(
195-
"grouping.similarity.did_call_seer",
196-
sample_rate=sample_rate,
197-
tags={
198-
"call_made": False,
199-
"blocker": "empty-stacktrace-string",
200-
},
193+
mock_record_did_call_seer.assert_any_call(
194+
call_made=False, blocker="empty-stacktrace-string"
201195
)
202196

203197
@patch("sentry.grouping.ingest.seer.get_similarity_data_from_seer", return_value=[])
@@ -340,8 +334,11 @@ def test_returns_no_grouphash_and_empty_metadata_if_empty_stacktrace(
340334
},
341335
)
342336

337+
@patch("sentry.seer.similarity.utils.record_did_call_seer_metric")
343338
@patch("sentry.seer.similarity.utils.metrics")
344-
def test_too_many_frames(self, mock_metrics: Mock) -> None:
339+
def test_too_many_frames(
340+
self, mock_metrics: Mock, mock_record_did_call_seer: MagicMock
341+
) -> None:
345342
type = "FailedToFetchError"
346343
value = "Charlie didn't bring the ball back"
347344
context_line = f"raise {type}('{value}')"
@@ -386,17 +383,10 @@ def test_too_many_frames(self, mock_metrics: Mock) -> None:
386383
sample_rate=sample_rate,
387384
tags={"platform": "java", "referrer": "ingest"},
388385
)
389-
mock_metrics.incr.assert_any_call(
390-
"grouping.similarity.did_call_seer",
391-
sample_rate=1.0,
392-
tags={
393-
"call_made": False,
394-
"blocker": "over-threshold-frames",
395-
},
396-
)
386+
mock_record_did_call_seer.assert_any_call(call_made=False, blocker="over-threshold-frames")
397387

398-
@patch("sentry.seer.similarity.utils.metrics")
399-
def test_too_many_frames_allowed_platform(self, mock_metrics: Mock) -> None:
388+
@patch("sentry.seer.similarity.utils.record_did_call_seer_metric")
389+
def test_too_many_frames_allowed_platform(self, mock_record_did_call_seer: MagicMock) -> None:
400390
type = "FailedToFetchError"
401391
value = "Charlie didn't bring the ball back"
402392
context_line = f"raise {type}('{value}')"
@@ -436,15 +426,8 @@ def test_too_many_frames_allowed_platform(self, mock_metrics: Mock) -> None:
436426
)
437427

438428
assert (
439-
call(
440-
"grouping.similarity.did_call_seer",
441-
sample_rate=1.0,
442-
tags={
443-
"call_made": False,
444-
"blocker": "over-threshold-frames",
445-
},
446-
)
447-
not in mock_metrics.incr.call_args_list
429+
call(call_made=False, blocker="over-threshold-frames")
430+
not in mock_record_did_call_seer.call_args_list
448431
)
449432

450433

@@ -505,10 +488,14 @@ def test_valid_maybe_check_seer_for_matching_group_hash(
505488
}
506489
)
507490

491+
@patch("sentry.seer.similarity.utils.record_did_call_seer_metric")
508492
@patch("sentry.grouping.ingest.seer.get_seer_similar_issues")
509493
@patch("sentry.seer.similarity.utils.metrics")
510494
def test_too_many_only_system_frames_maybe_check_seer_for_matching_group_hash(
511-
self, mock_metrics: MagicMock, mock_get_similar_issues: MagicMock
495+
self,
496+
mock_metrics: MagicMock,
497+
mock_get_similar_issues: MagicMock,
498+
mock_record_did_call_seer: MagicMock,
512499
) -> None:
513500
self.project.update_option("sentry:similarity_backfill_completed", int(time()))
514501

@@ -556,14 +543,7 @@ def test_too_many_only_system_frames_maybe_check_seer_for_matching_group_hash(
556543
sample_rate=sample_rate,
557544
tags={"platform": "java", "referrer": "ingest"},
558545
)
559-
mock_metrics.incr.assert_any_call(
560-
"grouping.similarity.did_call_seer",
561-
sample_rate=1.0,
562-
tags={
563-
"call_made": False,
564-
"blocker": "over-threshold-frames",
565-
},
566-
)
546+
mock_record_did_call_seer.assert_any_call(call_made=False, blocker="over-threshold-frames")
567547

568548
mock_get_similar_issues.assert_not_called()
569549

0 commit comments

Comments
 (0)