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
6 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 // AlertNameLabel is the name of the label containing the an alert's name.
26 AlertNameLabel = "alertname"
28 // ExportedLabelPrefix is the prefix to prepend to the label names present in
29 // exported metrics if a label of the same name is added by the server.
30 ExportedLabelPrefix = "exported_"
32 // MetricNameLabel is the label name indicating the metric name of a
34 MetricNameLabel = "__name__"
36 // SchemeLabel is the name of the label that holds the scheme on which to
38 SchemeLabel = "__scheme__"
40 // AddressLabel is the name of the label that holds the address of
42 AddressLabel = "__address__"
44 // MetricsPathLabel is the name of the label that holds the path on which to
46 MetricsPathLabel = "__metrics_path__"
48 // ReservedLabelPrefix is a prefix which is not legal in user-supplied
50 ReservedLabelPrefix = "__"
52 // MetaLabelPrefix is a prefix for labels that provide meta information.
53 // Labels with this prefix are used for intermediate label processing and
54 // will not be attached to time series.
55 MetaLabelPrefix = "__meta_"
57 // TmpLabelPrefix is a prefix for temporary labels as part of relabelling.
58 // Labels with this prefix are used for intermediate label processing and
59 // will not be attached to time series. This is reserved for use in
60 // Prometheus configuration files by users.
61 TmpLabelPrefix = "__tmp_"
63 // ParamLabelPrefix is a prefix for labels that provide URL parameters
64 // used to scrape a target.
65 ParamLabelPrefix = "__param_"
67 // JobLabel is the label name indicating the job from which a timeseries
71 // InstanceLabel is the label name used for the instance label.
72 InstanceLabel = "instance"
74 // BucketLabel is used for the label that defines the upper bound of a
75 // bucket of a histogram ("le" -> "less or equal").
78 // QuantileLabel is used for the label that defines the quantile in a
80 QuantileLabel = "quantile"
83 // LabelNameRE is a regular expression matching valid label names. Note that the
84 // IsValid method of LabelName performs the same check but faster than a match
85 // with this regular expression.
86 var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
88 // A LabelName is a key for a LabelSet or Metric. It has a value associated
92 // IsValid is true iff the label name matches the pattern of LabelNameRE. This
93 // method, however, does not use LabelNameRE for the check but a much faster
94 // hardcoded implementation.
95 func (ln LabelName) IsValid() bool {
99 for i, b := range ln {
100 if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
107 // UnmarshalYAML implements the yaml.Unmarshaler interface.
108 func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
110 if err := unmarshal(&s); err != nil {
113 if !LabelName(s).IsValid() {
114 return fmt.Errorf("%q is not a valid label name", s)
120 // UnmarshalJSON implements the json.Unmarshaler interface.
121 func (ln *LabelName) UnmarshalJSON(b []byte) error {
123 if err := json.Unmarshal(b, &s); err != nil {
126 if !LabelName(s).IsValid() {
127 return fmt.Errorf("%q is not a valid label name", s)
133 // LabelNames is a sortable LabelName slice. In implements sort.Interface.
134 type LabelNames []LabelName
136 func (l LabelNames) Len() int {
140 func (l LabelNames) Less(i, j int) bool {
144 func (l LabelNames) Swap(i, j int) {
145 l[i], l[j] = l[j], l[i]
148 func (l LabelNames) String() string {
149 labelStrings := make([]string, 0, len(l))
150 for _, label := range l {
151 labelStrings = append(labelStrings, string(label))
153 return strings.Join(labelStrings, ", ")
156 // A LabelValue is an associated value for a LabelName.
157 type LabelValue string
159 // IsValid returns true iff the string is a valid UTF8.
160 func (lv LabelValue) IsValid() bool {
161 return utf8.ValidString(string(lv))
164 // LabelValues is a sortable LabelValue slice. It implements sort.Interface.
165 type LabelValues []LabelValue
167 func (l LabelValues) Len() int {
171 func (l LabelValues) Less(i, j int) bool {
172 return string(l[i]) < string(l[j])
175 func (l LabelValues) Swap(i, j int) {
176 l[i], l[j] = l[j], l[i]
179 // LabelPair pairs a name with a value.
180 type LabelPair struct {
185 // LabelPairs is a sortable slice of LabelPair pointers. It implements
187 type LabelPairs []*LabelPair
189 func (l LabelPairs) Len() int {
193 func (l LabelPairs) Less(i, j int) bool {
195 case l[i].Name > l[j].Name:
197 case l[i].Name < l[j].Name:
199 case l[i].Value > l[j].Value:
201 case l[i].Value < l[j].Value:
208 func (l LabelPairs) Swap(i, j int) {
209 l[i], l[j] = l[j], l[i]