-
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
Alert metric reports different results to what the user sees via API #2943
Merged
roidelapluie
merged 2 commits into
prometheus:main
from
gotjosh:fix-alert-count-metrics
Jun 16, 2022
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/model" | ||
|
||
"github.com/prometheus/alertmanager/provider" | ||
|
@@ -33,8 +34,10 @@ const alertChannelLength = 200 | |
type Alerts struct { | ||
cancel context.CancelFunc | ||
|
||
alerts *store.Alerts | ||
marker types.Marker | ||
|
||
mtx sync.Mutex | ||
alerts *store.Alerts | ||
listeners map[int]listeningAlerts | ||
next int | ||
|
||
|
@@ -62,14 +65,34 @@ type listeningAlerts struct { | |
done chan struct{} | ||
} | ||
|
||
func (a *Alerts) registerMetrics(r prometheus.Registerer) { | ||
newMemAlertByStatus := func(s types.AlertState) prometheus.GaugeFunc { | ||
return prometheus.NewGaugeFunc( | ||
prometheus.GaugeOpts{ | ||
Name: "alertmanager_alerts", | ||
Help: "How many alerts by state.", | ||
ConstLabels: prometheus.Labels{"state": string(s)}, | ||
}, | ||
func() float64 { | ||
return float64(a.count(s)) | ||
}, | ||
) | ||
} | ||
|
||
r.MustRegister(newMemAlertByStatus(types.AlertStateActive)) | ||
r.MustRegister(newMemAlertByStatus(types.AlertStateSuppressed)) | ||
r.MustRegister(newMemAlertByStatus(types.AlertStateUnprocessed)) | ||
} | ||
|
||
// NewAlerts returns a new alert provider. | ||
func NewAlerts(ctx context.Context, m types.Marker, intervalGC time.Duration, alertCallback AlertStoreCallback, l log.Logger) (*Alerts, error) { | ||
func NewAlerts(ctx context.Context, m types.Marker, intervalGC time.Duration, alertCallback AlertStoreCallback, l log.Logger, r prometheus.Registerer) (*Alerts, error) { | ||
if alertCallback == nil { | ||
alertCallback = noopCallback{} | ||
} | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
a := &Alerts{ | ||
marker: m, | ||
alerts: store.NewAlerts(), | ||
cancel: cancel, | ||
listeners: map[int]listeningAlerts{}, | ||
|
@@ -98,6 +121,11 @@ func NewAlerts(ctx context.Context, m types.Marker, intervalGC time.Duration, al | |
} | ||
a.mtx.Unlock() | ||
}) | ||
|
||
if r != nil { | ||
a.registerMetrics(r) | ||
} | ||
|
||
go a.alerts.Run(ctx, intervalGC) | ||
|
||
return a, nil | ||
|
@@ -212,6 +240,25 @@ func (a *Alerts) Put(alerts ...*types.Alert) error { | |
return nil | ||
} | ||
|
||
// count returns the number of non-resolved alerts we currently have stored filtered by the provided state. | ||
func (a *Alerts) count(state types.AlertState) int { | ||
var count int | ||
for _, alert := range a.alerts.List() { | ||
if alert.Resolved() { | ||
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. This is the big change, we now skip resolved alerts. |
||
continue | ||
} | ||
|
||
status := a.marker.Status(alert.Fingerprint()) | ||
if status.State != state { | ||
continue | ||
} | ||
|
||
count++ | ||
} | ||
|
||
return count | ||
} | ||
|
||
type noopCallback struct{} | ||
|
||
func (n noopCallback) PreStore(_ *types.Alert, _ bool) error { return nil } | ||
|
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 |
---|---|---|
|
@@ -108,11 +108,11 @@ type memMarker struct { | |
} | ||
|
||
func (m *memMarker) registerMetrics(r prometheus.Registerer) { | ||
newAlertMetricByState := func(st AlertState) prometheus.GaugeFunc { | ||
newMarkedAlertMetricByState := func(st AlertState) prometheus.GaugeFunc { | ||
return prometheus.NewGaugeFunc( | ||
prometheus.GaugeOpts{ | ||
Name: "alertmanager_alerts", | ||
Help: "How many alerts by state.", | ||
Name: "alertmanager_marked_alerts", | ||
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. Kept the old metric intact functionality-wise, just added more label to complete the story. |
||
Help: "How many alerts by state are currently marked in the Alertmanager regardless of their expiry.", | ||
ConstLabels: prometheus.Labels{"state": string(st)}, | ||
}, | ||
func() float64 { | ||
|
@@ -121,11 +121,13 @@ func (m *memMarker) registerMetrics(r prometheus.Registerer) { | |
) | ||
} | ||
|
||
alertsActive := newAlertMetricByState(AlertStateActive) | ||
alertsSuppressed := newAlertMetricByState(AlertStateSuppressed) | ||
alertsActive := newMarkedAlertMetricByState(AlertStateActive) | ||
alertsSuppressed := newMarkedAlertMetricByState(AlertStateSuppressed) | ||
alertStateUnprocessed := newMarkedAlertMetricByState(AlertStateUnprocessed) | ||
|
||
r.MustRegister(alertsActive) | ||
r.MustRegister(alertsSuppressed) | ||
r.MustRegister(alertStateUnprocessed) | ||
} | ||
|
||
// Count implements Marker. | ||
|
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
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.
Moved
alerts
becausemtx
is not protecting it and this was a bit confusing at first.alerts
has its own mutex.