...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package model
15
16 import (
17 "encoding/json"
18 "fmt"
19 "regexp"
20 "strings"
21 "unicode/utf8"
22 )
23
24 const (
25
26 AlertNameLabel = "alertname"
27
28
29
30 ExportedLabelPrefix = "exported_"
31
32
33
34 MetricNameLabel = "__name__"
35
36
37
38 SchemeLabel = "__scheme__"
39
40
41
42 AddressLabel = "__address__"
43
44
45
46 MetricsPathLabel = "__metrics_path__"
47
48
49
50 ScrapeIntervalLabel = "__scrape_interval__"
51
52
53
54 ScrapeTimeoutLabel = "__scrape_timeout__"
55
56
57
58 ReservedLabelPrefix = "__"
59
60
61
62
63 MetaLabelPrefix = "__meta_"
64
65
66
67
68
69 TmpLabelPrefix = "__tmp_"
70
71
72
73 ParamLabelPrefix = "__param_"
74
75
76
77 JobLabel = "job"
78
79
80 InstanceLabel = "instance"
81
82
83
84 BucketLabel = "le"
85
86
87
88 QuantileLabel = "quantile"
89 )
90
91
92
93
94 var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
95
96
97
98 type LabelName string
99
100
101
102
103
104 func (ln LabelName) IsValid() bool {
105 if len(ln) == 0 {
106 return false
107 }
108 switch NameValidationScheme {
109 case LegacyValidation:
110 for i, b := range ln {
111 if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
112 return false
113 }
114 }
115 case UTF8Validation:
116 return utf8.ValidString(string(ln))
117 default:
118 panic(fmt.Sprintf("Invalid name validation scheme requested: %d", NameValidationScheme))
119 }
120 return true
121 }
122
123
124 func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
125 var s string
126 if err := unmarshal(&s); err != nil {
127 return err
128 }
129 if !LabelName(s).IsValid() {
130 return fmt.Errorf("%q is not a valid label name", s)
131 }
132 *ln = LabelName(s)
133 return nil
134 }
135
136
137 func (ln *LabelName) UnmarshalJSON(b []byte) error {
138 var s string
139 if err := json.Unmarshal(b, &s); err != nil {
140 return err
141 }
142 if !LabelName(s).IsValid() {
143 return fmt.Errorf("%q is not a valid label name", s)
144 }
145 *ln = LabelName(s)
146 return nil
147 }
148
149
150 type LabelNames []LabelName
151
152 func (l LabelNames) Len() int {
153 return len(l)
154 }
155
156 func (l LabelNames) Less(i, j int) bool {
157 return l[i] < l[j]
158 }
159
160 func (l LabelNames) Swap(i, j int) {
161 l[i], l[j] = l[j], l[i]
162 }
163
164 func (l LabelNames) String() string {
165 labelStrings := make([]string, 0, len(l))
166 for _, label := range l {
167 labelStrings = append(labelStrings, string(label))
168 }
169 return strings.Join(labelStrings, ", ")
170 }
171
172
173 type LabelValue string
174
175
176 func (lv LabelValue) IsValid() bool {
177 return utf8.ValidString(string(lv))
178 }
179
180
181 type LabelValues []LabelValue
182
183 func (l LabelValues) Len() int {
184 return len(l)
185 }
186
187 func (l LabelValues) Less(i, j int) bool {
188 return string(l[i]) < string(l[j])
189 }
190
191 func (l LabelValues) Swap(i, j int) {
192 l[i], l[j] = l[j], l[i]
193 }
194
195
196 type LabelPair struct {
197 Name LabelName
198 Value LabelValue
199 }
200
201
202
203 type LabelPairs []*LabelPair
204
205 func (l LabelPairs) Len() int {
206 return len(l)
207 }
208
209 func (l LabelPairs) Less(i, j int) bool {
210 switch {
211 case l[i].Name > l[j].Name:
212 return false
213 case l[i].Name < l[j].Name:
214 return true
215 case l[i].Value > l[j].Value:
216 return false
217 case l[i].Value < l[j].Value:
218 return true
219 default:
220 return false
221 }
222 }
223
224 func (l LabelPairs) Swap(i, j int) {
225 l[i], l[j] = l[j], l[i]
226 }
227
View as plain text