-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix zeroed EndsAt timestamp #3805
Closed
grobinson-grafana
wants to merge
5
commits into
prometheus:main
from
grobinson-grafana:grobinson/fix-ends-at
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -454,9 +454,7 @@ func (ag *aggrGroup) run(nf notifyFunc) { | |
ag.hasFlushed = true | ||
ag.mtx.Unlock() | ||
|
||
ag.flush(func(alerts ...*types.Alert) bool { | ||
return nf(ctx, alerts...) | ||
}) | ||
ag.flush(ctx, nf) | ||
|
||
cancel() | ||
|
||
|
@@ -493,29 +491,30 @@ func (ag *aggrGroup) empty() bool { | |
} | ||
|
||
// flush sends notifications for all new alerts. | ||
func (ag *aggrGroup) flush(notify func(...*types.Alert) bool) { | ||
func (ag *aggrGroup) flush(ctx context.Context, nf notifyFunc) { | ||
if ag.empty() { | ||
return | ||
} | ||
|
||
now, ok := notify.Now(ctx) | ||
if !ok { | ||
level.Error(ag.logger).Log("msg", "now missing") | ||
return | ||
} | ||
|
||
var ( | ||
alerts = ag.alerts.List() | ||
alertsSlice = make(types.AlertSlice, 0, len(alerts)) | ||
now = time.Now() | ||
) | ||
for _, alert := range alerts { | ||
a := *alert | ||
// Ensure that alerts don't resolve as time move forwards. | ||
if !a.ResolvedAt(now) { | ||
a.EndsAt = time.Time{} | ||
} | ||
alertsSlice = append(alertsSlice, &a) | ||
} | ||
sort.Stable(alertsSlice) | ||
|
||
level.Debug(ag.logger).Log("msg", "flushing", "alerts", fmt.Sprintf("%v", alertsSlice)) | ||
|
||
if notify(alertsSlice...) { | ||
if nf(ctx, alertsSlice...) { | ||
for _, a := range alertsSlice { | ||
// Only delete if the fingerprint has not been inserted | ||
// again since we notified about it. | ||
|
@@ -526,7 +525,7 @@ func (ag *aggrGroup) flush(notify func(...*types.Alert) bool) { | |
level.Error(ag.logger).Log("msg", "failed to get alert", "err", err, "alert", a.String()) | ||
continue | ||
} | ||
if a.Resolved() && got.UpdatedAt == a.UpdatedAt { | ||
if a.ResolvedAt(now) && got.UpdatedAt == a.UpdatedAt { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems fine. If |
||
if err := ag.alerts.Delete(fp); err != nil { | ||
level.Error(ag.logger).Log("msg", "error on delete alert", "err", err, "alert", a.String()) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect we can remove this, but will do so in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand the intent correctly this is passing the reference time through the context and every notifier should use the
.*At(now time.Time)
variant. As we don't (plan) to modify the alert we don't need to take a deep copy (some notifiers call types.Alerts).Dropping this copy might be hard. On line 528 (529) the UpdatedAt time is compared between the copy and the storage of the aggregation group. Without a copy these numbers will always be the same.
I am wondering if there are ways to make sure that integrations never call the
Resolved()
,Status()
? Given that alertmanager is likely to see an increase in number of integrations that appears desirable to me to ease writing and reviewing these.Given that the model.Alert is copied and one needs the reference time. What is your thought on creating something like a model.AlertsSnapshot that holds an immutable snapshot of the alert and the status/reference time? This would ease writing and reviewing the notifiers, protect the core from accidental modificiations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh well spotted!
This is what I tried at first, but I found it to be quite an intrusive change (#3351 (comment)). I haven't ruled it out though - do you think that would be a better way to fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think longterm we are better off with a genuine snapshot. It eases writing integrations, it eases code review of them. I think some of the type changes we can probably automate with gofmt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but from my point of view the trade-off is whether we want all of these fields to be mutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh – I think I misunderstood. By mutable/immutable are you talking about the fields being package-public (capitalized) or package-private (starting with lowercase)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Starting with lower case. I wanted to see how an implementation of this looks like and got the below to compile (and pass most tests but it requires some clean ups).
For me the biggest benefit is that it creates an immutable snapshot and simplifies API. The notification handlers are unable to modify the underlying Alert and the API removes the
*At(time.Time)
functions and we don't have to remember usingnotify.Now
.Please see:
main...zecke:alertmanager:zecke-alternative-ends-at
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think you have time to open a PR for this? @gotjosh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Let me fix the formatting and remove some of the repeated code in the tests.