alert.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. package model
  14. import (
  15. "errors"
  16. "fmt"
  17. "time"
  18. )
  19. type AlertStatus string
  20. const (
  21. AlertFiring AlertStatus = "firing"
  22. AlertResolved AlertStatus = "resolved"
  23. )
  24. // Alert is a generic representation of an alert in the Prometheus eco-system.
  25. type Alert struct {
  26. // Label value pairs for purpose of aggregation, matching, and disposition
  27. // dispatching. This must minimally include an "alertname" label.
  28. Labels LabelSet `json:"labels"`
  29. // Extra key/value information which does not define alert identity.
  30. Annotations LabelSet `json:"annotations"`
  31. // The known time range for this alert. Both ends are optional.
  32. StartsAt time.Time `json:"startsAt,omitempty"`
  33. EndsAt time.Time `json:"endsAt,omitempty"`
  34. GeneratorURL string `json:"generatorURL"`
  35. }
  36. // Name returns the name of the alert. It is equivalent to the "alertname" label.
  37. func (a *Alert) Name() string {
  38. return string(a.Labels[AlertNameLabel])
  39. }
  40. // Fingerprint returns a unique hash for the alert. It is equivalent to
  41. // the fingerprint of the alert's label set.
  42. func (a *Alert) Fingerprint() Fingerprint {
  43. return a.Labels.Fingerprint()
  44. }
  45. func (a *Alert) String() string {
  46. s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7])
  47. if a.Resolved() {
  48. return s + "[resolved]"
  49. }
  50. return s + "[active]"
  51. }
  52. // Resolved returns true iff the activity interval ended in the past.
  53. func (a *Alert) Resolved() bool {
  54. return a.ResolvedAt(time.Now())
  55. }
  56. // ResolvedAt returns true iff the activity interval ended before
  57. // the given timestamp.
  58. func (a *Alert) ResolvedAt(ts time.Time) bool {
  59. if a.EndsAt.IsZero() {
  60. return false
  61. }
  62. return !a.EndsAt.After(ts)
  63. }
  64. // Status returns the status of the alert.
  65. func (a *Alert) Status() AlertStatus {
  66. return a.StatusAt(time.Now())
  67. }
  68. // StatusAt returns the status of the alert at the given timestamp.
  69. func (a *Alert) StatusAt(ts time.Time) AlertStatus {
  70. if a.ResolvedAt(ts) {
  71. return AlertResolved
  72. }
  73. return AlertFiring
  74. }
  75. // Validate checks whether the alert data is inconsistent.
  76. func (a *Alert) Validate() error {
  77. if a.StartsAt.IsZero() {
  78. return errors.New("start time missing")
  79. }
  80. if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
  81. return errors.New("start time must be before end time")
  82. }
  83. if err := a.Labels.Validate(); err != nil {
  84. return fmt.Errorf("invalid label set: %w", err)
  85. }
  86. if len(a.Labels) == 0 {
  87. return errors.New("at least one label pair required")
  88. }
  89. if err := a.Annotations.Validate(); err != nil {
  90. return fmt.Errorf("invalid annotations: %w", err)
  91. }
  92. return nil
  93. }
  94. // Alert is a list of alerts that can be sorted in chronological order.
  95. type Alerts []*Alert
  96. func (as Alerts) Len() int { return len(as) }
  97. func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
  98. func (as Alerts) Less(i, j int) bool {
  99. if as[i].StartsAt.Before(as[j].StartsAt) {
  100. return true
  101. }
  102. if as[i].EndsAt.Before(as[j].EndsAt) {
  103. return true
  104. }
  105. return as[i].Fingerprint() < as[j].Fingerprint()
  106. }
  107. // HasFiring returns true iff one of the alerts is not resolved.
  108. func (as Alerts) HasFiring() bool {
  109. for _, a := range as {
  110. if !a.Resolved() {
  111. return true
  112. }
  113. }
  114. return false
  115. }
  116. // HasFiringAt returns true iff one of the alerts is not resolved
  117. // at the time ts.
  118. func (as Alerts) HasFiringAt(ts time.Time) bool {
  119. for _, a := range as {
  120. if !a.ResolvedAt(ts) {
  121. return true
  122. }
  123. }
  124. return false
  125. }
  126. // Status returns StatusFiring iff at least one of the alerts is firing.
  127. func (as Alerts) Status() AlertStatus {
  128. if as.HasFiring() {
  129. return AlertFiring
  130. }
  131. return AlertResolved
  132. }
  133. // StatusAt returns StatusFiring iff at least one of the alerts is firing
  134. // at the time ts.
  135. func (as Alerts) StatusAt(ts time.Time) AlertStatus {
  136. if as.HasFiringAt(ts) {
  137. return AlertFiring
  138. }
  139. return AlertResolved
  140. }