labels.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. "strings"
  19. "unicode/utf8"
  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. Note that the
  67. // IsValid method of LabelName performs the same check but faster than a match
  68. // with this regular expression.
  69. var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
  70. // A LabelName is a key for a LabelSet or Metric. It has a value associated
  71. // therewith.
  72. type LabelName string
  73. // IsValid is true iff the label name matches the pattern of LabelNameRE. This
  74. // method, however, does not use LabelNameRE for the check but a much faster
  75. // hardcoded implementation.
  76. func (ln LabelName) IsValid() bool {
  77. if len(ln) == 0 {
  78. return false
  79. }
  80. for i, b := range ln {
  81. if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
  82. return false
  83. }
  84. }
  85. return true
  86. }
  87. // UnmarshalYAML implements the yaml.Unmarshaler interface.
  88. func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
  89. var s string
  90. if err := unmarshal(&s); err != nil {
  91. return err
  92. }
  93. if !LabelName(s).IsValid() {
  94. return fmt.Errorf("%q is not a valid label name", s)
  95. }
  96. *ln = LabelName(s)
  97. return nil
  98. }
  99. // UnmarshalJSON implements the json.Unmarshaler interface.
  100. func (ln *LabelName) UnmarshalJSON(b []byte) error {
  101. var s string
  102. if err := json.Unmarshal(b, &s); err != nil {
  103. return err
  104. }
  105. if !LabelName(s).IsValid() {
  106. return fmt.Errorf("%q is not a valid label name", s)
  107. }
  108. *ln = LabelName(s)
  109. return nil
  110. }
  111. // LabelNames is a sortable LabelName slice. In implements sort.Interface.
  112. type LabelNames []LabelName
  113. func (l LabelNames) Len() int {
  114. return len(l)
  115. }
  116. func (l LabelNames) Less(i, j int) bool {
  117. return l[i] < l[j]
  118. }
  119. func (l LabelNames) Swap(i, j int) {
  120. l[i], l[j] = l[j], l[i]
  121. }
  122. func (l LabelNames) String() string {
  123. labelStrings := make([]string, 0, len(l))
  124. for _, label := range l {
  125. labelStrings = append(labelStrings, string(label))
  126. }
  127. return strings.Join(labelStrings, ", ")
  128. }
  129. // A LabelValue is an associated value for a LabelName.
  130. type LabelValue string
  131. // IsValid returns true iff the string is a valid UTF8.
  132. func (lv LabelValue) IsValid() bool {
  133. return utf8.ValidString(string(lv))
  134. }
  135. // LabelValues is a sortable LabelValue slice. It implements sort.Interface.
  136. type LabelValues []LabelValue
  137. func (l LabelValues) Len() int {
  138. return len(l)
  139. }
  140. func (l LabelValues) Less(i, j int) bool {
  141. return string(l[i]) < string(l[j])
  142. }
  143. func (l LabelValues) Swap(i, j int) {
  144. l[i], l[j] = l[j], l[i]
  145. }
  146. // LabelPair pairs a name with a value.
  147. type LabelPair struct {
  148. Name LabelName
  149. Value LabelValue
  150. }
  151. // LabelPairs is a sortable slice of LabelPair pointers. It implements
  152. // sort.Interface.
  153. type LabelPairs []*LabelPair
  154. func (l LabelPairs) Len() int {
  155. return len(l)
  156. }
  157. func (l LabelPairs) Less(i, j int) bool {
  158. switch {
  159. case l[i].Name > l[j].Name:
  160. return false
  161. case l[i].Name < l[j].Name:
  162. return true
  163. case l[i].Value > l[j].Value:
  164. return false
  165. case l[i].Value < l[j].Value:
  166. return true
  167. default:
  168. return false
  169. }
  170. }
  171. func (l LabelPairs) Swap(i, j int) {
  172. l[i], l[j] = l[j], l[i]
  173. }