silence.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // Silence defines the representation of a silence definiton
  42. // in the Prometheus eco-system.
  43. type Silence struct {
  44. ID uint64 `json:"id,omitempty"`
  45. Matchers []*Matcher `json:"matchers"`
  46. StartsAt time.Time `json:"startsAt"`
  47. EndsAt time.Time `json:"endsAt"`
  48. CreatedAt time.Time `json:"createdAt,omitempty"`
  49. CreatedBy string `json:"createdBy"`
  50. Comment string `json:"comment,omitempty"`
  51. }