-
Notifications
You must be signed in to change notification settings - Fork 476
/
prometheus_chunk.go
316 lines (265 loc) · 9.08 KB
/
prometheus_chunk.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/chunk/encoding/prometheus_chunk.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package chunk
import (
"fmt"
"io"
"unsafe"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/util/zeropool"
)
// Wrapper around a generic Prometheus chunk.
type prometheusChunk struct {
chunk chunkenc.Chunk
}
func (p *prometheusChunk) NewIterator(iterator Iterator) Iterator {
if p.chunk == nil {
return errorIterator("Prometheus chunk is not set")
}
if pit, ok := iterator.(*prometheusChunkIterator); ok {
pit.c = p.chunk
pit.it = p.chunk.Iterator(pit.it)
return pit
}
return &prometheusChunkIterator{c: p.chunk, it: p.chunk.Iterator(nil)}
}
func (p *prometheusChunk) Marshal(i io.Writer) error {
if p.chunk == nil {
return errors.New("chunk data not set")
}
_, err := i.Write(p.chunk.Bytes())
return err
}
func (p *prometheusChunk) Len() int {
if p.chunk == nil {
return 0
}
return p.chunk.NumSamples()
}
// Wrapper around a Prometheus XOR chunk.
type prometheusXorChunk struct {
prometheusChunk
}
func newPrometheusXorChunk() *prometheusXorChunk {
return &prometheusXorChunk{}
}
// Add adds another sample to the chunk. While Add works, it is only implemented
// to make tests work, and should not be used in production. In particular, it appends
// all samples to single chunk, and uses new Appender for each Add.
func (p *prometheusXorChunk) Add(m model.SamplePair) (EncodedChunk, error) {
if p.chunk == nil {
p.chunk = chunkenc.NewXORChunk()
}
app, err := p.chunk.Appender()
if err != nil {
return nil, err
}
app.Append(int64(m.Timestamp), float64(m.Value))
return nil, nil
}
func (p *prometheusXorChunk) AddHistogram(_ int64, _ *histogram.Histogram) (EncodedChunk, error) {
return nil, fmt.Errorf("cannot add histogram to sample chunk")
}
func (p *prometheusXorChunk) AddFloatHistogram(_ int64, _ *histogram.FloatHistogram) (EncodedChunk, error) {
return nil, fmt.Errorf("cannot add float histogram to sample chunk")
}
func (p *prometheusXorChunk) UnmarshalFromBuf(bytes []byte) error {
c, err := chunkenc.FromData(chunkenc.EncXOR, bytes)
if err != nil {
return errors.Wrap(err, "failed to create Prometheus chunk from bytes")
}
p.chunk = c
return nil
}
func (p *prometheusXorChunk) Encoding() Encoding {
return PrometheusXorChunk
}
// Wrapper around a Prometheus histogram chunk.
type prometheusHistogramChunk struct {
prometheusChunk
}
func newPrometheusHistogramChunk() *prometheusHistogramChunk {
return &prometheusHistogramChunk{}
}
func (p *prometheusHistogramChunk) Add(_ model.SamplePair) (EncodedChunk, error) {
return nil, fmt.Errorf("cannot add float sample to histogram chunk")
}
func (p *prometheusHistogramChunk) AddFloatHistogram(_ int64, _ *histogram.FloatHistogram) (EncodedChunk, error) {
return nil, fmt.Errorf("cannot add float histogram to histogram chunk")
}
// AddHistogram adds another histogram to the chunk. While AddHistogram works, it is only implemented to make tests
// work, and should not be used in production. In particular, it appends all histograms to single chunk, and uses new
// Appender for each invocation.
func (p *prometheusHistogramChunk) AddHistogram(timestamp int64, h *histogram.Histogram) (EncodedChunk, error) {
if p.chunk == nil {
p.chunk = chunkenc.NewHistogramChunk()
}
app, err := p.chunk.Appender()
if err != nil {
return nil, err
}
_, _, _, err = app.AppendHistogram(nil, timestamp, h, true)
return nil, err
}
func (p *prometheusHistogramChunk) UnmarshalFromBuf(bytes []byte) error {
c, err := chunkenc.FromData(chunkenc.EncHistogram, bytes)
if err != nil {
return errors.Wrap(err, "failed to create Prometheus chunk from bytes")
}
p.chunk = c
return nil
}
func (p *prometheusHistogramChunk) Encoding() Encoding {
return PrometheusHistogramChunk
}
// Wrapper around a Prometheus histogram chunk.
type prometheusFloatHistogramChunk struct {
prometheusChunk
}
func newPrometheusFloatHistogramChunk() *prometheusFloatHistogramChunk {
return &prometheusFloatHistogramChunk{}
}
func (p *prometheusFloatHistogramChunk) Add(_ model.SamplePair) (EncodedChunk, error) {
return nil, fmt.Errorf("cannot add float sample to histogram chunk")
}
func (p *prometheusFloatHistogramChunk) AddHistogram(_ int64, _ *histogram.Histogram) (EncodedChunk, error) {
return nil, fmt.Errorf("cannot add histogram sample to float histogram chunk")
}
// AddFloatHistogram adds another float histogram to the chunk. While AddFloatHistogram works, it is only implemented to make tests
// work, and should not be used in production. In particular, it appends all histograms to single chunk, and uses new
// Appender for each invocation.
func (p *prometheusFloatHistogramChunk) AddFloatHistogram(timestamp int64, h *histogram.FloatHistogram) (EncodedChunk, error) {
if p.chunk == nil {
p.chunk = chunkenc.NewFloatHistogramChunk()
}
app, err := p.chunk.Appender()
if err != nil {
return nil, err
}
_, _, _, err = app.AppendFloatHistogram(nil, timestamp, h, true)
return nil, err
}
func (p *prometheusFloatHistogramChunk) UnmarshalFromBuf(bytes []byte) error {
c, err := chunkenc.FromData(chunkenc.EncFloatHistogram, bytes)
if err != nil {
return errors.Wrap(err, "failed to create Prometheus chunk from bytes")
}
p.chunk = c
return nil
}
func (p *prometheusFloatHistogramChunk) Encoding() Encoding {
return PrometheusFloatHistogramChunk
}
type prometheusChunkIterator struct {
c chunkenc.Chunk // we need chunk, because FindAtOrAfter needs to start with fresh iterator.
it chunkenc.Iterator
}
func (p *prometheusChunkIterator) Scan() chunkenc.ValueType {
return p.it.Next()
}
func (p *prometheusChunkIterator) FindAtOrAfter(time model.Time) chunkenc.ValueType {
// FindAtOrAfter must return OLDEST value at given time. That means we need to start with a fresh iterator,
// otherwise we cannot guarantee OLDEST.
p.it = p.c.Iterator(p.it)
return p.it.Seek(int64(time))
}
func (p *prometheusChunkIterator) Value() model.SamplePair {
ts, val := p.it.At()
return model.SamplePair{
Timestamp: model.Time(ts),
Value: model.SampleValue(val),
}
}
func (p *prometheusChunkIterator) AtHistogram(h *histogram.Histogram) (int64, *histogram.Histogram) {
return p.it.AtHistogram(h)
}
func (p *prometheusChunkIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
return p.it.AtFloatHistogram(fh)
}
func (p *prometheusChunkIterator) Timestamp() int64 {
return p.it.AtT()
}
func (p *prometheusChunkIterator) Batch(size int, valueType chunkenc.ValueType, hPool *zeropool.Pool[*histogram.Histogram], fhPool *zeropool.Pool[*histogram.FloatHistogram]) Batch {
var batch Batch
batch.ValueType = valueType
var populate func(j int)
switch valueType {
case chunkenc.ValNone:
// Here in case we will introduce a linter that checks that all possible types are covered
return batch
case chunkenc.ValFloat:
populate = func(j int) {
batch.Timestamps[j], batch.Values[j] = p.it.At()
}
case chunkenc.ValHistogram:
populate = func(j int) {
var (
h *histogram.Histogram
t int64
)
if hPool == nil {
h = &histogram.Histogram{}
} else {
h = hPool.Get()
}
t, h = p.it.AtHistogram(h)
batch.Timestamps[j], batch.PointerValues[j] = t, unsafe.Pointer(h)
}
case chunkenc.ValFloatHistogram:
populate = func(j int) {
var (
fh *histogram.FloatHistogram
t int64
)
if fhPool == nil {
fh = &histogram.FloatHistogram{}
} else {
fh = fhPool.Get()
}
t, fh = p.it.AtFloatHistogram(fh)
batch.Timestamps[j], batch.PointerValues[j] = t, unsafe.Pointer(fh)
}
default:
panic(fmt.Sprintf("invalid chunk encoding %v", valueType))
}
j := 0
for j < size {
populate(j)
j++
if j < size {
vt := p.it.Next()
if vt == chunkenc.ValNone {
break
}
if vt != valueType {
panic(fmt.Sprintf("chunk encoding expected to be consistent in chunk start %v now %v", valueType, vt))
}
}
}
batch.Index = 0
batch.Length = j
return batch
}
func (p *prometheusChunkIterator) Err() error {
return p.it.Err()
}
type errorIterator string
func (e errorIterator) Scan() chunkenc.ValueType { return chunkenc.ValNone }
func (e errorIterator) FindAtOrAfter(_ model.Time) chunkenc.ValueType { return chunkenc.ValNone }
func (e errorIterator) Value() model.SamplePair { panic("no values") }
func (e errorIterator) AtHistogram(*histogram.Histogram) (int64, *histogram.Histogram) {
panic("no integer histograms")
}
func (e errorIterator) AtFloatHistogram(*histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
panic("no float histograms")
}
func (e errorIterator) Timestamp() int64 { panic("no samples") }
func (e errorIterator) Batch(_ int, _ chunkenc.ValueType, _ *zeropool.Pool[*histogram.Histogram], _ *zeropool.Pool[*histogram.FloatHistogram]) Batch {
panic("no values")
}
func (e errorIterator) Err() error { return errors.New(string(e)) }