cubefs
218 строк · 5.9 Кб
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
14package model
15
16import (
17"encoding/json"
18"fmt"
19"regexp"
20"strings"
21"unicode/utf8"
22)
23
24const (
25// AlertNameLabel is the name of the label containing the an alert's name.
26AlertNameLabel = "alertname"
27
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.
30ExportedLabelPrefix = "exported_"
31
32// MetricNameLabel is the label name indicating the metric name of a
33// timeseries.
34MetricNameLabel = "__name__"
35
36// SchemeLabel is the name of the label that holds the scheme on which to
37// scrape a target.
38SchemeLabel = "__scheme__"
39
40// AddressLabel is the name of the label that holds the address of
41// a scrape target.
42AddressLabel = "__address__"
43
44// MetricsPathLabel is the name of the label that holds the path on which to
45// scrape a target.
46MetricsPathLabel = "__metrics_path__"
47
48// ScrapeIntervalLabel is the name of the label that holds the scrape interval
49// used to scrape a target.
50ScrapeIntervalLabel = "__scrape_interval__"
51
52// ScrapeTimeoutLabel is the name of the label that holds the scrape
53// timeout used to scrape a target.
54ScrapeTimeoutLabel = "__scrape_timeout__"
55
56// ReservedLabelPrefix is a prefix which is not legal in user-supplied
57// label names.
58ReservedLabelPrefix = "__"
59
60// MetaLabelPrefix is a prefix for labels that provide meta information.
61// Labels with this prefix are used for intermediate label processing and
62// will not be attached to time series.
63MetaLabelPrefix = "__meta_"
64
65// TmpLabelPrefix is a prefix for temporary labels as part of relabelling.
66// Labels with this prefix are used for intermediate label processing and
67// will not be attached to time series. This is reserved for use in
68// Prometheus configuration files by users.
69TmpLabelPrefix = "__tmp_"
70
71// ParamLabelPrefix is a prefix for labels that provide URL parameters
72// used to scrape a target.
73ParamLabelPrefix = "__param_"
74
75// JobLabel is the label name indicating the job from which a timeseries
76// was scraped.
77JobLabel = "job"
78
79// InstanceLabel is the label name used for the instance label.
80InstanceLabel = "instance"
81
82// BucketLabel is used for the label that defines the upper bound of a
83// bucket of a histogram ("le" -> "less or equal").
84BucketLabel = "le"
85
86// QuantileLabel is used for the label that defines the quantile in a
87// summary.
88QuantileLabel = "quantile"
89)
90
91// LabelNameRE is a regular expression matching valid label names. Note that the
92// IsValid method of LabelName performs the same check but faster than a match
93// with this regular expression.
94var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
95
96// A LabelName is a key for a LabelSet or Metric. It has a value associated
97// therewith.
98type LabelName string
99
100// IsValid is true iff the label name matches the pattern of LabelNameRE. This
101// method, however, does not use LabelNameRE for the check but a much faster
102// hardcoded implementation.
103func (ln LabelName) IsValid() bool {
104if len(ln) == 0 {
105return false
106}
107for i, b := range ln {
108if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
109return false
110}
111}
112return true
113}
114
115// UnmarshalYAML implements the yaml.Unmarshaler interface.
116func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
117var s string
118if err := unmarshal(&s); err != nil {
119return err
120}
121if !LabelName(s).IsValid() {
122return fmt.Errorf("%q is not a valid label name", s)
123}
124*ln = LabelName(s)
125return nil
126}
127
128// UnmarshalJSON implements the json.Unmarshaler interface.
129func (ln *LabelName) UnmarshalJSON(b []byte) error {
130var s string
131if err := json.Unmarshal(b, &s); err != nil {
132return err
133}
134if !LabelName(s).IsValid() {
135return fmt.Errorf("%q is not a valid label name", s)
136}
137*ln = LabelName(s)
138return nil
139}
140
141// LabelNames is a sortable LabelName slice. In implements sort.Interface.
142type LabelNames []LabelName
143
144func (l LabelNames) Len() int {
145return len(l)
146}
147
148func (l LabelNames) Less(i, j int) bool {
149return l[i] < l[j]
150}
151
152func (l LabelNames) Swap(i, j int) {
153l[i], l[j] = l[j], l[i]
154}
155
156func (l LabelNames) String() string {
157labelStrings := make([]string, 0, len(l))
158for _, label := range l {
159labelStrings = append(labelStrings, string(label))
160}
161return strings.Join(labelStrings, ", ")
162}
163
164// A LabelValue is an associated value for a LabelName.
165type LabelValue string
166
167// IsValid returns true iff the string is a valid UTF8.
168func (lv LabelValue) IsValid() bool {
169return utf8.ValidString(string(lv))
170}
171
172// LabelValues is a sortable LabelValue slice. It implements sort.Interface.
173type LabelValues []LabelValue
174
175func (l LabelValues) Len() int {
176return len(l)
177}
178
179func (l LabelValues) Less(i, j int) bool {
180return string(l[i]) < string(l[j])
181}
182
183func (l LabelValues) Swap(i, j int) {
184l[i], l[j] = l[j], l[i]
185}
186
187// LabelPair pairs a name with a value.
188type LabelPair struct {
189Name LabelName
190Value LabelValue
191}
192
193// LabelPairs is a sortable slice of LabelPair pointers. It implements
194// sort.Interface.
195type LabelPairs []*LabelPair
196
197func (l LabelPairs) Len() int {
198return len(l)
199}
200
201func (l LabelPairs) Less(i, j int) bool {
202switch {
203case l[i].Name > l[j].Name:
204return false
205case l[i].Name < l[j].Name:
206return true
207case l[i].Value > l[j].Value:
208return false
209case l[i].Value < l[j].Value:
210return true
211default:
212return false
213}
214}
215
216func (l LabelPairs) Swap(i, j int) {
217l[i], l[j] = l[j], l[i]
218}
219