blob: 460f554f2945daf09ffb99bd4c4961fc84d35d68 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// Copyright 2013 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package model
15
16import (
Abhay Kumar40252eb2025-10-13 13:25:53 +000017 "errors"
khenaidoo59ce9dd2019-11-11 13:05:32 -050018 "fmt"
19 "time"
20)
21
22type AlertStatus string
23
24const (
25 AlertFiring AlertStatus = "firing"
26 AlertResolved AlertStatus = "resolved"
27)
28
29// Alert is a generic representation of an alert in the Prometheus eco-system.
30type Alert struct {
31 // Label value pairs for purpose of aggregation, matching, and disposition
32 // dispatching. This must minimally include an "alertname" label.
33 Labels LabelSet `json:"labels"`
34
35 // Extra key/value information which does not define alert identity.
36 Annotations LabelSet `json:"annotations"`
37
38 // The known time range for this alert. Both ends are optional.
39 StartsAt time.Time `json:"startsAt,omitempty"`
40 EndsAt time.Time `json:"endsAt,omitempty"`
41 GeneratorURL string `json:"generatorURL"`
42}
43
44// Name returns the name of the alert. It is equivalent to the "alertname" label.
45func (a *Alert) Name() string {
46 return string(a.Labels[AlertNameLabel])
47}
48
49// Fingerprint returns a unique hash for the alert. It is equivalent to
50// the fingerprint of the alert's label set.
51func (a *Alert) Fingerprint() Fingerprint {
52 return a.Labels.Fingerprint()
53}
54
55func (a *Alert) String() string {
56 s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7])
57 if a.Resolved() {
58 return s + "[resolved]"
59 }
60 return s + "[active]"
61}
62
63// Resolved returns true iff the activity interval ended in the past.
64func (a *Alert) Resolved() bool {
65 return a.ResolvedAt(time.Now())
66}
67
Abhay Kumar40252eb2025-10-13 13:25:53 +000068// ResolvedAt returns true iff the activity interval ended before
khenaidoo59ce9dd2019-11-11 13:05:32 -050069// the given timestamp.
70func (a *Alert) ResolvedAt(ts time.Time) bool {
71 if a.EndsAt.IsZero() {
72 return false
73 }
74 return !a.EndsAt.After(ts)
75}
76
77// Status returns the status of the alert.
78func (a *Alert) Status() AlertStatus {
Abhay Kumar40252eb2025-10-13 13:25:53 +000079 return a.StatusAt(time.Now())
80}
81
82// StatusAt returns the status of the alert at the given timestamp.
83func (a *Alert) StatusAt(ts time.Time) AlertStatus {
84 if a.ResolvedAt(ts) {
khenaidoo59ce9dd2019-11-11 13:05:32 -050085 return AlertResolved
86 }
87 return AlertFiring
88}
89
90// Validate checks whether the alert data is inconsistent.
91func (a *Alert) Validate() error {
92 if a.StartsAt.IsZero() {
Abhay Kumar40252eb2025-10-13 13:25:53 +000093 return errors.New("start time missing")
khenaidoo59ce9dd2019-11-11 13:05:32 -050094 }
95 if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
Abhay Kumar40252eb2025-10-13 13:25:53 +000096 return errors.New("start time must be before end time")
khenaidoo59ce9dd2019-11-11 13:05:32 -050097 }
98 if err := a.Labels.Validate(); err != nil {
Abhay Kumar40252eb2025-10-13 13:25:53 +000099 return fmt.Errorf("invalid label set: %w", err)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500100 }
101 if len(a.Labels) == 0 {
Abhay Kumar40252eb2025-10-13 13:25:53 +0000102 return errors.New("at least one label pair required")
khenaidoo59ce9dd2019-11-11 13:05:32 -0500103 }
104 if err := a.Annotations.Validate(); err != nil {
Abhay Kumar40252eb2025-10-13 13:25:53 +0000105 return fmt.Errorf("invalid annotations: %w", err)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500106 }
107 return nil
108}
109
110// Alert is a list of alerts that can be sorted in chronological order.
111type Alerts []*Alert
112
113func (as Alerts) Len() int { return len(as) }
114func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
115
116func (as Alerts) Less(i, j int) bool {
117 if as[i].StartsAt.Before(as[j].StartsAt) {
118 return true
119 }
120 if as[i].EndsAt.Before(as[j].EndsAt) {
121 return true
122 }
123 return as[i].Fingerprint() < as[j].Fingerprint()
124}
125
126// HasFiring returns true iff one of the alerts is not resolved.
127func (as Alerts) HasFiring() bool {
128 for _, a := range as {
129 if !a.Resolved() {
130 return true
131 }
132 }
133 return false
134}
135
Abhay Kumar40252eb2025-10-13 13:25:53 +0000136// HasFiringAt returns true iff one of the alerts is not resolved
137// at the time ts.
138func (as Alerts) HasFiringAt(ts time.Time) bool {
139 for _, a := range as {
140 if !a.ResolvedAt(ts) {
141 return true
142 }
143 }
144 return false
145}
146
khenaidoo59ce9dd2019-11-11 13:05:32 -0500147// Status returns StatusFiring iff at least one of the alerts is firing.
148func (as Alerts) Status() AlertStatus {
149 if as.HasFiring() {
150 return AlertFiring
151 }
152 return AlertResolved
153}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000154
155// StatusAt returns StatusFiring iff at least one of the alerts is firing
156// at the time ts.
157func (as Alerts) StatusAt(ts time.Time) AlertStatus {
158 if as.HasFiringAt(ts) {
159 return AlertFiring
160 }
161 return AlertResolved
162}