labels.go 6.6 KB

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