本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.7KB

  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. GeneratorURL string `json:"generatorURL"`
  34. }
  35. // Name returns the name of the alert. It is equivalent to the "alertname" label.
  36. func (a *Alert) Name() string {
  37. return string(a.Labels[AlertNameLabel])
  38. }
  39. // Fingerprint returns a unique hash for the alert. It is equivalent to
  40. // the fingerprint of the alert's label set.
  41. func (a *Alert) Fingerprint() Fingerprint {
  42. return a.Labels.Fingerprint()
  43. }
  44. func (a *Alert) String() string {
  45. s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7])
  46. if a.Resolved() {
  47. return s + "[resolved]"
  48. }
  49. return s + "[active]"
  50. }
  51. // Resolved returns true iff the activity interval ended in the past.
  52. func (a *Alert) Resolved() bool {
  53. return a.ResolvedAt(time.Now())
  54. }
  55. // ResolvedAt returns true off the activity interval ended before
  56. // the given timestamp.
  57. func (a *Alert) ResolvedAt(ts time.Time) bool {
  58. if a.EndsAt.IsZero() {
  59. return false
  60. }
  61. return !a.EndsAt.After(ts)
  62. }
  63. // Status returns the status of the alert.
  64. func (a *Alert) Status() AlertStatus {
  65. if a.Resolved() {
  66. return AlertResolved
  67. }
  68. return AlertFiring
  69. }
  70. // Validate checks whether the alert data is inconsistent.
  71. func (a *Alert) Validate() error {
  72. if a.StartsAt.IsZero() {
  73. return fmt.Errorf("start time missing")
  74. }
  75. if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
  76. return fmt.Errorf("start time must be before end time")
  77. }
  78. if err := a.Labels.Validate(); err != nil {
  79. return fmt.Errorf("invalid label set: %s", err)
  80. }
  81. if len(a.Labels) == 0 {
  82. return fmt.Errorf("at least one label pair required")
  83. }
  84. if err := a.Annotations.Validate(); err != nil {
  85. return fmt.Errorf("invalid annotations: %s", err)
  86. }
  87. return nil
  88. }
  89. // Alert is a list of alerts that can be sorted in chronological order.
  90. type Alerts []*Alert
  91. func (as Alerts) Len() int { return len(as) }
  92. func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
  93. func (as Alerts) Less(i, j int) bool {
  94. if as[i].StartsAt.Before(as[j].StartsAt) {
  95. return true
  96. }
  97. if as[i].EndsAt.Before(as[j].EndsAt) {
  98. return true
  99. }
  100. return as[i].Fingerprint() < as[j].Fingerprint()
  101. }
  102. // HasFiring returns true iff one of the alerts is not resolved.
  103. func (as Alerts) HasFiring() bool {
  104. for _, a := range as {
  105. if !a.Resolved() {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. // Status returns StatusFiring iff at least one of the alerts is firing.
  112. func (as Alerts) Status() AlertStatus {
  113. if as.HasFiring() {
  114. return AlertFiring
  115. }
  116. return AlertResolved
  117. }
上海开阖软件有限公司 沪ICP备12045867号-1