Repository navigation
feat(dispatch): add alert markers per group - #5047
Conversation
cece42d to
b5e2104
Compare
b5e2104 to
b4055cb
Compare
|
Reviewed the new marker package standalone. LGTM |
| } | ||
|
|
||
| // SetInhibited implements AlertMarker. | ||
| func (m *alertMarker) SetInhibited(fp model.Fingerprint, inhibitedBy []string) { |
There was a problem hiding this comment.
Just out of curiosity the previous SetIhibited used
func (m *MemMarker) SetInhibited(alert model.Fingerprint, ids ...string) {
And we changed it to []string ... Is there a reason we changed?
There was a problem hiding this comment.
Just for consistency in the interface API for both SetInhibited and SetSilenced.
| delete(m.groups, routeID+groupKey) | ||
| } | ||
|
|
||
| func (m *MemMarker) registerMetrics(r prometheus.Registerer) { |
There was a problem hiding this comment.
We should note in the release note that we removed this metric, as it would be too expensive to calculate it across all markers... ? Unless each time we change it a marker we were to add/remove the relevant number from the metric... But yeah, if not worth it/useful definitely we should say it's gone
There was a problem hiding this comment.
I put it in the PR description:
[CHANGE] Remove `alertmanager_marked_alerts`
Do we need to also update any MD files? cc @SoloJacobs
There was a problem hiding this comment.
Are this TODO and the one above still relevant?
There was a problem hiding this comment.
I think he wanted to add/change the metrics here.
I am not sure if we need it.
I can remove these but my changes don't touch these.
724c634 to
9a5c33e
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughMigrates from a global marker to per-aggregation-group markers, removes callback-based alert-status plumbing (Options.AlertStatusFunc), introduces a marker package with context propagation, switches code from ChangesPer-group marker migration
Sequence DiagramsequenceDiagram
participant Client
participant APIv2
participant Dispatcher
participant AggrGroup
participant markerGroup as GroupMarker
participant alertMarker as AlertMarker
participant Context
Client->>APIv2: GET /api/v2/alerts
APIv2->>Dispatcher: request groups + alertFilter(ctx)
Dispatcher->>markerGroup: ensure group-level marker (marker.NewGroupMarker)
Dispatcher->>AggrGroup: create aggrGroup (allocates alertMarker = NewAlertMarker())
AggrGroup->>Context: ctx' = marker.WithContext(ctx, alertMarker)
Dispatcher->>APIv2: return groups
APIv2->>alertMarker: Status(fp) via marker.FromContext(ctx') for each alert
alertMarker-->>APIv2: alert.AlertStatus
APIv2-->>Client: OpenAPI response with per-alert statuses
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
|
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
Signed-off-by: Siavash Safi <[email protected]>
db964e5 to
c146730
Compare
Compute each alert's status with a fresh AlertMarker at query time instead of reading the group's actual marker or sharing a single request-scoped marker across all groups. This addresses two issues: 1. A shared tempMarker across groups let setAlertStatus writes from one group overwrite another, since the same fingerprint can appear in multiple aggregation groups. 2. Reading the group's actual marker (alertGroup.AlertStatuses) makes the response depend on whether the notification pipeline has run between calls. The endpoint should return the same output when no new alerts, silences, or inhibits have been added. The filter callback also uses a fresh per-alert marker when none is provided, so prediction for one alert does not leak into another. Signed-off-by: Siavash Safi <[email protected]>
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
marker/marker.go (1)
26-33: ⚡ Quick winDefine slice ownership/mutation semantics in the interface contract.
[]stringis accepted and returned on goroutine-safe interfaces, but ownership rules are not specified. Please document whether implementations copy inputs and whether returned slices are immutable/copies, so callers cannot accidentally introduce aliasing races.💡 Suggested doc update
type AlertMarker interface { // SetInhibited sets the inhibitedBy for the given fingerprint. - // If inhibitedBy is empty, it clears the inhibitedBy. + // Implementations must not retain caller-owned slice backing arrays. + // If inhibitedBy is empty, it clears the inhibitedBy. SetInhibited(fingerprint model.Fingerprint, inhibitedBy []string) // SetSilenced sets the silencedBy for the given fingerprint. - // If silencedBy is empty, it clears the silencedBy. + // Implementations must not retain caller-owned slice backing arrays. + // If silencedBy is empty, it clears the silencedBy. SetSilenced(fingerprint model.Fingerprint, silencedBy []string) @@ type GroupMarker interface { // Muted returns true if the group is muted, otherwise false. If the group // is muted then it also returns the names of the time intervals that muted - // it. + // it. Callers must treat the returned slice as immutable. Muted(routeID, groupKey string) ([]string, bool)Also applies to: 49-57
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@marker/marker.go` around lines 26 - 33, Update the interface documentation to define slice ownership/mutation semantics: for SetInhibited and SetSilenced (and any other methods in the same interface around lines 49-57), state explicitly that implementations MUST copy the input []string before storing it (so callers retain ownership) and that any method returning []string MUST return a newly allocated copy (so callers may mutate without causing aliasing/races); mention this contract in the comments for SetInhibited and SetSilenced and in the corresponding getter methods to make the goroutine-safety guarantee explicit.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@dispatch/metric.go`:
- Around line 40-45: newNoopDispatcherMetrics currently constructs a
DispatcherMetrics with several Prometheus metrics but omits initializing
aggrGroupCreationRetries and aggrGroupCreationGivenUp, which can cause
nil-interface panics when .Inc() is later called; update
newNoopDispatcherMetrics to initialize those two fields
(aggrGroupCreationRetries and aggrGroupCreationGivenUp) with non-nil
prometheus.Counter values (e.g., prometheus.NewCounter with empty CounterOpts,
matching how aggrGroupLimitReached is created) so all metric fields on
DispatcherMetrics are always non-nil.
---
Nitpick comments:
In `@marker/marker.go`:
- Around line 26-33: Update the interface documentation to define slice
ownership/mutation semantics: for SetInhibited and SetSilenced (and any other
methods in the same interface around lines 49-57), state explicitly that
implementations MUST copy the input []string before storing it (so callers
retain ownership) and that any method returning []string MUST return a newly
allocated copy (so callers may mutate without causing aliasing/races); mention
this contract in the comments for SetInhibited and SetSilenced and in the
corresponding getter methods to make the goroutine-safety guarantee explicit.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: a1e23e96-57fb-4c00-b1f1-72beead7e651
📥 Commits
Reviewing files that changed from the base of the PR and between a28f0b4367b13c0c000ecbbbe751e91e862ccbf9 and 63c3d21.
📒 Files selected for processing (31)
alert/state.goapi/api.goapi/v2/api.goapi/v2/api_test.goapi/v2/compat.gocmd/alertmanager/main.godispatch/dispatch.godispatch/dispatch_bench_test.godispatch/dispatch_test.godispatch/metric.gofeaturecontrol/featurecontrol.goinhibit/inhibit.goinhibit/inhibit_bench_test.goinhibit/inhibit_test.gomarker/alert.gomarker/alert_test.gomarker/context.gomarker/group.gomarker/group_test.gomarker/marker.gomarker/status.gonotify/mute.gonotify/mute_test.gonotify/notify.goprovider/mem/mem.goprovider/mem/mem_test.gosilence/silence.gosilence/silence_bench_test.gosilence/silence_test.gotypes/types.gotypes/types_test.go
💤 Files with no reviewable changes (4)
- api/api.go
- types/types_test.go
- types/types.go
- provider/mem/mem.go
🚧 Files skipped from review as they are similar to previous changes (23)
- marker/group.go
- marker/group_test.go
- alert/state.go
- inhibit/inhibit_bench_test.go
- featurecontrol/featurecontrol.go
- marker/context.go
- dispatch/dispatch_bench_test.go
- api/v2/compat.go
- cmd/alertmanager/main.go
- silence/silence_test.go
- inhibit/inhibit.go
- marker/alert_test.go
- notify/mute.go
- silence/silence.go
- api/v2/api_test.go
- marker/alert.go
- notify/mute_test.go
- silence/silence_bench_test.go
- inhibit/inhibit_test.go
- notify/notify.go
- api/v2/api.go
- dispatch/dispatch.go
- dispatch/dispatch_test.go
Signed-off-by: Siavash Safi <[email protected]>
Since 9aae092 (per-group alert markers, prometheus#5047) the alert groups handler ran the inhibitor/silencer pipeline twice for every alert in every group: once inside the alert filter to apply the silenced/inhibited/active query parameters, with a fresh marker whose result was discarded, and again in predictAlertStatus to fill in the response status. Each run allocated a map-backed, mutex-guarded marker and went through the silence query path and the inhibitor source cache. The dispatcher also computed per-group statuses from its markers that the handler never read. Make the filter return the status it computed. dispatch.Groups records it per aggregation group in AlertGroup.AlertStatuses and the handler reads it back, so the pipeline runs exactly once per alert per group and the dead marker read in Groups goes away. Evaluation is deliberately per group as in the future we want to support an alert being in a different status per-group (e.g. silenced by receiver). The pipeline reports silence and inhibition IDs only through the marker found in the context, so the read path now hands it a small single-alert collector instead of a full marker. Status is still predicted at query time rather than read from the dispatcher's markers. BenchmarkGetAlertGroupsHandler (new, api/v2) runs the handler against a real silencer with 10 active silences and a real inhibitor with 10 source alerts, about 10% of alerts silenced and 10% inhibited. Before is main at 418e7ed, after is this commit, 3 runs each on the same machine: groups x alerts before after delta 100 x 10 7.37 ms/op 4.50 ms/op -39% 1000 x 10 89.2 ms/op 50.7 ms/op -43% 100 x 100 83.6 ms/op 53.6 ms/op -36% memory (geomean) 14.6 MiB/op 8.6 MiB/op -41% allocs (geomean) 342k/op 195k/op -43% Tests: TestGetAlertGroupsHandlerPredictsStatusPerGroup (one pipeline run per alert per group, each group reports its own evaluation), TestAlertFilter (short-circuits before the pipeline, query parameters against the predicted status), TestStatusCollector, and TestGroupsRecordsFilterStatus in dispatch (filter called once per alert per group, status recorded per group, rejected alerts left out). Signed-off-by: Guido Trotter <[email protected]>
…#5674) Since 9aae092 (per-group alert markers, #5047) the alert groups handler ran the inhibitor/silencer pipeline twice for every alert in every group: once inside the alert filter to apply the silenced/inhibited/active query parameters, with a fresh marker whose result was discarded, and again in predictAlertStatus to fill in the response status. Each run allocated a map-backed, mutex-guarded marker and went through the silence query path and the inhibitor source cache. The dispatcher also computed per-group statuses from its markers that the handler never read. Make the filter return the status it computed. dispatch.Groups records it per aggregation group in AlertGroup.AlertStatuses and the handler reads it back, so the pipeline runs exactly once per alert per group and the dead marker read in Groups goes away. Evaluation is deliberately per group as in the future we want to support an alert being in a different status per-group (e.g. silenced by receiver). The pipeline reports silence and inhibition IDs only through the marker found in the context, so the read path now hands it a small single-alert collector instead of a full marker. Status is still predicted at query time rather than read from the dispatcher's markers. BenchmarkGetAlertGroupsHandler (new, api/v2) runs the handler against a real silencer with 10 active silences and a real inhibitor with 10 source alerts, about 10% of alerts silenced and 10% inhibited. Before is main at 418e7ed, after is this commit, 3 runs each on the same machine: groups x alerts before after delta 100 x 10 7.37 ms/op 4.50 ms/op -39% 1000 x 10 89.2 ms/op 50.7 ms/op -43% 100 x 100 83.6 ms/op 53.6 ms/op -36% memory (geomean) 14.6 MiB/op 8.6 MiB/op -41% allocs (geomean) 342k/op 195k/op -43% Tests: TestGetAlertGroupsHandlerPredictsStatusPerGroup (one pipeline run per alert per group, each group reports its own evaluation), TestAlertFilter (short-circuits before the pipeline, query parameters against the predicted status), TestStatusCollector, and TestGroupsRecordsFilterStatus in dispatch (filter called once per alert per group, status recorded per group, rejected alerts left out). Signed-off-by: Guido Trotter <[email protected]> Co-authored-by: Guido Trotter <[email protected]>
This change adds alert markers to the aggregation groups in dispatcher.
Alert markers replace the global marker and are used to track the state of alerts in each aggregation group.
This change touches many components of the alertmanager.
Per Group alert markers are passed to the notifiers and then inhibitor and silencer using context.
The API has no breaking changes:
The metrics are also updated to use group markers.
The
alertmanager_alertsmetric is moved to dispatcher.The
alertmanager_marked_alertsmetric is removed.By default it behaves the same as before, by aggregating all alerts in the groups.
Enabling
group-key-in-metricsflag will cause the metrics to be grouped bygroup_key.Pull Request Checklist
Please check all the applicable boxes.
Which user-facing changes does this PR introduce?
Summary by CodeRabbit
New Features
Bug Fixes
Refactor