alert.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "fmt"
  16. "time"
  17. )
  18. type AlertStatus string
  19. const (
  20. AlertFiring AlertStatus = "firing"
  21. AlertResolved AlertStatus = "resolved"
  22. )
  23. // Alert is a generic representation of an alert in the Prometheus eco-system.
  24. type Alert struct {
  25. // Label value pairs for purpose of aggregation, matching, and disposition
  26. // dispatching. This must minimally include an "alertname" label.
  27. Labels LabelSet `json:"labels"`
  28. // Extra key/value information which does not define alert identity.
  29. Annotations LabelSet `json:"annotations"`
  30. // The known time range for this alert. Both ends are optional.
  31. StartsAt time.Time `json:"startsAt,omitempty"`
  32. EndsAt time.Time `json:"endsAt,omitempty"`
  33. }
  34. // Name returns the name of the alert. It is equivalent to the "alertname" label.
  35. func (a *Alert) Name() string {
  36. return string(a.Labels[AlertNameLabel])
  37. }
  38. // Fingerprint returns a unique hash for the alert. It is equivalent to
  39. // the fingerprint of the alert's label set.
  40. func (a *Alert) Fingerprint() Fingerprint {
  41. return a.Labels.Fingerprint()
  42. }
  43. func (a *Alert) String() string {
  44. s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7])
  45. if a.Resolved() {
  46. return s + "[resolved]"
  47. }
  48. return s + "[active]"
  49. }
  50. // Resolved returns true iff the activity interval ended in the past.
  51. func (a *Alert) Resolved() bool {
  52. if a.EndsAt.IsZero() {
  53. return false
  54. }
  55. return !a.EndsAt.After(time.Now())
  56. }
  57. // Status returns the status of the alert.
  58. func (a *Alert) Status() AlertStatus {
  59. if a.Resolved() {
  60. return AlertResolved
  61. }
  62. return AlertFiring
  63. }
  64. // Alert is a list of alerts that can be sorted in chronological order.
  65. type Alerts []*Alert
  66. func (as Alerts) Len() int { return len(as) }
  67. func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
  68. func (as Alerts) Less(i, j int) bool {
  69. if as[i].StartsAt.Before(as[j].StartsAt) {
  70. return true
  71. }
  72. if as[i].EndsAt.Before(as[j].EndsAt) {
  73. return true
  74. }
  75. return as[i].Fingerprint() < as[j].Fingerprint()
  76. }
  77. // HasFiring returns true iff one of the alerts is not resolved.
  78. func (as Alerts) HasFiring() bool {
  79. for _, a := range as {
  80. if !a.Resolved() {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. // Status returns StatusFiring iff at least one of the alerts is firing.
  87. func (as Alerts) Status() AlertStatus {
  88. if as.HasFiring() {
  89. return AlertFiring
  90. }
  91. return AlertResolved
  92. }