-
Notifications
You must be signed in to change notification settings - Fork 21
/
collector_uncompressed.go
237 lines (211 loc) · 7.5 KB
/
collector_uncompressed.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
package ftdc
import (
"bytes"
"io"
"github.com/evergreen-ci/birch"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
// NewUncompressedCollectorJSON constructs a collector that resolves
// data into a stream of JSON documents. The output of these
// uncompressed collectors does not use the FTDC encoding for data,
// and can be read as newline separated JSON.
//
// This collector will not allow you to collect documents with
// different schema (determined by the number of top-level fields.)
//
// If you do not resolve the after receiving the maximum number of
// samples, then additional Add operations will fail.
//
// The metadata for this collector is rendered as the first document
// in the stream.
func NewUncompressedCollectorJSON(maxSamples int) Collector {
return &uncompressedCollector{
exportJSON: true,
batchSize: maxSamples,
}
}
// NewUncompressedCollectorBSON constructs a collector that resolves
// data into a stream of BSON documents. The output of these
// uncompressed collectors does not use the FTDC encoding for data,
// and can be read with the bsondump and other related utilites.
//
// This collector will not allow you to collect documents with
// different schema (determined by the number of top-level fields.)
//
// If you do not resolve the after receiving the maximum number of
// samples, then additional Add operations will fail.
//
// The metadata for this collector is rendered as the first document
// in the stream.
func NewUncompressedCollectorBSON(maxSamples int) Collector {
return &uncompressedCollector{
exportBSON: true,
batchSize: maxSamples,
}
}
// NewUncompressedCollectorJSON constructs a collector that resolves
// data into a stream of JSON documents. The output of these
// uncompressed collectors does not use the FTDC encoding for data,
// and can be read as newline separated JSON.
//
// This collector will not allow you to collect documents with
// different schema (determined by the number of top-level fields.)
//
// The metadata for this collector is rendered as the first document
// in the stream.
//
// All data is written to the writer when the underlying collector has
// captured its target number of collectors and is automatically
// flushed to the writer during the write operation or when you call
// Close. You can also use the FlushCollector helper.
func NewStreamingUncompressedCollectorJSON(maxSamples int, writer io.Writer) Collector {
return &streamingCollector{
maxSamples: maxSamples,
output: writer,
Collector: NewUncompressedCollectorJSON(maxSamples),
}
}
// NewUncompressedCollectorBSON constructs a collector that resolves
// data into a stream of BSON documents. The output of these
// uncompressed collectors does not use the FTDC encoding for data,
// and can be read with the bsondump and other related utilites.
//
// This collector will not allow you to collect documents with
// different schema (determined by the number of top-level fields.)
//
// The metadata for this collector is rendered as the first document
// in the stream.
//
// All data is written to the writer when the underlying collector has
// captured its target number of collectors and is automatically
// flushed to the writer during the write operation or when you call
// Close. You can also use the FlushCollector helper.
func NewStreamingUncompressedCollectorBSON(maxSamples int, writer io.Writer) Collector {
return &streamingCollector{
maxSamples: maxSamples,
output: writer,
Collector: NewUncompressedCollectorBSON(maxSamples),
}
}
// NewStreamingUncompressedCollectorJSON constructs a collector that
// resolves data into a stream of JSON documents. The output of these
// uncompressed collectors does not use the FTDC encoding for data,
// and can be read as newline separated JSON.
//
// The metadata for this collector is rendered as the first document
// in the stream. Additionally, the collector will automatically
// handle schema changes by flushing the previous batch.
//
// All data is written to the writer when the underlying collector has
// captured its target number of collectors and is automatically
// flushed to the writer during the write operation or when you call
// Close. You can also use the FlushCollector helper.
func NewStreamingDynamicUncompressedCollectorBSON(maxSamples int, writer io.Writer) Collector {
return &streamingDynamicCollector{
output: writer,
streamingCollector: NewStreamingUncompressedCollectorBSON(maxSamples, writer).(*streamingCollector),
}
}
// NewStreamingUncompressedCollectorBSON constructs a collector that
// resolves data into a stream of BSON documents. The output of these
// uncompressed collectors does not use the FTDC encoding for data,
// and can be read with the bsondump and other related utilites.
//
// The metadata for this collector is rendered as the first document
// in the stream. Additionally, the collector will automatically
// handle schema changes by flushing the previous batch.
//
// All data is written to the writer when the underlying collector has
// captured its target number of collectors and is automatically
// flushed to the writer during the write operation or when you call
// Close. You can also use the FlushCollector helper.
func NewStreamingDynamicUncompressedCollectorJSON(maxSamples int, writer io.Writer) Collector {
return &streamingDynamicCollector{
output: writer,
streamingCollector: NewStreamingUncompressedCollectorJSON(maxSamples, writer).(*streamingCollector),
}
}
type uncompressedCollector struct {
exportBSON bool
exportJSON bool
batchSize int
metricCount int
metadata *birch.Document
samples []*birch.Document
}
func (c *uncompressedCollector) Reset() {
c.samples = []*birch.Document{}
c.metricCount = 0
}
func (c *uncompressedCollector) Info() CollectorInfo {
return CollectorInfo{
SampleCount: len(c.samples),
MetricsCount: c.metricCount,
}
}
func (c *uncompressedCollector) SetMetadata(in interface{}) error {
doc, err := readDocument(in)
if err != nil {
return errors.WithStack(err)
}
c.metadata = doc
return nil
}
func (c *uncompressedCollector) Add(in interface{}) error {
doc, err := readDocument(in)
if err != nil {
return errors.WithStack(err)
}
if c.metricCount == 0 {
c.metricCount = doc.Len()
}
if doc.Len() != c.metricCount {
return errors.New("unexpected schema change detected")
}
if len(c.samples) >= c.batchSize {
return errors.New("collector is overfull")
}
c.samples = append(c.samples, doc)
return nil
}
func (c *uncompressedCollector) Resolve() ([]byte, error) {
if len(c.samples) == 0 {
return nil, errors.New("no data")
}
buf := bytes.NewBuffer([]byte{})
if c.metadata != nil {
if err := c.marshalWrite(buf, c.metadata); err != nil {
return nil, errors.WithStack(err)
}
}
for _, sample := range c.samples {
if err := c.marshalWrite(buf, sample); err != nil {
return nil, errors.WithStack(err)
}
}
return buf.Bytes(), nil
}
func (c *uncompressedCollector) marshalWrite(buf io.Writer, doc io.WriterTo) error {
switch {
case c.exportBSON == c.exportJSON:
return errors.New("collector export format is not configured")
case c.exportBSON:
_, err := doc.WriteTo(buf)
return errors.WithStack(err)
case c.exportJSON:
data, err := bson.MarshalExtJSON(doc, false, false)
if err != nil {
return errors.WithStack(err)
}
if _, err = buf.Write(data); err != nil {
return errors.WithStack(err)
}
if _, err = buf.Write([]byte("\n")); err != nil {
return errors.WithStack(err)
}
return nil
default:
return errors.New("invalid collector configuration")
}
}