Debian Codegraph @

processor/batchprocessor/splitmetrics.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package batchprocessor // import "go.opentelemetry.io/collector/processor/batchprocessor"
5
6 import (
7 "go.opentelemetry.io/collector/pdata/pmetric"
8 )
9
10 // splitMetrics removes metrics from the input data and returns a new data of the specified size.
11 func splitMetrics(size int, src pmetric.Metrics) pmetric.Metrics {
12 dataPoints := src.DataPointCount()
13 if dataPoints <= size {
14 return src
15 }
16 totalCopiedDataPoints := 0
17 dest := pmetric.NewMetrics()
18
19 src.ResourceMetrics().RemoveIf(func(srcRs pmetric.ResourceMetrics) bool {
20 // If we are done skip everything else.
21 if totalCopiedDataPoints == size {
22 return false
23 }
24
25 // If it fully fits
26 srcRsDataPointCount := resourceMetricsDPC(srcRs)
27 if (totalCopiedDataPoints + srcRsDataPointCount) <= size {
28 totalCopiedDataPoints += srcRsDataPointCount
29 srcRs.MoveTo(dest.ResourceMetrics().AppendEmpty())
30 return true
31 }
32
33 destRs := dest.ResourceMetrics().AppendEmpty()
34 srcRs.Resource().CopyTo(destRs.Resource())
35 srcRs.ScopeMetrics().RemoveIf(func(srcIlm pmetric.ScopeMetrics) bool {
36 // If we are done skip everything else.
37 if totalCopiedDataPoints == size {
38 return false
39 }
40
41 // If possible to move all metrics do that.
42 srcIlmDataPointCount := scopeMetricsDPC(srcIlm)
43 if srcIlmDataPointCount+totalCopiedDataPoints <= size {
44 totalCopiedDataPoints += srcIlmDataPointCount
45 srcIlm.MoveTo(destRs.ScopeMetrics().AppendEmpty())
46 return true
47 }
48
49 destIlm := destRs.ScopeMetrics().AppendEmpty()
50 srcIlm.Scope().CopyTo(destIlm.Scope())
51 srcIlm.Metrics().RemoveIf(func(srcMetric pmetric.Metric) bool {
52 // If we are done skip everything else.
53 if totalCopiedDataPoints == size {
54 return false
55 }
56
57 // If possible to move all points do that.
58 srcMetricPointCount := metricDPC(srcMetric)
59 if srcMetricPointCount+totalCopiedDataPoints <= size {
60 totalCopiedDataPoints += srcMetricPointCount
61 srcMetric.MoveTo(destIlm.Metrics().AppendEmpty())
62 return true
63 }
64
65 // If the metric has more data points than free slots we should split it.
66 copiedDataPoints, remove := splitMetric(srcMetric, destIlm.Metrics().AppendEmpty(), size-totalCopiedDataPoints)
67 totalCopiedDataPoints += copiedDataPoints
68 return remove
69 })
70 return false
71 })
72 return srcRs.ScopeMetrics().Len() == 0
73 })
74
75 return dest
76 }
77
78 // resourceMetricsDPC calculates the total number of data points in the pmetric.ResourceMetrics.
79 func resourceMetricsDPC(rs pmetric.ResourceMetrics) int {
80 dataPointCount := 0
81 ilms := rs.ScopeMetrics()
82 for k := 0; k < ilms.Len(); k++ {
83 dataPointCount += scopeMetricsDPC(ilms.At(k))
84 }
85 return dataPointCount
86 }
87
88 // scopeMetricsDPC calculates the total number of data points in the pmetric.ScopeMetrics.
89 func scopeMetricsDPC(ilm pmetric.ScopeMetrics) int {
90 dataPointCount := 0
91 ms := ilm.Metrics()
92 for k := 0; k < ms.Len(); k++ {
93 dataPointCount += metricDPC(ms.At(k))
94 }
95 return dataPointCount
96 }
97
98 // metricDPC calculates the total number of data points in the pmetric.Metric.
99 func metricDPC(ms pmetric.Metric) int {
100 switch ms.Type() {
101 case pmetric.MetricTypeGauge:
102 return ms.Gauge().DataPoints().Len()
103 case pmetric.MetricTypeSum:
104 return ms.Sum().DataPoints().Len()
105 case pmetric.MetricTypeHistogram:
106 return ms.Histogram().DataPoints().Len()
107 case pmetric.MetricTypeExponentialHistogram:
108 return ms.ExponentialHistogram().DataPoints().Len()
109 case pmetric.MetricTypeSummary:
110 return ms.Summary().DataPoints().Len()
111 }
112 return 0
113 }
114
115 // splitMetric removes metric points from the input data and moves data of the specified size to destination.
116 // Returns size of moved data and boolean describing, whether the metric should be removed from original slice.
117 func splitMetric(ms, dest pmetric.Metric, size int) (int, bool) {
118 dest.SetName(ms.Name())
119 dest.SetDescription(ms.Description())
120 dest.SetUnit(ms.Unit())
121
122 switch ms.Type() {
123 case pmetric.MetricTypeGauge:
124 return splitNumberDataPoints(ms.Gauge().DataPoints(), dest.SetEmptyGauge().DataPoints(), size)
125 case pmetric.MetricTypeSum:
126 destSum := dest.SetEmptySum()
127 destSum.SetAggregationTemporality(ms.Sum().AggregationTemporality())
128 destSum.SetIsMonotonic(ms.Sum().IsMonotonic())
129 return splitNumberDataPoints(ms.Sum().DataPoints(), destSum.DataPoints(), size)
130 case pmetric.MetricTypeHistogram:
131 destHistogram := dest.SetEmptyHistogram()
132 destHistogram.SetAggregationTemporality(ms.Histogram().AggregationTemporality())
133 return splitHistogramDataPoints(ms.Histogram().DataPoints(), destHistogram.DataPoints(), size)
134 case pmetric.MetricTypeExponentialHistogram:
135 destHistogram := dest.SetEmptyExponentialHistogram()
136 destHistogram.SetAggregationTemporality(ms.ExponentialHistogram().AggregationTemporality())
137 return splitExponentialHistogramDataPoints(ms.ExponentialHistogram().DataPoints(), destHistogram.DataPoints(), size)
138 case pmetric.MetricTypeSummary:
139 return splitSummaryDataPoints(ms.Summary().DataPoints(), dest.SetEmptySummary().DataPoints(), size)
140 }
141 return size, false
142 }
143
144 func splitNumberDataPoints(src, dst pmetric.NumberDataPointSlice, size int) (int, bool) {
145 dst.EnsureCapacity(size)
146 i := 0
147 src.RemoveIf(func(dp pmetric.NumberDataPoint) bool {
148 if i < size {
149 dp.MoveTo(dst.AppendEmpty())
150 i++
151 return true
152 }
153 return false
154 })
155 return size, false
156 }
157
158 func splitHistogramDataPoints(src, dst pmetric.HistogramDataPointSlice, size int) (int, bool) {
159 dst.EnsureCapacity(size)
160 i := 0
161 src.RemoveIf(func(dp pmetric.HistogramDataPoint) bool {
162 if i < size {
163 dp.MoveTo(dst.AppendEmpty())
164 i++
165 return true
166 }
167 return false
168 })
169 return size, false
170 }
171
172 func splitExponentialHistogramDataPoints(src, dst pmetric.ExponentialHistogramDataPointSlice, size int) (int, bool) {
173 dst.EnsureCapacity(size)
174 i := 0
175 src.RemoveIf(func(dp pmetric.ExponentialHistogramDataPoint) bool {
176 if i < size {
177 dp.MoveTo(dst.AppendEmpty())
178 i++
179 return true
180 }
181 return false
182 })
183 return size, false
184 }
185
186 func splitSummaryDataPoints(src, dst pmetric.SummaryDataPointSlice, size int) (int, bool) {
187 dst.EnsureCapacity(size)
188 i := 0
189 src.RemoveIf(func(dp pmetric.SummaryDataPoint) bool {
190 if i < size {
191 dp.MoveTo(dst.AppendEmpty())
192 i++
193 return true
194 }
195 return false
196 })
197 return size, false
198 }