Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

107 řádky
2.8 KiB

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