-
Notifications
You must be signed in to change notification settings - Fork 791
/
errors.go
252 lines (211 loc) · 7.63 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package validation
import (
"fmt"
"strconv"
"strings"
"github.com/prometheus/common/model"
"github.com/cortexproject/cortex/pkg/cortexpb"
)
// ValidationError is an error returned by series validation.
//
// Ignore stutter warning.
// nolint:revive
type ValidationError error
// genericValidationError is a basic implementation of ValidationError which can be used when the
// error format only contains the cause and the series.
type genericValidationError struct {
message string
cause string
series []cortexpb.LabelAdapter
}
func (e *genericValidationError) Error() string {
return fmt.Sprintf(e.message, e.cause, formatLabelSet(e.series))
}
// labelNameTooLongError is a customized ValidationError, in that the cause and the series are
// formatted in different order in Error.
type labelNameTooLongError struct {
labelName string
series []cortexpb.LabelAdapter
limit int
}
func (e *labelNameTooLongError) Error() string {
return fmt.Sprintf("label name too long for metric (actual: %d, limit: %d) metric: %.200q label name: %.200q", len(e.labelName), e.limit, formatLabelSet(e.series), e.labelName)
}
func newLabelNameTooLongError(series []cortexpb.LabelAdapter, labelName string, limit int) ValidationError {
return &labelNameTooLongError{
labelName: labelName,
series: series,
limit: limit,
}
}
// labelValueTooLongError is a customized ValidationError, in that the cause and the series are
// formatted in different order in Error.
type labelValueTooLongError struct {
labelName string
labelValue string
series []cortexpb.LabelAdapter
limit int
}
func (e *labelValueTooLongError) Error() string {
return fmt.Sprintf("label value too long for metric (actual: %d, limit: %d) metric: %.200q label name: %.200q label value: %.200q",
len(e.labelValue), e.limit, formatLabelSet(e.series), e.labelName, e.labelValue)
}
func newLabelValueTooLongError(series []cortexpb.LabelAdapter, labelName, labelValue string, limit int) ValidationError {
return &labelValueTooLongError{
labelName: labelName,
labelValue: labelValue,
series: series,
limit: limit,
}
}
// labelsSizeBytesExceededError is a customized ValidationError, in that the cause and the series are
// formatted in different order in Error.
type labelsSizeBytesExceededError struct {
labelsSizeBytes int
series []cortexpb.LabelAdapter
limit int
}
func (e *labelsSizeBytesExceededError) Error() string {
return fmt.Sprintf("labels size bytes exceeded for metric (actual: %d, limit: %d) metric: %.200q", e.labelsSizeBytes, e.limit, formatLabelSet(e.series))
}
func labelSizeBytesExceededError(series []cortexpb.LabelAdapter, labelsSizeBytes int, limit int) ValidationError {
return &labelsSizeBytesExceededError{
labelsSizeBytes: labelsSizeBytes,
series: series,
limit: limit,
}
}
func newInvalidLabelError(series []cortexpb.LabelAdapter, labelName string) ValidationError {
return &genericValidationError{
message: "sample invalid label: %.200q metric %.200q",
cause: labelName,
series: series,
}
}
func newDuplicatedLabelError(series []cortexpb.LabelAdapter, labelName string) ValidationError {
return &genericValidationError{
message: "duplicate label name: %.200q metric %.200q",
cause: labelName,
series: series,
}
}
func newLabelsNotSortedError(series []cortexpb.LabelAdapter, labelName string) ValidationError {
return &genericValidationError{
message: "labels not sorted: %.200q metric %.200q",
cause: labelName,
series: series,
}
}
type tooManyLabelsError struct {
series []cortexpb.LabelAdapter
limit int
}
func newTooManyLabelsError(series []cortexpb.LabelAdapter, limit int) ValidationError {
return &tooManyLabelsError{
series: series,
limit: limit,
}
}
func (e *tooManyLabelsError) Error() string {
return fmt.Sprintf(
"series has too many labels (actual: %d, limit: %d) series: '%s'",
len(e.series), e.limit, cortexpb.FromLabelAdaptersToMetric(e.series).String())
}
type noMetricNameError struct{}
func newNoMetricNameError() ValidationError {
return &noMetricNameError{}
}
func (e *noMetricNameError) Error() string {
return "sample missing metric name"
}
type invalidMetricNameError struct {
metricName string
}
func newInvalidMetricNameError(metricName string) ValidationError {
return &invalidMetricNameError{
metricName: metricName,
}
}
func (e *invalidMetricNameError) Error() string {
return fmt.Sprintf("sample invalid metric name: %.200q", e.metricName)
}
// sampleValidationError is a ValidationError implementation suitable for sample validation errors.
type sampleValidationError struct {
message string
metricName string
timestamp int64
}
func (e *sampleValidationError) Error() string {
return fmt.Sprintf(e.message, e.timestamp, e.metricName)
}
func newSampleTimestampTooOldError(metricName string, timestamp int64) ValidationError {
return &sampleValidationError{
message: "timestamp too old: %d metric: %.200q",
metricName: metricName,
timestamp: timestamp,
}
}
func newSampleTimestampTooNewError(metricName string, timestamp int64) ValidationError {
return &sampleValidationError{
message: "timestamp too new: %d metric: %.200q",
metricName: metricName,
timestamp: timestamp,
}
}
// exemplarValidationError is a ValidationError implementation suitable for exemplar validation errors.
type exemplarValidationError struct {
message string
seriesLabels []cortexpb.LabelAdapter
exemplarLabels []cortexpb.LabelAdapter
timestamp int64
}
func (e *exemplarValidationError) Error() string {
return fmt.Sprintf(e.message, e.timestamp, cortexpb.FromLabelAdaptersToLabels(e.seriesLabels).String(), cortexpb.FromLabelAdaptersToLabels(e.exemplarLabels).String())
}
func newExemplarEmtpyLabelsError(seriesLabels []cortexpb.LabelAdapter, exemplarLabels []cortexpb.LabelAdapter, timestamp int64) ValidationError {
return &exemplarValidationError{
message: "exemplar missing labels, timestamp: %d series: %s labels: %s",
seriesLabels: seriesLabels,
exemplarLabels: exemplarLabels,
timestamp: timestamp,
}
}
func newExemplarMissingTimestampError(seriesLabels []cortexpb.LabelAdapter, exemplarLabels []cortexpb.LabelAdapter, timestamp int64) ValidationError {
return &exemplarValidationError{
message: "exemplar missing timestamp, timestamp: %d series: %s labels: %s",
seriesLabels: seriesLabels,
exemplarLabels: exemplarLabels,
timestamp: timestamp,
}
}
var labelLenMsg = "exemplar combined labelset exceeds " + strconv.Itoa(ExemplarMaxLabelSetLength) + " characters, timestamp: %d series: %s labels: %s"
func newExemplarLabelLengthError(seriesLabels []cortexpb.LabelAdapter, exemplarLabels []cortexpb.LabelAdapter, timestamp int64) ValidationError {
return &exemplarValidationError{
message: labelLenMsg,
seriesLabels: seriesLabels,
exemplarLabels: exemplarLabels,
timestamp: timestamp,
}
}
// formatLabelSet formats label adapters as a metric name with labels, while preserving
// label order, and keeping duplicates. If there are multiple "__name__" labels, only
// first one is used as metric name, other ones will be included as regular labels.
func formatLabelSet(ls []cortexpb.LabelAdapter) string {
metricName, hasMetricName := "", false
labelStrings := make([]string, 0, len(ls))
for _, l := range ls {
if l.Name == model.MetricNameLabel && !hasMetricName && l.Value != "" {
metricName = l.Value
hasMetricName = true
} else {
labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", l.Name, l.Value))
}
}
if len(labelStrings) == 0 {
if hasMetricName {
return metricName
}
return "{}"
}
return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
}