mirror of
https://github.com/prometheus/prometheus.git
synced 2025-10-23 21:41:00 +02:00
fix: Updates unknown state to -1,adds fix for failing test case
Signed-off-by: Sahil Rasaikar <sahil.rasaikar@gmail.com>
This commit is contained in:
parent
516afbea67
commit
cc1e6e40f0
@ -50,6 +50,8 @@ const (
|
|||||||
type AlertState int
|
type AlertState int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// StateUnknown is the state of an alert that has not yet been evaluated.
|
||||||
|
StateUnknown AlertState = -1
|
||||||
// StateInactive is the state of an alert that is neither firing nor pending.
|
// StateInactive is the state of an alert that is neither firing nor pending.
|
||||||
StateInactive AlertState = iota
|
StateInactive AlertState = iota
|
||||||
// StatePending is the state of an alert that has been active for less than
|
// StatePending is the state of an alert that has been active for less than
|
||||||
@ -58,8 +60,6 @@ const (
|
|||||||
// StateFiring is the state of an alert that has been active for longer than
|
// StateFiring is the state of an alert that has been active for longer than
|
||||||
// the configured threshold duration.
|
// the configured threshold duration.
|
||||||
StateFiring
|
StateFiring
|
||||||
// StateUnknown is the state of an alert that has not yet been evaluated.
|
|
||||||
StateUnknown
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s AlertState) String() string {
|
func (s AlertState) String() string {
|
||||||
@ -536,14 +536,15 @@ func (r *AlertingRule) Eval(ctx context.Context, queryOffset time.Duration, ts t
|
|||||||
// State returns the maximum state of alert instances for this rule.
|
// State returns the maximum state of alert instances for this rule.
|
||||||
// StateFiring > StatePending > StateInactive > StateUnknown.
|
// StateFiring > StatePending > StateInactive > StateUnknown.
|
||||||
func (r *AlertingRule) State() AlertState {
|
func (r *AlertingRule) State() AlertState {
|
||||||
// If the rule has never been evaluated, return StateUnknown
|
|
||||||
if r.GetEvaluationTimestamp().IsZero() {
|
|
||||||
return StateUnknown
|
|
||||||
}
|
|
||||||
|
|
||||||
r.activeMtx.Lock()
|
r.activeMtx.Lock()
|
||||||
defer r.activeMtx.Unlock()
|
defer r.activeMtx.Unlock()
|
||||||
|
|
||||||
|
// Check if the rule has been evaluated
|
||||||
|
if r.evaluationTimestamp.Load().IsZero() {
|
||||||
|
return StateUnknown
|
||||||
|
}
|
||||||
|
|
||||||
maxState := StateInactive
|
maxState := StateInactive
|
||||||
for _, a := range r.active {
|
for _, a := range r.active {
|
||||||
if a.State > maxState {
|
if a.State > maxState {
|
||||||
|
@ -85,6 +85,8 @@ func TestAlertingRuleState(t *testing.T) {
|
|||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
rule := NewAlertingRule(test.name, nil, 0, 0, labels.EmptyLabels(), labels.EmptyLabels(), labels.EmptyLabels(), "", true, nil)
|
rule := NewAlertingRule(test.name, nil, 0, 0, labels.EmptyLabels(), labels.EmptyLabels(), labels.EmptyLabels(), "", true, nil)
|
||||||
rule.active = test.active
|
rule.active = test.active
|
||||||
|
// Set evaluation timestamp to simulate that the rule has been evaluated
|
||||||
|
rule.SetEvaluationTimestamp(time.Now())
|
||||||
got := rule.State()
|
got := rule.State()
|
||||||
require.Equal(t, test.want, got, "test case %d unexpected AlertState, want:%d got:%d", i, test.want, got)
|
require.Equal(t, test.want, got, "test case %d unexpected AlertState, want:%d got:%d", i, test.want, got)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user