| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // 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 | |
| 14 | package model |
| 15 | |
| 16 | import ( |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 17 | "errors" |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 18 | "fmt" |
| 19 | "time" |
| 20 | ) |
| 21 | |
| 22 | type AlertStatus string |
| 23 | |
| 24 | const ( |
| 25 | AlertFiring AlertStatus = "firing" |
| 26 | AlertResolved AlertStatus = "resolved" |
| 27 | ) |
| 28 | |
| 29 | // Alert is a generic representation of an alert in the Prometheus eco-system. |
| 30 | type 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. |
| 45 | func (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. |
| 51 | func (a *Alert) Fingerprint() Fingerprint { |
| 52 | return a.Labels.Fingerprint() |
| 53 | } |
| 54 | |
| 55 | func (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. |
| 64 | func (a *Alert) Resolved() bool { |
| 65 | return a.ResolvedAt(time.Now()) |
| 66 | } |
| 67 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 68 | // ResolvedAt returns true iff the activity interval ended before |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 69 | // the given timestamp. |
| 70 | func (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. |
| 78 | func (a *Alert) Status() AlertStatus { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 79 | return a.StatusAt(time.Now()) |
| 80 | } |
| 81 | |
| 82 | // StatusAt returns the status of the alert at the given timestamp. |
| 83 | func (a *Alert) StatusAt(ts time.Time) AlertStatus { |
| 84 | if a.ResolvedAt(ts) { |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 85 | return AlertResolved |
| 86 | } |
| 87 | return AlertFiring |
| 88 | } |
| 89 | |
| 90 | // Validate checks whether the alert data is inconsistent. |
| 91 | func (a *Alert) Validate() error { |
| 92 | if a.StartsAt.IsZero() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 93 | return errors.New("start time missing") |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 94 | } |
| 95 | if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 96 | return errors.New("start time must be before end time") |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 97 | } |
| 98 | if err := a.Labels.Validate(); err != nil { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 99 | return fmt.Errorf("invalid label set: %w", err) |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 100 | } |
| 101 | if len(a.Labels) == 0 { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 102 | return errors.New("at least one label pair required") |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 103 | } |
| 104 | if err := a.Annotations.Validate(); err != nil { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 105 | return fmt.Errorf("invalid annotations: %w", err) |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 106 | } |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | // Alert is a list of alerts that can be sorted in chronological order. |
| 111 | type Alerts []*Alert |
| 112 | |
| 113 | func (as Alerts) Len() int { return len(as) } |
| 114 | func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] } |
| 115 | |
| 116 | func (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. |
| 127 | func (as Alerts) HasFiring() bool { |
| 128 | for _, a := range as { |
| 129 | if !a.Resolved() { |
| 130 | return true |
| 131 | } |
| 132 | } |
| 133 | return false |
| 134 | } |
| 135 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 136 | // HasFiringAt returns true iff one of the alerts is not resolved |
| 137 | // at the time ts. |
| 138 | func (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 | |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 147 | // Status returns StatusFiring iff at least one of the alerts is firing. |
| 148 | func (as Alerts) Status() AlertStatus { |
| 149 | if as.HasFiring() { |
| 150 | return AlertFiring |
| 151 | } |
| 152 | return AlertResolved |
| 153 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 154 | |
| 155 | // StatusAt returns StatusFiring iff at least one of the alerts is firing |
| 156 | // at the time ts. |
| 157 | func (as Alerts) StatusAt(ts time.Time) AlertStatus { |
| 158 | if as.HasFiringAt(ts) { |
| 159 | return AlertFiring |
| 160 | } |
| 161 | return AlertResolved |
| 162 | } |