forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_batch.go
More file actions
363 lines (334 loc) · 13.5 KB
/
metrics_batch.go
File metadata and controls
363 lines (334 loc) · 13.5 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package exporterhelper // import "go.opentelemetry.io/collector/exporter/exporterhelper"
import (
"context"
"errors"
"fmt"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/sizer"
"go.opentelemetry.io/collector/pdata/pmetric"
)
// MergeSplit splits and/or merges the provided metrics request and the current request into one or more requests
// conforming with the MaxSizeConfig.
func (req *metricsRequest) MergeSplit(_ context.Context, maxSize int, szt RequestSizerType, r2 Request) ([]Request, error) {
var sz sizer.MetricsSizer
switch szt {
case RequestSizerTypeItems:
sz = &sizer.MetricsCountSizer{}
case RequestSizerTypeBytes:
sz = &sizer.MetricsBytesSizer{}
default:
return nil, errors.New("unknown sizer type")
}
if r2 != nil {
req2, ok := r2.(*metricsRequest)
if !ok {
return nil, errors.New("invalid input type")
}
req2.mergeTo(req, sz)
}
// If no limit we can simply merge the new request into the current and return.
if maxSize == 0 {
return []Request{req}, nil
}
return req.split(maxSize, sz)
}
func (req *metricsRequest) mergeTo(dst *metricsRequest, sz sizer.MetricsSizer) {
if sz != nil {
dst.setCachedSize(dst.size(sz) + req.size(sz))
req.setCachedSize(0)
}
req.md.ResourceMetrics().MoveAndAppendTo(dst.md.ResourceMetrics())
}
func (req *metricsRequest) split(maxSize int, sz sizer.MetricsSizer) ([]Request, error) {
var res []Request
var md pmetric.Metrics
rmSize := -1
previousSize := req.size(sz)
for req.size(sz) > maxSize && rmSize != 0 {
md, rmSize = extractMetrics(req.md, maxSize, sz)
if md.DataPointCount() > 0 {
req.setCachedSize(req.size(sz) - rmSize)
res = append(res, newMetricsRequest(md))
}
}
if req.size(sz) == previousSize && req.size(sz) > maxSize {
err := fmt.Errorf(
"partial success: failed to split metrics request: size is greater than max size. size: %d, max_size: %d. Failed: %d",
req.size(sz),
maxSize,
req.md.MetricCount(),
)
return res, err
}
res = append(res, req)
return res, nil
}
// extractMetrics extracts metrics from srcMetrics until capacity is reached.
func extractMetrics(srcMetrics pmetric.Metrics, capacity int, sz sizer.MetricsSizer) (pmetric.Metrics, int) {
destMetrics := pmetric.NewMetrics()
capacityLeft := capacity - sz.MetricsSize(destMetrics)
removedSize := 0
srcMetrics.ResourceMetrics().RemoveIf(func(srcRM pmetric.ResourceMetrics) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rawRlSize := sz.ResourceMetricsSize(srcRM)
rlSize := sz.DeltaSize(rawRlSize)
if rlSize > capacityLeft {
extSrcRM, extRmSize := extractResourceMetrics(srcRM, capacityLeft, sz)
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
removedSize += extRmSize
// There represents the delta between the delta sizes.
removedSize += rlSize - rawRlSize - (sz.DeltaSize(rawRlSize-extRmSize) - (rawRlSize - extRmSize))
// It is possible that for the bytes scenario, the extracted field contains no scope metrics.
// Do not add it to the destination if that is the case.
if extSrcRM.ScopeMetrics().Len() > 0 {
extSrcRM.MoveTo(destMetrics.ResourceMetrics().AppendEmpty())
}
return extSrcRM.ScopeMetrics().Len() != 0
}
capacityLeft -= rlSize
removedSize += rlSize
srcRM.MoveTo(destMetrics.ResourceMetrics().AppendEmpty())
return true
})
return destMetrics, removedSize
}
// extractResourceMetrics extracts resource metrics and returns a new resource metrics with the specified number of data points.
func extractResourceMetrics(srcRM pmetric.ResourceMetrics, capacity int, sz sizer.MetricsSizer) (pmetric.ResourceMetrics, int) {
destRM := pmetric.NewResourceMetrics()
destRM.SetSchemaUrl(srcRM.SchemaUrl())
srcRM.Resource().CopyTo(destRM.Resource())
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ResourceMetricsSize(destRM)
removedSize := 0
srcRM.ScopeMetrics().RemoveIf(func(srcSM pmetric.ScopeMetrics) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rawSmSize := sz.ScopeMetricsSize(srcSM)
smSize := sz.DeltaSize(rawSmSize)
if smSize > capacityLeft {
extSrcSM, extSmSize := extractScopeMetrics(srcSM, capacityLeft, sz)
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
removedSize += extSmSize
// There represents the delta between the delta sizes.
removedSize += smSize - rawSmSize - (sz.DeltaSize(rawSmSize-extSmSize) - (rawSmSize - extSmSize))
// It is possible that for the bytes scenario, the extracted field contains no scope metrics.
// Do not add it to the destination if that is the case.
if extSrcSM.Metrics().Len() > 0 {
extSrcSM.MoveTo(destRM.ScopeMetrics().AppendEmpty())
}
return extSrcSM.Metrics().Len() != 0
}
capacityLeft -= smSize
removedSize += smSize
srcSM.MoveTo(destRM.ScopeMetrics().AppendEmpty())
return true
})
return destRM, removedSize
}
// extractScopeMetrics extracts scope metrics and returns a new scope metrics with the specified number of data points.
func extractScopeMetrics(srcSM pmetric.ScopeMetrics, capacity int, sz sizer.MetricsSizer) (pmetric.ScopeMetrics, int) {
destSM := pmetric.NewScopeMetrics()
destSM.SetSchemaUrl(srcSM.SchemaUrl())
srcSM.Scope().CopyTo(destSM.Scope())
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ScopeMetricsSize(destSM)
removedSize := 0
srcSM.Metrics().RemoveIf(func(srcSM pmetric.Metric) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rawRmSize := sz.MetricSize(srcSM)
rmSize := sz.DeltaSize(rawRmSize)
if rmSize > capacityLeft {
extSrcDP, extRmSize := extractMetricDataPoints(srcSM, capacityLeft, sz)
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
removedSize += extRmSize
// There represents the delta between the delta sizes.
removedSize += rmSize - rawRmSize - (sz.DeltaSize(rawRmSize-extRmSize) - (rawRmSize - extRmSize))
// It is possible that for the bytes scenario, the extracted field contains no datapoints.
// Do not add it to the destination if that is the case.
if dataPointsLen(extSrcDP) > 0 {
extSrcDP.MoveTo(destSM.Metrics().AppendEmpty())
}
return dataPointsLen(extSrcDP) != 0
}
capacityLeft -= rmSize
removedSize += rmSize
srcSM.MoveTo(destSM.Metrics().AppendEmpty())
return true
})
return destSM, removedSize
}
func extractMetricDataPoints(srcMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) (pmetric.Metric, int) {
destMetric := pmetric.NewMetric()
destMetric.SetName(srcMetric.Name())
destMetric.SetDescription(srcMetric.Description())
destMetric.SetUnit(srcMetric.Unit())
srcMetric.Metadata().CopyTo(destMetric.Metadata())
var removedSize int
switch srcMetric.Type() {
case pmetric.MetricTypeGauge:
removedSize = extractGaugeDataPoints(srcMetric.Gauge(), destMetric, capacity, sz)
case pmetric.MetricTypeSum:
removedSize = extractSumDataPoints(srcMetric.Sum(), destMetric, capacity, sz)
destMetric.Sum().SetIsMonotonic(srcMetric.Sum().IsMonotonic())
destMetric.Sum().SetAggregationTemporality(srcMetric.Sum().AggregationTemporality())
case pmetric.MetricTypeHistogram:
removedSize = extractHistogramDataPoints(srcMetric.Histogram(), destMetric, capacity, sz)
destMetric.Histogram().SetAggregationTemporality(srcMetric.Histogram().AggregationTemporality())
case pmetric.MetricTypeExponentialHistogram:
removedSize = extractExponentialHistogramDataPoints(srcMetric.ExponentialHistogram(), destMetric, capacity, sz)
destMetric.ExponentialHistogram().SetAggregationTemporality(srcMetric.ExponentialHistogram().AggregationTemporality())
case pmetric.MetricTypeSummary:
removedSize = extractSummaryDataPoints(srcMetric.Summary(), destMetric, capacity, sz)
}
return destMetric, removedSize
}
func dataPointsLen(m pmetric.Metric) int {
switch m.Type() {
case pmetric.MetricTypeGauge:
return m.Gauge().DataPoints().Len()
case pmetric.MetricTypeSum:
return m.Sum().DataPoints().Len()
case pmetric.MetricTypeHistogram:
return m.Histogram().DataPoints().Len()
case pmetric.MetricTypeExponentialHistogram:
return m.ExponentialHistogram().DataPoints().Len()
case pmetric.MetricTypeSummary:
return m.Summary().DataPoints().Len()
}
return 0
}
func extractGaugeDataPoints(srcGauge pmetric.Gauge, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
destGauge := destMetric.SetEmptyGauge()
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
removedSize := 0
srcGauge.DataPoints().RemoveIf(func(srcDP pmetric.NumberDataPoint) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rdSize := sz.DeltaSize(sz.NumberDataPointSize(srcDP))
if rdSize > capacityLeft {
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
return false
}
capacityLeft -= rdSize
removedSize += rdSize
srcDP.MoveTo(destGauge.DataPoints().AppendEmpty())
return true
})
return removedSize
}
func extractSumDataPoints(srcSum pmetric.Sum, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
destSum := destMetric.SetEmptySum()
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
removedSize := 0
srcSum.DataPoints().RemoveIf(func(srcDP pmetric.NumberDataPoint) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rdSize := sz.DeltaSize(sz.NumberDataPointSize(srcDP))
if rdSize > capacityLeft {
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
return false
}
capacityLeft -= rdSize
removedSize += rdSize
srcDP.MoveTo(destSum.DataPoints().AppendEmpty())
return true
})
return removedSize
}
func extractHistogramDataPoints(srcHistogram pmetric.Histogram, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
destHistogram := destMetric.SetEmptyHistogram()
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
removedSize := 0
srcHistogram.DataPoints().RemoveIf(func(srcDP pmetric.HistogramDataPoint) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rdSize := sz.DeltaSize(sz.HistogramDataPointSize(srcDP))
if rdSize > capacityLeft {
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
return false
}
capacityLeft -= rdSize
removedSize += rdSize
srcDP.MoveTo(destHistogram.DataPoints().AppendEmpty())
return true
})
return removedSize
}
func extractExponentialHistogramDataPoints(srcExponentialHistogram pmetric.ExponentialHistogram, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
destExponentialHistogram := destMetric.SetEmptyExponentialHistogram()
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
removedSize := 0
srcExponentialHistogram.DataPoints().RemoveIf(func(srcDP pmetric.ExponentialHistogramDataPoint) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rdSize := sz.DeltaSize(sz.ExponentialHistogramDataPointSize(srcDP))
if rdSize > capacityLeft {
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
return false
}
capacityLeft -= rdSize
removedSize += rdSize
srcDP.MoveTo(destExponentialHistogram.DataPoints().AppendEmpty())
return true
})
return removedSize
}
func extractSummaryDataPoints(srcSummary pmetric.Summary, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
destSummary := destMetric.SetEmptySummary()
// Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
removedSize := 0
srcSummary.DataPoints().RemoveIf(func(srcDP pmetric.SummaryDataPoint) bool {
// If the no more capacity left just return.
if capacityLeft == 0 {
return false
}
rdSize := sz.DeltaSize(sz.SummaryDataPointSize(srcDP))
if rdSize > capacityLeft {
// This cannot make it to exactly 0 for the bytes,
// force it to be 0 since that is the stopping condition.
capacityLeft = 0
return false
}
capacityLeft -= rdSize
removedSize += rdSize
srcDP.MoveTo(destSummary.DataPoints().AppendEmpty())
return true
})
return removedSize
}