Debian Codegraph @

exporter/exporterhelper/internal/queuebatch/metrics_batch.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package queuebatch // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch"
5
6 import (
7 "context"
8 "errors"
9 "fmt"
10
11 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
12 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/sizer"
13 "go.opentelemetry.io/collector/pdata/pmetric"
14 )
15
16 // MergeSplit splits and/or merges the provided metrics request and the current request into one or more requests
17 // conforming with the MaxSizeConfig.
18 func (req *metricsRequest) MergeSplit(_ context.Context, maxSize int, szt request.SizerType, r2 request.Request) ([]request.Request, error) {
19 var sz sizer.MetricsSizer
20 switch szt {
21 case request.SizerTypeItems:
22 sz = &sizer.MetricsCountSizer{}
23 case request.SizerTypeBytes:
24 sz = &sizer.MetricsBytesSizer{}
25 default:
26 return nil, errors.New("unknown sizer type")
27 }
28
29 if r2 != nil {
30 req2, ok := r2.(*metricsRequest)
31 if !ok {
32 return nil, errors.New("invalid input type")
33 }
34 req2.mergeTo(req, sz)
35 }
36
37 // If no limit we can simply merge the new request into the current and return.
38 if maxSize == 0 {
39 return []request.Request{req}, nil
40 }
41 return req.split(maxSize, sz)
42 }
43
44 func (req *metricsRequest) mergeTo(dst *metricsRequest, sz sizer.MetricsSizer) {
45 if sz != nil {
46 dst.setCachedSize(dst.size(sz) + req.size(sz))
47 req.setCachedSize(0)
48 }
49 req.md.ResourceMetrics().MoveAndAppendTo(dst.md.ResourceMetrics())
50 }
51
52 func (req *metricsRequest) split(maxSize int, sz sizer.MetricsSizer) ([]request.Request, error) {
53 var res []request.Request
54 for req.size(sz) > maxSize {
55 md, rmSize := extractMetrics(req.md, maxSize, sz)
56 if md.DataPointCount() == 0 {
57 return res, fmt.Errorf("one datapoint size is greater than max size, dropping items: %d", req.md.DataPointCount())
58 }
59 req.setCachedSize(req.size(sz) - rmSize)
60 res = append(res, newMetricsRequest(md))
61 }
62 res = append(res, req)
63 return res, nil
64 }
65
66 // extractMetrics extracts metrics from srcMetrics until capacity is reached.
67 func extractMetrics(srcMetrics pmetric.Metrics, capacity int, sz sizer.MetricsSizer) (pmetric.Metrics, int) {
68 destMetrics := pmetric.NewMetrics()
69 capacityLeft := capacity - sz.MetricsSize(destMetrics)
70 removedSize := 0
71 srcMetrics.ResourceMetrics().RemoveIf(func(srcRM pmetric.ResourceMetrics) bool {
72 // If the no more capacity left just return.
73 if capacityLeft == 0 {
74 return false
75 }
76 rawRlSize := sz.ResourceMetricsSize(srcRM)
77 rlSize := sz.DeltaSize(rawRlSize)
78 if rlSize > capacityLeft {
79 extSrcRM, extRmSize := extractResourceMetrics(srcRM, capacityLeft, sz)
80 // This cannot make it to exactly 0 for the bytes,
81 // force it to be 0 since that is the stopping condition.
82 capacityLeft = 0
83 removedSize += extRmSize
84 // There represents the delta between the delta sizes.
85 removedSize += rlSize - rawRlSize - (sz.DeltaSize(rawRlSize-extRmSize) - (rawRlSize - extRmSize))
86 // It is possible that for the bytes scenario, the extracted field contains no scope metrics.
87 // Do not add it to the destination if that is the case.
88 if extSrcRM.ScopeMetrics().Len() > 0 {
89 extSrcRM.MoveTo(destMetrics.ResourceMetrics().AppendEmpty())
90 }
91 return extSrcRM.ScopeMetrics().Len() != 0
92 }
93 capacityLeft -= rlSize
94 removedSize += rlSize
95 srcRM.MoveTo(destMetrics.ResourceMetrics().AppendEmpty())
96 return true
97 })
98 return destMetrics, removedSize
99 }
100
101 // extractResourceMetrics extracts resource metrics and returns a new resource metrics with the specified number of data points.
102 func extractResourceMetrics(srcRM pmetric.ResourceMetrics, capacity int, sz sizer.MetricsSizer) (pmetric.ResourceMetrics, int) {
103 destRM := pmetric.NewResourceMetrics()
104 destRM.SetSchemaUrl(srcRM.SchemaUrl())
105 srcRM.Resource().CopyTo(destRM.Resource())
106 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
107 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ResourceMetricsSize(destRM)
108 removedSize := 0
109 srcRM.ScopeMetrics().RemoveIf(func(srcSM pmetric.ScopeMetrics) bool {
110 // If the no more capacity left just return.
111 if capacityLeft == 0 {
112 return false
113 }
114 rawSmSize := sz.ScopeMetricsSize(srcSM)
115 smSize := sz.DeltaSize(rawSmSize)
116 if smSize > capacityLeft {
117 extSrcSM, extSmSize := extractScopeMetrics(srcSM, capacityLeft, sz)
118 // This cannot make it to exactly 0 for the bytes,
119 // force it to be 0 since that is the stopping condition.
120 capacityLeft = 0
121 removedSize += extSmSize
122 // There represents the delta between the delta sizes.
123 removedSize += smSize - rawSmSize - (sz.DeltaSize(rawSmSize-extSmSize) - (rawSmSize - extSmSize))
124 // It is possible that for the bytes scenario, the extracted field contains no scope metrics.
125 // Do not add it to the destination if that is the case.
126 if extSrcSM.Metrics().Len() > 0 {
127 extSrcSM.MoveTo(destRM.ScopeMetrics().AppendEmpty())
128 }
129 return extSrcSM.Metrics().Len() != 0
130 }
131 capacityLeft -= smSize
132 removedSize += smSize
133 srcSM.MoveTo(destRM.ScopeMetrics().AppendEmpty())
134 return true
135 })
136 return destRM, removedSize
137 }
138
139 // extractScopeMetrics extracts scope metrics and returns a new scope metrics with the specified number of data points.
140 func extractScopeMetrics(srcSM pmetric.ScopeMetrics, capacity int, sz sizer.MetricsSizer) (pmetric.ScopeMetrics, int) {
141 destSM := pmetric.NewScopeMetrics()
142 destSM.SetSchemaUrl(srcSM.SchemaUrl())
143 srcSM.Scope().CopyTo(destSM.Scope())
144 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
145 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ScopeMetricsSize(destSM)
146 removedSize := 0
147 srcSM.Metrics().RemoveIf(func(srcSM pmetric.Metric) bool {
148 // If the no more capacity left just return.
149 if capacityLeft == 0 {
150 return false
151 }
152 rawRmSize := sz.MetricSize(srcSM)
153 rmSize := sz.DeltaSize(rawRmSize)
154 if rmSize > capacityLeft {
155 extSrcDP, extRmSize := extractMetricDataPoints(srcSM, capacityLeft, sz)
156 // This cannot make it to exactly 0 for the bytes,
157 // force it to be 0 since that is the stopping condition.
158 capacityLeft = 0
159 removedSize += extRmSize
160 // There represents the delta between the delta sizes.
161 removedSize += rmSize - rawRmSize - (sz.DeltaSize(rawRmSize-extRmSize) - (rawRmSize - extRmSize))
162 // It is possible that for the bytes scenario, the extracted field contains no datapoints.
163 // Do not add it to the destination if that is the case.
164 if dataPointsLen(extSrcDP) > 0 {
165 extSrcDP.MoveTo(destSM.Metrics().AppendEmpty())
166 }
167 return dataPointsLen(extSrcDP) != 0
168 }
169 capacityLeft -= rmSize
170 removedSize += rmSize
171 srcSM.MoveTo(destSM.Metrics().AppendEmpty())
172 return true
173 })
174 return destSM, removedSize
175 }
176
177 func extractMetricDataPoints(srcMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) (pmetric.Metric, int) {
178 destMetric := pmetric.NewMetric()
179 destMetric.SetName(srcMetric.Name())
180 destMetric.SetDescription(srcMetric.Description())
181 destMetric.SetUnit(srcMetric.Unit())
182 srcMetric.Metadata().CopyTo(destMetric.Metadata())
183
184 var removedSize int
185 switch srcMetric.Type() {
186 case pmetric.MetricTypeGauge:
187 removedSize = extractGaugeDataPoints(srcMetric.Gauge(), destMetric, capacity, sz)
188 case pmetric.MetricTypeSum:
189 removedSize = extractSumDataPoints(srcMetric.Sum(), destMetric, capacity, sz)
190 destMetric.Sum().SetIsMonotonic(srcMetric.Sum().IsMonotonic())
191 destMetric.Sum().SetAggregationTemporality(srcMetric.Sum().AggregationTemporality())
192 case pmetric.MetricTypeHistogram:
193 removedSize = extractHistogramDataPoints(srcMetric.Histogram(), destMetric, capacity, sz)
194 destMetric.Histogram().SetAggregationTemporality(srcMetric.Histogram().AggregationTemporality())
195 case pmetric.MetricTypeExponentialHistogram:
196 removedSize = extractExponentialHistogramDataPoints(srcMetric.ExponentialHistogram(), destMetric, capacity, sz)
197 destMetric.ExponentialHistogram().SetAggregationTemporality(srcMetric.ExponentialHistogram().AggregationTemporality())
198 case pmetric.MetricTypeSummary:
199 removedSize = extractSummaryDataPoints(srcMetric.Summary(), destMetric, capacity, sz)
200 }
201 return destMetric, removedSize
202 }
203
204 func dataPointsLen(m pmetric.Metric) int {
205 switch m.Type() {
206 case pmetric.MetricTypeGauge:
207 return m.Gauge().DataPoints().Len()
208 case pmetric.MetricTypeSum:
209 return m.Sum().DataPoints().Len()
210 case pmetric.MetricTypeHistogram:
211 return m.Histogram().DataPoints().Len()
212 case pmetric.MetricTypeExponentialHistogram:
213 return m.ExponentialHistogram().DataPoints().Len()
214 case pmetric.MetricTypeSummary:
215 return m.Summary().DataPoints().Len()
216 }
217 return 0
218 }
219
220 func extractGaugeDataPoints(srcGauge pmetric.Gauge, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
221 destGauge := destMetric.SetEmptyGauge()
222
223 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
224 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
225 removedSize := 0
226
227 srcGauge.DataPoints().RemoveIf(func(srcDP pmetric.NumberDataPoint) bool {
228 // If the no more capacity left just return.
229 if capacityLeft == 0 {
230 return false
231 }
232
233 rdSize := sz.DeltaSize(sz.NumberDataPointSize(srcDP))
234 if rdSize > capacityLeft {
235 // This cannot make it to exactly 0 for the bytes,
236 // force it to be 0 since that is the stopping condition.
237 capacityLeft = 0
238 return false
239 }
240 capacityLeft -= rdSize
241 removedSize += rdSize
242 srcDP.MoveTo(destGauge.DataPoints().AppendEmpty())
243 return true
244 })
245 return removedSize
246 }
247
248 func extractSumDataPoints(srcSum pmetric.Sum, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
249 destSum := destMetric.SetEmptySum()
250 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
251 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
252 removedSize := 0
253 srcSum.DataPoints().RemoveIf(func(srcDP pmetric.NumberDataPoint) bool {
254 // If the no more capacity left just return.
255 if capacityLeft == 0 {
256 return false
257 }
258
259 rdSize := sz.DeltaSize(sz.NumberDataPointSize(srcDP))
260 if rdSize > capacityLeft {
261 // This cannot make it to exactly 0 for the bytes,
262 // force it to be 0 since that is the stopping condition.
263 capacityLeft = 0
264 return false
265 }
266 capacityLeft -= rdSize
267 removedSize += rdSize
268 srcDP.MoveTo(destSum.DataPoints().AppendEmpty())
269 return true
270 })
271 return removedSize
272 }
273
274 func extractHistogramDataPoints(srcHistogram pmetric.Histogram, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
275 destHistogram := destMetric.SetEmptyHistogram()
276 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
277 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
278 removedSize := 0
279 srcHistogram.DataPoints().RemoveIf(func(srcDP pmetric.HistogramDataPoint) bool {
280 // If the no more capacity left just return.
281 if capacityLeft == 0 {
282 return false
283 }
284
285 rdSize := sz.DeltaSize(sz.HistogramDataPointSize(srcDP))
286 if rdSize > capacityLeft {
287 // This cannot make it to exactly 0 for the bytes,
288 // force it to be 0 since that is the stopping condition.
289 capacityLeft = 0
290 return false
291 }
292 capacityLeft -= rdSize
293 removedSize += rdSize
294 srcDP.MoveTo(destHistogram.DataPoints().AppendEmpty())
295 return true
296 })
297 return removedSize
298 }
299
300 func extractExponentialHistogramDataPoints(srcExponentialHistogram pmetric.ExponentialHistogram, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
301 destExponentialHistogram := destMetric.SetEmptyExponentialHistogram()
302 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
303 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
304 removedSize := 0
305 srcExponentialHistogram.DataPoints().RemoveIf(func(srcDP pmetric.ExponentialHistogramDataPoint) bool {
306 // If the no more capacity left just return.
307 if capacityLeft == 0 {
308 return false
309 }
310
311 rdSize := sz.DeltaSize(sz.ExponentialHistogramDataPointSize(srcDP))
312 if rdSize > capacityLeft {
313 // This cannot make it to exactly 0 for the bytes,
314 // force it to be 0 since that is the stopping condition.
315 capacityLeft = 0
316 return false
317 }
318 capacityLeft -= rdSize
319 removedSize += rdSize
320 srcDP.MoveTo(destExponentialHistogram.DataPoints().AppendEmpty())
321 return true
322 })
323 return removedSize
324 }
325
326 func extractSummaryDataPoints(srcSummary pmetric.Summary, destMetric pmetric.Metric, capacity int, sz sizer.MetricsSizer) int {
327 destSummary := destMetric.SetEmptySummary()
328 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
329 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.MetricSize(destMetric)
330 removedSize := 0
331 srcSummary.DataPoints().RemoveIf(func(srcDP pmetric.SummaryDataPoint) bool {
332 // If the no more capacity left just return.
333 if capacityLeft == 0 {
334 return false
335 }
336
337 rdSize := sz.DeltaSize(sz.SummaryDataPointSize(srcDP))
338 if rdSize > capacityLeft {
339 // This cannot make it to exactly 0 for the bytes,
340 // force it to be 0 since that is the stopping condition.
341 capacityLeft = 0
342 return false
343 }
344 capacityLeft -= rdSize
345 removedSize += rdSize
346 srcDP.MoveTo(destSummary.DataPoints().AppendEmpty())
347 return true
348 })
349 return removedSize
350 }