-
Notifications
You must be signed in to change notification settings - Fork 796
/
compat.go
250 lines (223 loc) · 7.22 KB
/
compat.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
package client
import (
"fmt"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/cortexproject/cortex/pkg/cortexpb"
)
// ToQueryRequest builds a QueryRequest proto.
func ToQueryRequest(from, to model.Time, matchers []*labels.Matcher) (*QueryRequest, error) {
ms, err := toLabelMatchers(matchers)
if err != nil {
return nil, err
}
return &QueryRequest{
StartTimestampMs: int64(from),
EndTimestampMs: int64(to),
Matchers: ms,
}, nil
}
// FromQueryRequest unpacks a QueryRequest proto.
func FromQueryRequest(req *QueryRequest) (model.Time, model.Time, []*labels.Matcher, error) {
matchers, err := FromLabelMatchers(req.Matchers)
if err != nil {
return 0, 0, nil, err
}
from := model.Time(req.StartTimestampMs)
to := model.Time(req.EndTimestampMs)
return from, to, matchers, nil
}
// ToExemplarQueryRequest builds an ExemplarQueryRequest proto.
func ToExemplarQueryRequest(from, to model.Time, matchers ...[]*labels.Matcher) (*ExemplarQueryRequest, error) {
var reqMatchers []*LabelMatchers
for _, m := range matchers {
ms, err := toLabelMatchers(m)
if err != nil {
return nil, err
}
reqMatchers = append(reqMatchers, &LabelMatchers{ms})
}
return &ExemplarQueryRequest{
StartTimestampMs: int64(from),
EndTimestampMs: int64(to),
Matchers: reqMatchers,
}, nil
}
// FromExemplarQueryRequest unpacks a ExemplarQueryRequest proto.
func FromExemplarQueryRequest(req *ExemplarQueryRequest) (int64, int64, [][]*labels.Matcher, error) {
var result [][]*labels.Matcher
for _, m := range req.Matchers {
matchers, err := FromLabelMatchers(m.Matchers)
if err != nil {
return 0, 0, nil, err
}
result = append(result, matchers)
}
return req.StartTimestampMs, req.EndTimestampMs, result, nil
}
// ToQueryResponse builds a QueryResponse proto.
func ToQueryResponse(matrix model.Matrix) *QueryResponse {
resp := &QueryResponse{}
for _, ss := range matrix {
ts := cortexpb.TimeSeries{
Labels: cortexpb.FromMetricsToLabelAdapters(ss.Metric),
Samples: make([]cortexpb.Sample, 0, len(ss.Values)),
}
for _, s := range ss.Values {
ts.Samples = append(ts.Samples, cortexpb.Sample{
Value: float64(s.Value),
TimestampMs: int64(s.Timestamp),
})
}
resp.Timeseries = append(resp.Timeseries, ts)
}
return resp
}
// FromQueryResponse unpacks a QueryResponse proto.
func FromQueryResponse(resp *QueryResponse) model.Matrix {
m := make(model.Matrix, 0, len(resp.Timeseries))
for _, ts := range resp.Timeseries {
var ss model.SampleStream
ss.Metric = cortexpb.FromLabelAdaptersToMetric(ts.Labels)
ss.Values = make([]model.SamplePair, 0, len(ts.Samples))
for _, s := range ts.Samples {
ss.Values = append(ss.Values, model.SamplePair{
Value: model.SampleValue(s.Value),
Timestamp: model.Time(s.TimestampMs),
})
}
m = append(m, &ss)
}
return m
}
// ToMetricsForLabelMatchersRequest builds a MetricsForLabelMatchersRequest proto
func ToMetricsForLabelMatchersRequest(from, to model.Time, matchers []*labels.Matcher) (*MetricsForLabelMatchersRequest, error) {
ms, err := toLabelMatchers(matchers)
if err != nil {
return nil, err
}
return &MetricsForLabelMatchersRequest{
StartTimestampMs: int64(from),
EndTimestampMs: int64(to),
MatchersSet: []*LabelMatchers{{Matchers: ms}},
}, nil
}
// FromMetricsForLabelMatchersRequest unpacks a MetricsForLabelMatchersRequest proto
func FromMetricsForLabelMatchersRequest(req *MetricsForLabelMatchersRequest) (model.Time, model.Time, [][]*labels.Matcher, error) {
matchersSet := make([][]*labels.Matcher, 0, len(req.MatchersSet))
for _, matchers := range req.MatchersSet {
matchers, err := FromLabelMatchers(matchers.Matchers)
if err != nil {
return 0, 0, nil, err
}
matchersSet = append(matchersSet, matchers)
}
from := model.Time(req.StartTimestampMs)
to := model.Time(req.EndTimestampMs)
return from, to, matchersSet, nil
}
// FromMetricsForLabelMatchersResponse unpacks a MetricsForLabelMatchersResponse proto
func FromMetricsForLabelMatchersResponse(resp *MetricsForLabelMatchersResponse) []model.Metric {
metrics := []model.Metric{}
for _, m := range resp.Metric {
metrics = append(metrics, cortexpb.FromLabelAdaptersToMetric(m.Labels))
}
return metrics
}
// ToLabelValuesRequest builds a LabelValuesRequest proto
func ToLabelValuesRequest(labelName model.LabelName, from, to model.Time, matchers []*labels.Matcher) (*LabelValuesRequest, error) {
ms, err := toLabelMatchers(matchers)
if err != nil {
return nil, err
}
return &LabelValuesRequest{
LabelName: string(labelName),
StartTimestampMs: int64(from),
EndTimestampMs: int64(to),
Matchers: &LabelMatchers{Matchers: ms},
}, nil
}
// FromLabelValuesRequest unpacks a LabelValuesRequest proto
func FromLabelValuesRequest(req *LabelValuesRequest) (string, int64, int64, []*labels.Matcher, error) {
var err error
var matchers []*labels.Matcher
if req.Matchers != nil {
matchers, err = FromLabelMatchers(req.Matchers.Matchers)
if err != nil {
return "", 0, 0, nil, err
}
}
return req.LabelName, req.StartTimestampMs, req.EndTimestampMs, matchers, nil
}
func toLabelMatchers(matchers []*labels.Matcher) ([]*LabelMatcher, error) {
result := make([]*LabelMatcher, 0, len(matchers))
for _, matcher := range matchers {
var mType MatchType
switch matcher.Type {
case labels.MatchEqual:
mType = EQUAL
case labels.MatchNotEqual:
mType = NOT_EQUAL
case labels.MatchRegexp:
mType = REGEX_MATCH
case labels.MatchNotRegexp:
mType = REGEX_NO_MATCH
default:
return nil, fmt.Errorf("invalid matcher type")
}
result = append(result, &LabelMatcher{
Type: mType,
Name: matcher.Name,
Value: matcher.Value,
})
}
return result, nil
}
func FromLabelMatchers(matchers []*LabelMatcher) ([]*labels.Matcher, error) {
result := make([]*labels.Matcher, 0, len(matchers))
for _, matcher := range matchers {
var mtype labels.MatchType
switch matcher.Type {
case EQUAL:
mtype = labels.MatchEqual
case NOT_EQUAL:
mtype = labels.MatchNotEqual
case REGEX_MATCH:
mtype = labels.MatchRegexp
case REGEX_NO_MATCH:
mtype = labels.MatchNotRegexp
default:
return nil, fmt.Errorf("invalid matcher type")
}
matcher, err := labels.NewMatcher(mtype, matcher.Name, matcher.Value)
if err != nil {
return nil, err
}
result = append(result, matcher)
}
return result, nil
}
// FastFingerprint runs the same algorithm as Prometheus labelSetToFastFingerprint()
func FastFingerprint(ls []cortexpb.LabelAdapter) model.Fingerprint {
if len(ls) == 0 {
return model.Metric(nil).FastFingerprint()
}
var result uint64
for _, l := range ls {
sum := hashNew()
sum = hashAdd(sum, l.Name)
sum = hashAddByte(sum, model.SeparatorByte)
sum = hashAdd(sum, l.Value)
result ^= sum
}
return model.Fingerprint(result)
}
// LabelsToKeyString is used to form a string to be used as
// the hashKey. Don't print, use l.String() for printing.
func LabelsToKeyString(l labels.Labels) string {
// We are allocating 1024, even though most series are less than 600b long.
// But this is not an issue as this function is being inlined when called in a loop
// and buffer allocated is a static buffer and not a dynamic buffer on the heap.
b := make([]byte, 0, 1024)
return string(l.Bytes(b))
}