silence.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015 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. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "regexp"
  19. "time"
  20. )
  21. // Matcher describes a matches the value of a given label.
  22. type Matcher struct {
  23. Name LabelName `json:"name"`
  24. Value string `json:"value"`
  25. IsRegex bool `json:"isRegex"`
  26. }
  27. func (m *Matcher) UnmarshalJSON(b []byte) error {
  28. type plain Matcher
  29. if err := json.Unmarshal(b, (*plain)(m)); err != nil {
  30. return err
  31. }
  32. if len(m.Name) == 0 {
  33. return errors.New("label name in matcher must not be empty")
  34. }
  35. if m.IsRegex {
  36. if _, err := regexp.Compile(m.Value); err != nil {
  37. return err
  38. }
  39. }
  40. return nil
  41. }
  42. // Validate returns true iff all fields of the matcher have valid values.
  43. func (m *Matcher) Validate() error {
  44. if !m.Name.IsValid() {
  45. return fmt.Errorf("invalid name %q", m.Name)
  46. }
  47. if m.IsRegex {
  48. if _, err := regexp.Compile(m.Value); err != nil {
  49. return fmt.Errorf("invalid regular expression %q", m.Value)
  50. }
  51. } else if !LabelValue(m.Value).IsValid() || len(m.Value) == 0 {
  52. return fmt.Errorf("invalid value %q", m.Value)
  53. }
  54. return nil
  55. }
  56. // Silence defines the representation of a silence definition in the Prometheus
  57. // eco-system.
  58. type Silence struct {
  59. ID uint64 `json:"id,omitempty"`
  60. Matchers []*Matcher `json:"matchers"`
  61. StartsAt time.Time `json:"startsAt"`
  62. EndsAt time.Time `json:"endsAt"`
  63. CreatedAt time.Time `json:"createdAt,omitempty"`
  64. CreatedBy string `json:"createdBy"`
  65. Comment string `json:"comment,omitempty"`
  66. }
  67. // Validate returns true iff all fields of the silence have valid values.
  68. func (s *Silence) Validate() error {
  69. if len(s.Matchers) == 0 {
  70. return errors.New("at least one matcher required")
  71. }
  72. for _, m := range s.Matchers {
  73. if err := m.Validate(); err != nil {
  74. return fmt.Errorf("invalid matcher: %w", err)
  75. }
  76. }
  77. if s.StartsAt.IsZero() {
  78. return errors.New("start time missing")
  79. }
  80. if s.EndsAt.IsZero() {
  81. return errors.New("end time missing")
  82. }
  83. if s.EndsAt.Before(s.StartsAt) {
  84. return errors.New("start time must be before end time")
  85. }
  86. if s.CreatedBy == "" {
  87. return errors.New("creator information missing")
  88. }
  89. if s.Comment == "" {
  90. return errors.New("comment missing")
  91. }
  92. if s.CreatedAt.IsZero() {
  93. return errors.New("creation timestamp missing")
  94. }
  95. return nil
  96. }