/
custom.go
133 lines (117 loc) · 3.47 KB
/
custom.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/thanos-io/thanos/blob/main/pkg/store/storepb/custom.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Thanos Authors.
package storepb
import (
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/labels"
)
func NewSeriesResponse(series *Series) *SeriesResponse {
return &SeriesResponse{
Result: &SeriesResponse_Series{
Series: series,
},
}
}
func NewHintsSeriesResponse(hints *types.Any) *SeriesResponse {
return &SeriesResponse{
Result: &SeriesResponse_Hints{
Hints: hints,
},
}
}
func NewStatsResponse(indexBytesFetched int) *SeriesResponse {
return &SeriesResponse{
Result: &SeriesResponse_Stats{
Stats: &Stats{FetchedIndexBytes: uint64(indexBytesFetched)},
},
}
}
func NewStreamingSeriesResponse(series *StreamingSeriesBatch) *SeriesResponse {
return &SeriesResponse{
Result: &SeriesResponse_StreamingSeries{
StreamingSeries: series,
},
}
}
func NewStreamingChunksResponse(series *StreamingChunksBatch) *SeriesResponse {
return &SeriesResponse{
Result: &SeriesResponse_StreamingChunks{
StreamingChunks: series,
},
}
}
func NewStreamingChunksEstimate(estimatedChunks uint64) *SeriesResponse {
return &SeriesResponse{
Result: &SeriesResponse_StreamingChunksEstimate{
StreamingChunksEstimate: &StreamingChunksEstimate{
EstimatedChunkCount: estimatedChunks,
},
},
}
}
type emptySeriesSet struct{}
func (emptySeriesSet) Next() bool { return false }
func (emptySeriesSet) At() (labels.Labels, []AggrChunk) { return labels.EmptyLabels(), nil }
func (emptySeriesSet) Err() error { return nil }
// EmptySeriesSet returns a new series set that contains no series.
func EmptySeriesSet() SeriesSet {
return emptySeriesSet{}
}
// SeriesSet is a set of series and their corresponding chunks.
// The set is sorted by the label sets. Chunks may be overlapping or expected of order.
type SeriesSet interface {
Next() bool
At() (labels.Labels, []AggrChunk)
Err() error
}
// PromMatchersToMatchers returns proto matchers from Prometheus matchers.
// NOTE: It allocates memory.
func PromMatchersToMatchers(ms ...*labels.Matcher) ([]LabelMatcher, error) {
res := make([]LabelMatcher, 0, len(ms))
for _, m := range ms {
var t LabelMatcher_Type
switch m.Type {
case labels.MatchEqual:
t = LabelMatcher_EQ
case labels.MatchNotEqual:
t = LabelMatcher_NEQ
case labels.MatchRegexp:
t = LabelMatcher_RE
case labels.MatchNotRegexp:
t = LabelMatcher_NRE
default:
return nil, errors.Errorf("unrecognized matcher type %d", m.Type)
}
res = append(res, LabelMatcher{Type: t, Name: m.Name, Value: m.Value})
}
return res, nil
}
// MatchersToPromMatchers returns Prometheus matchers from proto matchers.
// NOTE: It allocates memory.
func MatchersToPromMatchers(ms ...LabelMatcher) ([]*labels.Matcher, error) {
res := make([]*labels.Matcher, 0, len(ms))
for _, m := range ms {
var t labels.MatchType
switch m.Type {
case LabelMatcher_EQ:
t = labels.MatchEqual
case LabelMatcher_NEQ:
t = labels.MatchNotEqual
case LabelMatcher_RE:
t = labels.MatchRegexp
case LabelMatcher_NRE:
t = labels.MatchNotRegexp
default:
return nil, errors.Errorf("unrecognized label matcher type %d", m.Type)
}
m, err := labels.NewMatcher(t, m.Name, m.Value)
if err != nil {
return nil, err
}
res = append(res, m)
}
return res, nil
}