-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
marshal.go
286 lines (248 loc) · 6.79 KB
/
marshal.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package writecapnp
import (
"capnproto.org/go/capnp/v3"
"github.com/pkg/errors"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb/prompb"
)
func Marshal(tenant string, tsreq []prompb.TimeSeries) ([]byte, error) {
wr, err := Build(tenant, tsreq)
if err != nil {
return nil, err
}
return wr.Message().Marshal()
}
func MarshalPacked(tenant string, tsreq []prompb.TimeSeries) ([]byte, error) {
wr, err := Build(tenant, tsreq)
if err != nil {
return nil, err
}
return wr.Message().MarshalPacked()
}
func Build(tenant string, tsreq []prompb.TimeSeries) (WriteRequest, error) {
arena := capnp.SingleSegment(nil)
_, seg, err := capnp.NewMessage(arena)
if err != nil {
return WriteRequest{}, err
}
wr, err := NewRootWriteRequest(seg)
if err != nil {
return WriteRequest{}, err
}
if err := BuildInto(wr, tenant, tsreq); err != nil {
return WriteRequest{}, err
}
return wr, nil
}
func BuildInto(wr WriteRequest, tenant string, tsreq []prompb.TimeSeries) error {
if err := wr.SetTenant(tenant); err != nil {
return errors.Wrap(err, "set tenant")
}
series, err := wr.NewTimeSeries(int32(len(tsreq)))
if err != nil {
return err
}
builder := newSymbolsBuilder()
for i, ts := range tsreq {
tsc := series.At(i)
lblsc, err := tsc.NewLabels(int32(len(ts.Labels)))
if err != nil {
return errors.Wrap(err, "new labels")
}
if err := marshalLabels(lblsc, ts.Labels, builder); err != nil {
return errors.Wrap(err, "marshal labels")
}
if err := marshalSamples(tsc, ts.Samples); err != nil {
return errors.Wrap(err, "marshal samples")
}
if err := marshalHistograms(tsc, ts.Histograms); err != nil {
return errors.Wrap(err, "marshal histograms")
}
if err := marshalExemplars(tsc, ts.Exemplars, builder); err != nil {
return errors.Wrap(err, "marshal exemplars")
}
}
symbols, err := wr.NewSymbols()
if err != nil {
return errors.Wrap(err, "new symbols")
}
if err := marshalSymbols(builder, symbols); err != nil {
return errors.Wrap(err, "marshal symbols")
}
return nil
}
func marshalSymbols(builder *symbolsBuilder, symbols Symbols) error {
offsets, err := symbols.NewOffsets(builder.len())
if err != nil {
return err
}
data := make([]byte, builder.symbolsSize)
for k, entry := range builder.table {
end := entry.start + uint32(len(k))
copy(data[entry.start:end], k)
offsets.Set(int(entry.index), end)
}
return symbols.SetData(data)
}
func marshalLabels(lbls Label_List, pbLbls []labelpb.ZLabel, symbols *symbolsBuilder) error {
for i, pbLbl := range pbLbls {
lbl := lbls.At(i)
lbl.SetName(symbols.addEntry(pbLbl.Name))
lbl.SetValue(symbols.addEntry(pbLbl.Value))
}
return nil
}
func marshalSamples(ts TimeSeries, pbSamples []prompb.Sample) error {
samples, err := ts.NewSamples(int32(len(pbSamples)))
if err != nil {
return err
}
for j, sample := range pbSamples {
sc := samples.At(j)
sc.SetTimestamp(sample.Timestamp)
sc.SetValue(sample.Value)
}
return nil
}
func marshalHistograms(ts TimeSeries, pbHistograms []prompb.Histogram) error {
if len(pbHistograms) == 0 {
return nil
}
histograms, err := ts.NewHistograms(int32(len(pbHistograms)))
if err != nil {
return err
}
for i, h := range pbHistograms {
if err := marshalHistogram(histograms.At(i), h); err != nil {
return err
}
}
return nil
}
func marshalHistogram(histogram Histogram, h prompb.Histogram) error {
histogram.SetResetHint(Histogram_ResetHint(h.ResetHint))
switch h.Count.(type) {
case *prompb.Histogram_CountInt:
histogram.Count().SetCountInt(h.GetCountInt())
case *prompb.Histogram_CountFloat:
histogram.Count().SetCountFloat(h.GetCountFloat())
}
histogram.SetSum(h.Sum)
histogram.SetSchema(h.Schema)
histogram.SetZeroThreshold(h.ZeroThreshold)
switch h.ZeroCount.(type) {
case *prompb.Histogram_ZeroCountInt:
histogram.ZeroCount().SetZeroCountInt(h.GetZeroCountInt())
case *prompb.Histogram_ZeroCountFloat:
histogram.ZeroCount().SetZeroCountFloat(h.GetZeroCountFloat())
}
// Negative spans, deltas and counts.
negativeSpans, err := histogram.NewNegativeSpans(int32(len(h.NegativeSpans)))
if err != nil {
return err
}
if err := marshalSpans(negativeSpans, h.NegativeSpans); err != nil {
return err
}
negativeDeltas, err := histogram.NewNegativeDeltas(int32(len(h.NegativeDeltas)))
if err != nil {
return err
}
marshalInt64List(negativeDeltas, h.NegativeDeltas)
negativeCounts, err := histogram.NewNegativeCounts(int32(len(h.NegativeCounts)))
if err != nil {
return err
}
marshalFloat64List(negativeCounts, h.NegativeCounts)
// Positive spans, deltas and counts.
positiveSpans, err := histogram.NewPositiveSpans(int32(len(h.PositiveSpans)))
if err != nil {
return err
}
if err := marshalSpans(positiveSpans, h.PositiveSpans); err != nil {
return err
}
positiveDeltas, err := histogram.NewPositiveDeltas(int32(len(h.PositiveDeltas)))
if err != nil {
return err
}
marshalInt64List(positiveDeltas, h.PositiveDeltas)
positiveCounts, err := histogram.NewPositiveCounts(int32(len(h.PositiveCounts)))
if err != nil {
return err
}
marshalFloat64List(positiveCounts, h.PositiveCounts)
histogram.SetTimestamp(h.Timestamp)
return nil
}
func marshalSpans(spans BucketSpan_List, pbSpans []prompb.BucketSpan) error {
for j, s := range pbSpans {
span := spans.At(j)
span.SetOffset(s.Offset)
span.SetLength(s.Length)
}
return nil
}
func marshalExemplars(ts TimeSeries, pbExemplars []prompb.Exemplar, symbols *symbolsBuilder) error {
if len(pbExemplars) == 0 {
return nil
}
exemplars, err := ts.NewExemplars(int32(len(pbExemplars)))
if err != nil {
return err
}
for i := 0; i < len(pbExemplars); i++ {
ex := exemplars.At(i)
lbls, err := ex.NewLabels(int32(len(pbExemplars[i].Labels)))
if err != nil {
return err
}
if err := marshalLabels(lbls, pbExemplars[i].Labels, symbols); err != nil {
return err
}
ex.SetValue(pbExemplars[i].Value)
ex.SetTimestamp(pbExemplars[i].Timestamp)
}
return nil
}
func marshalInt64List(list capnp.Int64List, ints []int64) {
for j, d := range ints {
list.Set(j, d)
}
}
func marshalFloat64List(list capnp.Float64List, ints []float64) {
for j, d := range ints {
list.Set(j, d)
}
}
type symbolsBuilder struct {
table map[string]tableEntry
symbolsSize uint32
}
func newSymbolsBuilder() *symbolsBuilder {
return &symbolsBuilder{
table: make(map[string]tableEntry),
}
}
func (s *symbolsBuilder) addEntry(item string) uint32 {
entry, ok := s.table[item]
if ok {
return entry.index
}
entry = tableEntry{
index: uint32(len(s.table)),
start: s.symbolsSize,
}
s.symbolsSize += uint32(len(item))
s.table[item] = entry
return entry.index
}
func (s *symbolsBuilder) len() int32 {
return int32(len(s.table))
}
type tableEntry struct {
index uint32
start uint32
}