|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.metrics.export; |
| 7 | + |
| 8 | +import io.opentelemetry.sdk.metrics.data.DoublePointData; |
| 9 | +import io.opentelemetry.sdk.metrics.data.ExponentialHistogramData; |
| 10 | +import io.opentelemetry.sdk.metrics.data.ExponentialHistogramPointData; |
| 11 | +import io.opentelemetry.sdk.metrics.data.HistogramData; |
| 12 | +import io.opentelemetry.sdk.metrics.data.HistogramPointData; |
| 13 | +import io.opentelemetry.sdk.metrics.data.LongPointData; |
| 14 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 15 | +import io.opentelemetry.sdk.metrics.data.PointData; |
| 16 | +import io.opentelemetry.sdk.metrics.data.SumData; |
| 17 | +import io.opentelemetry.sdk.metrics.data.SummaryPointData; |
| 18 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableExponentialHistogramData; |
| 19 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData; |
| 20 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramData; |
| 21 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData; |
| 22 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData; |
| 23 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/** |
| 30 | + * Batches metric data into multiple batches based on the maximum export batch size. This is used by |
| 31 | + * the {@link PeriodicMetricReader} to batch metric data before exporting it. |
| 32 | + * |
| 33 | + * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change |
| 34 | + * at any time. |
| 35 | + */ |
| 36 | +class MetricExportBatcher { |
| 37 | + |
| 38 | + private MetricExportBatcher() {} |
| 39 | + |
| 40 | + private static void validateMaxExportBatchSize(int maxExportBatchSize) { |
| 41 | + if (maxExportBatchSize <= 0) { |
| 42 | + throw new IllegalArgumentException("maxExportBatchSize must be positive"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Batches the given metric data into multiple batches based on the maximum export batch size. |
| 48 | + * |
| 49 | + * @param metrics The collection of metric data objects to batch based on the number of data |
| 50 | + * points they contain. |
| 51 | + * @return A collection of batches of metric data. |
| 52 | + */ |
| 53 | + static Collection<Collection<MetricData>> batchMetrics( |
| 54 | + Collection<MetricData> metrics, int maxExportBatchSize) { |
| 55 | + validateMaxExportBatchSize(maxExportBatchSize); |
| 56 | + if (metrics.isEmpty()) { |
| 57 | + return Collections.emptyList(); |
| 58 | + } |
| 59 | + Collection<Collection<MetricData>> preparedBatchesForExport = new ArrayList<>(); |
| 60 | + List<MetricData> currentBatch = new ArrayList<>(maxExportBatchSize); |
| 61 | + int currentPointsInBatch = 0; |
| 62 | + for (MetricData metricData : metrics) { |
| 63 | + int totalPointsInMetric = metricData.getData().getPoints().size(); |
| 64 | + if (currentPointsInBatch + totalPointsInMetric <= maxExportBatchSize) { |
| 65 | + currentBatch.add(metricData); |
| 66 | + currentPointsInBatch += totalPointsInMetric; |
| 67 | + continue; |
| 68 | + } |
| 69 | + int currentIndex = 0; |
| 70 | + List<PointData> originalPointsList = new ArrayList<>(metricData.getData().getPoints()); |
| 71 | + while (currentIndex < totalPointsInMetric) { |
| 72 | + if (currentPointsInBatch == maxExportBatchSize) { |
| 73 | + preparedBatchesForExport.add(currentBatch); |
| 74 | + currentBatch = new ArrayList<>(maxExportBatchSize); |
| 75 | + currentPointsInBatch = 0; |
| 76 | + } |
| 77 | + int pointsToTake = |
| 78 | + Math.min(maxExportBatchSize - currentPointsInBatch, totalPointsInMetric - currentIndex); |
| 79 | + currentBatch.add( |
| 80 | + copyMetricData(metricData, originalPointsList, currentIndex, pointsToTake)); |
| 81 | + currentPointsInBatch += pointsToTake; |
| 82 | + currentIndex += pointsToTake; |
| 83 | + } |
| 84 | + } |
| 85 | + if (!currentBatch.isEmpty()) { |
| 86 | + preparedBatchesForExport.add(currentBatch); |
| 87 | + } |
| 88 | + return Collections.unmodifiableCollection(preparedBatchesForExport); |
| 89 | + } |
| 90 | + |
| 91 | + private static MetricData copyMetricData( |
| 92 | + MetricData original, |
| 93 | + List<PointData> originalPointsList, |
| 94 | + int dataPointsOffset, |
| 95 | + int dataPointsToTake) { |
| 96 | + List<PointData> points = |
| 97 | + Collections.unmodifiableList( |
| 98 | + new ArrayList<>( |
| 99 | + originalPointsList.subList(dataPointsOffset, dataPointsOffset + dataPointsToTake))); |
| 100 | + return createMetricDataWithPoints(original, points); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates a new MetricData with the given points. |
| 105 | + * |
| 106 | + * @param original The original MetricData. |
| 107 | + * @param points The points to use for the new MetricData. |
| 108 | + * @return A new MetricData with the given points. |
| 109 | + */ |
| 110 | + @SuppressWarnings("unchecked") |
| 111 | + private static MetricData createMetricDataWithPoints( |
| 112 | + MetricData original, Collection<PointData> points) { |
| 113 | + switch (original.getType()) { |
| 114 | + case DOUBLE_GAUGE: |
| 115 | + return ImmutableMetricData.createDoubleGauge( |
| 116 | + original.getResource(), |
| 117 | + original.getInstrumentationScopeInfo(), |
| 118 | + original.getName(), |
| 119 | + original.getDescription(), |
| 120 | + original.getUnit(), |
| 121 | + ImmutableGaugeData.create((Collection<DoublePointData>) (Collection<?>) points)); |
| 122 | + case LONG_GAUGE: |
| 123 | + return ImmutableMetricData.createLongGauge( |
| 124 | + original.getResource(), |
| 125 | + original.getInstrumentationScopeInfo(), |
| 126 | + original.getName(), |
| 127 | + original.getDescription(), |
| 128 | + original.getUnit(), |
| 129 | + ImmutableGaugeData.create((Collection<LongPointData>) (Collection<?>) points)); |
| 130 | + case DOUBLE_SUM: |
| 131 | + SumData<DoublePointData> doubleSumData = original.getDoubleSumData(); |
| 132 | + return ImmutableMetricData.createDoubleSum( |
| 133 | + original.getResource(), |
| 134 | + original.getInstrumentationScopeInfo(), |
| 135 | + original.getName(), |
| 136 | + original.getDescription(), |
| 137 | + original.getUnit(), |
| 138 | + ImmutableSumData.create( |
| 139 | + doubleSumData.isMonotonic(), |
| 140 | + doubleSumData.getAggregationTemporality(), |
| 141 | + (Collection<DoublePointData>) (Collection<?>) points)); |
| 142 | + case LONG_SUM: |
| 143 | + SumData<LongPointData> longSumData = original.getLongSumData(); |
| 144 | + return ImmutableMetricData.createLongSum( |
| 145 | + original.getResource(), |
| 146 | + original.getInstrumentationScopeInfo(), |
| 147 | + original.getName(), |
| 148 | + original.getDescription(), |
| 149 | + original.getUnit(), |
| 150 | + ImmutableSumData.create( |
| 151 | + longSumData.isMonotonic(), |
| 152 | + longSumData.getAggregationTemporality(), |
| 153 | + (Collection<LongPointData>) (Collection<?>) points)); |
| 154 | + case HISTOGRAM: |
| 155 | + HistogramData histogramData = original.getHistogramData(); |
| 156 | + return ImmutableMetricData.createDoubleHistogram( |
| 157 | + original.getResource(), |
| 158 | + original.getInstrumentationScopeInfo(), |
| 159 | + original.getName(), |
| 160 | + original.getDescription(), |
| 161 | + original.getUnit(), |
| 162 | + ImmutableHistogramData.create( |
| 163 | + histogramData.getAggregationTemporality(), |
| 164 | + (Collection<HistogramPointData>) (Collection<?>) points)); |
| 165 | + case EXPONENTIAL_HISTOGRAM: |
| 166 | + ExponentialHistogramData expHistogramData = original.getExponentialHistogramData(); |
| 167 | + return ImmutableMetricData.createExponentialHistogram( |
| 168 | + original.getResource(), |
| 169 | + original.getInstrumentationScopeInfo(), |
| 170 | + original.getName(), |
| 171 | + original.getDescription(), |
| 172 | + original.getUnit(), |
| 173 | + ImmutableExponentialHistogramData.create( |
| 174 | + expHistogramData.getAggregationTemporality(), |
| 175 | + (Collection<ExponentialHistogramPointData>) (Collection<?>) points)); |
| 176 | + case SUMMARY: |
| 177 | + return ImmutableMetricData.createDoubleSummary( |
| 178 | + original.getResource(), |
| 179 | + original.getInstrumentationScopeInfo(), |
| 180 | + original.getName(), |
| 181 | + original.getDescription(), |
| 182 | + original.getUnit(), |
| 183 | + ImmutableSummaryData.create((Collection<SummaryPointData>) (Collection<?>) points)); |
| 184 | + } |
| 185 | + throw new UnsupportedOperationException("Unsupported metric type: " + original.getType()); |
| 186 | + } |
| 187 | +} |
0 commit comments