labels.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "encoding/json"
  16. "fmt"
  17. "regexp"
  18. "sort"
  19. "strings"
  20. )
  21. const (
  22. // AlertNameLabel is the name of the label containing the an alert's name.
  23. AlertNameLabel = "alertname"
  24. // ExportedLabelPrefix is the prefix to prepend to the label names present in
  25. // exported metrics if a label of the same name is added by the server.
  26. ExportedLabelPrefix = "exported_"
  27. // MetricNameLabel is the label name indicating the metric name of a
  28. // timeseries.
  29. MetricNameLabel = "__name__"
  30. // SchemeLabel is the name of the label that holds the scheme on which to
  31. // scrape a target.
  32. SchemeLabel = "__scheme__"
  33. // AddressLabel is the name of the label that holds the address of
  34. // a scrape target.
  35. AddressLabel = "__address__"
  36. // MetricsPathLabel is the name of the label that holds the path on which to
  37. // scrape a target.
  38. MetricsPathLabel = "__metrics_path__"
  39. // ReservedLabelPrefix is a prefix which is not legal in user-supplied
  40. // label names.
  41. ReservedLabelPrefix = "__"
  42. // MetaLabelPrefix is a prefix for labels that provide meta information.
  43. // Labels with this prefix are used for intermediate label processing and
  44. // will not be attached to time series.
  45. MetaLabelPrefix = "__meta_"
  46. // TmpLabelPrefix is a prefix for temporary labels as part of relabelling.
  47. // Labels with this prefix are used for intermediate label processing and
  48. // will not be attached to time series. This is reserved for use in
  49. // Prometheus configuration files by users.
  50. TmpLabelPrefix = "__tmp_"
  51. // ParamLabelPrefix is a prefix for labels that provide URL parameters
  52. // used to scrape a target.
  53. ParamLabelPrefix = "__param_"
  54. // JobLabel is the label name indicating the job from which a timeseries
  55. // was scraped.
  56. JobLabel = "job"
  57. // InstanceLabel is the label name used for the instance label.
  58. InstanceLabel = "instance"
  59. // BucketLabel is used for the label that defines the upper bound of a
  60. // bucket of a histogram ("le" -> "less or equal").
  61. BucketLabel = "le"
  62. // QuantileLabel is used for the label that defines the quantile in a
  63. // summary.
  64. QuantileLabel = "quantile"
  65. )
  66. // LabelNameRE is a regular expression matching valid label names.
  67. var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
  68. // A LabelName is a key for a LabelSet or Metric. It has a value associated
  69. // therewith.
  70. type LabelName string
  71. // UnmarshalYAML implements the yaml.Unmarshaler interface.
  72. func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
  73. var s string
  74. if err := unmarshal(&s); err != nil {
  75. return err
  76. }
  77. if !LabelNameRE.MatchString(s) {
  78. return fmt.Errorf("%q is not a valid label name", s)
  79. }
  80. *ln = LabelName(s)
  81. return nil
  82. }
  83. // UnmarshalJSON implements the json.Unmarshaler interface.
  84. func (ln *LabelName) UnmarshalJSON(b []byte) error {
  85. var s string
  86. if err := json.Unmarshal(b, &s); err != nil {
  87. return err
  88. }
  89. if !LabelNameRE.MatchString(s) {
  90. return fmt.Errorf("%q is not a valid label name", s)
  91. }
  92. *ln = LabelName(s)
  93. return nil
  94. }
  95. // LabelNames is a sortable LabelName slice. In implements sort.Interface.
  96. type LabelNames []LabelName
  97. func (l LabelNames) Len() int {
  98. return len(l)
  99. }
  100. func (l LabelNames) Less(i, j int) bool {
  101. return l[i] < l[j]
  102. }
  103. func (l LabelNames) Swap(i, j int) {
  104. l[i], l[j] = l[j], l[i]
  105. }
  106. func (l LabelNames) String() string {
  107. labelStrings := make([]string, 0, len(l))
  108. for _, label := range l {
  109. labelStrings = append(labelStrings, string(label))
  110. }
  111. return strings.Join(labelStrings, ", ")
  112. }
  113. // A LabelValue is an associated value for a LabelName.
  114. type LabelValue string
  115. // LabelValues is a sortable LabelValue slice. It implements sort.Interface.
  116. type LabelValues []LabelValue
  117. func (l LabelValues) Len() int {
  118. return len(l)
  119. }
  120. func (l LabelValues) Less(i, j int) bool {
  121. return sort.StringsAreSorted([]string{string(l[i]), string(l[j])})
  122. }
  123. func (l LabelValues) Swap(i, j int) {
  124. l[i], l[j] = l[j], l[i]
  125. }
  126. // LabelPair pairs a name with a value.
  127. type LabelPair struct {
  128. Name LabelName
  129. Value LabelValue
  130. }
  131. // LabelPairs is a sortable slice of LabelPair pointers. It implements
  132. // sort.Interface.
  133. type LabelPairs []*LabelPair
  134. func (l LabelPairs) Len() int {
  135. return len(l)
  136. }
  137. func (l LabelPairs) Less(i, j int) bool {
  138. switch {
  139. case l[i].Name > l[j].Name:
  140. return false
  141. case l[i].Name < l[j].Name:
  142. return true
  143. case l[i].Value > l[j].Value:
  144. return false
  145. case l[i].Value < l[j].Value:
  146. return true
  147. default:
  148. return false
  149. }
  150. }
  151. func (l LabelPairs) Swap(i, j int) {
  152. l[i], l[j] = l[j], l[i]
  153. }