labels.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // ScrapeIntervalLabel is the name of the label that holds the scrape interval
  40. // used to scrape a target.
  41. ScrapeIntervalLabel = "__scrape_interval__"
  42. // ScrapeTimeoutLabel is the name of the label that holds the scrape
  43. // timeout used to scrape a target.
  44. ScrapeTimeoutLabel = "__scrape_timeout__"
  45. // ReservedLabelPrefix is a prefix which is not legal in user-supplied
  46. // label names.
  47. ReservedLabelPrefix = "__"
  48. // MetaLabelPrefix is a prefix for labels that provide meta information.
  49. // Labels with this prefix are used for intermediate label processing and
  50. // will not be attached to time series.
  51. MetaLabelPrefix = "__meta_"
  52. // TmpLabelPrefix is a prefix for temporary labels as part of relabelling.
  53. // Labels with this prefix are used for intermediate label processing and
  54. // will not be attached to time series. This is reserved for use in
  55. // Prometheus configuration files by users.
  56. TmpLabelPrefix = "__tmp_"
  57. // ParamLabelPrefix is a prefix for labels that provide URL parameters
  58. // used to scrape a target.
  59. ParamLabelPrefix = "__param_"
  60. // JobLabel is the label name indicating the job from which a timeseries
  61. // was scraped.
  62. JobLabel = "job"
  63. // InstanceLabel is the label name used for the instance label.
  64. InstanceLabel = "instance"
  65. // BucketLabel is used for the label that defines the upper bound of a
  66. // bucket of a histogram ("le" -> "less or equal").
  67. BucketLabel = "le"
  68. // QuantileLabel is used for the label that defines the quantile in a
  69. // summary.
  70. QuantileLabel = "quantile"
  71. )
  72. // LabelNameRE is a regular expression matching valid label names. Note that the
  73. // IsValid method of LabelName performs the same check but faster than a match
  74. // with this regular expression.
  75. var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
  76. // A LabelName is a key for a LabelSet or Metric. It has a value associated
  77. // therewith.
  78. type LabelName string
  79. // IsValid is true iff the label name matches the pattern of LabelNameRE. This
  80. // method, however, does not use LabelNameRE for the check but a much faster
  81. // hardcoded implementation.
  82. func (ln LabelName) IsValid() bool {
  83. if len(ln) == 0 {
  84. return false
  85. }
  86. for i, b := range ln {
  87. if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
  88. return false
  89. }
  90. }
  91. return true
  92. }
  93. // UnmarshalYAML implements the yaml.Unmarshaler interface.
  94. func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
  95. var s string
  96. if err := unmarshal(&s); err != nil {
  97. return err
  98. }
  99. if !LabelName(s).IsValid() {
  100. return fmt.Errorf("%q is not a valid label name", s)
  101. }
  102. *ln = LabelName(s)
  103. return nil
  104. }
  105. // UnmarshalJSON implements the json.Unmarshaler interface.
  106. func (ln *LabelName) UnmarshalJSON(b []byte) error {
  107. var s string
  108. if err := json.Unmarshal(b, &s); err != nil {
  109. return err
  110. }
  111. if !LabelName(s).IsValid() {
  112. return fmt.Errorf("%q is not a valid label name", s)
  113. }
  114. *ln = LabelName(s)
  115. return nil
  116. }
  117. // LabelNames is a sortable LabelName slice. In implements sort.Interface.
  118. type LabelNames []LabelName
  119. func (l LabelNames) Len() int {
  120. return len(l)
  121. }
  122. func (l LabelNames) Less(i, j int) bool {
  123. return l[i] < l[j]
  124. }
  125. func (l LabelNames) Swap(i, j int) {
  126. l[i], l[j] = l[j], l[i]
  127. }
  128. func (l LabelNames) String() string {
  129. labelStrings := make([]string, 0, len(l))
  130. for _, label := range l {
  131. labelStrings = append(labelStrings, string(label))
  132. }
  133. return strings.Join(labelStrings, ", ")
  134. }
  135. // A LabelValue is an associated value for a LabelName.
  136. type LabelValue string
  137. // IsValid returns true iff the string is a valid UTF8.
  138. func (lv LabelValue) IsValid() bool {
  139. return utf8.ValidString(string(lv))
  140. }
  141. // LabelValues is a sortable LabelValue slice. It implements sort.Interface.
  142. type LabelValues []LabelValue
  143. func (l LabelValues) Len() int {
  144. return len(l)
  145. }
  146. func (l LabelValues) Less(i, j int) bool {
  147. return string(l[i]) < string(l[j])
  148. }
  149. func (l LabelValues) Swap(i, j int) {
  150. l[i], l[j] = l[j], l[i]
  151. }
  152. // LabelPair pairs a name with a value.
  153. type LabelPair struct {
  154. Name LabelName
  155. Value LabelValue
  156. }
  157. // LabelPairs is a sortable slice of LabelPair pointers. It implements
  158. // sort.Interface.
  159. type LabelPairs []*LabelPair
  160. func (l LabelPairs) Len() int {
  161. return len(l)
  162. }
  163. func (l LabelPairs) Less(i, j int) bool {
  164. switch {
  165. case l[i].Name > l[j].Name:
  166. return false
  167. case l[i].Name < l[j].Name:
  168. return true
  169. case l[i].Value > l[j].Value:
  170. return false
  171. case l[i].Value < l[j].Value:
  172. return true
  173. default:
  174. return false
  175. }
  176. }
  177. func (l LabelPairs) Swap(i, j int) {
  178. l[i], l[j] = l[j], l[i]
  179. }