Skip to content
Open
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
6 changes: 6 additions & 0 deletions keep/providers/base/base_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ def get_alert_fingerprint(alert: AlertDto, fingerprint_fields: list = []) -> str
"""
if not fingerprint_fields:
return alert.name
# if the only fingerprint field is the fingerprint itself, the alert
# already carries a fingerprint provided by the monitoring tool -
# preserve it as-is instead of hashing it, so Keep's fingerprint stays
# aligned with the tool's (e.g. Grafana/Prometheus)
if fingerprint_fields == ["fingerprint"] and alert.fingerprint:
return alert.fingerprint
fingerprint = hashlib.sha256()
event_dict = alert.dict()
for fingerprint_field in fingerprint_fields:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_get_alerts_custom_dedup.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@ def test_no_custom_rule_keeps_original_fingerprint(self, mock_get_rule):

self.assertEqual(alerts[0].fingerprint, "original-fp")

@patch("keep.providers.base.base_provider.get_custom_deduplication_rule")
def test_custom_dedup_on_fingerprint_field_preserves_tool_fingerprint(
self, mock_get_rule
):
"""A custom rule whose only field is "fingerprint" should keep the
tool-provided fingerprint verbatim instead of hashing it."""
alert = _make_alert(
"HighCPU",
labels={"alertname": "HighCPU"},
fingerprint="grafana-native-fp",
)

rule = MagicMock()
rule.fingerprint_fields = ["fingerprint"]
mock_get_rule.return_value = rule

provider = _make_provider([alert])

with patch(
"keep.providers.base.base_provider.tracer"
) as mock_tracer:
mock_tracer.start_as_current_span.return_value.__enter__ = MagicMock()
mock_tracer.start_as_current_span.return_value.__exit__ = MagicMock()
alerts = provider.get_alerts()

self.assertEqual(alerts[0].fingerprint, "grafana-native-fp")

@patch("keep.providers.base.base_provider.get_custom_deduplication_rule")
def test_custom_dedup_with_dot_notation_fields(self, mock_get_rule):
"""Custom dedup should support dot-notation to access nested dict fields."""
Expand Down
Loading