Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
91b087c
WIP: Add metrics batcher in the SDK
psx95 Apr 13, 2026
9534dea
Allow exporting metricData batches
psx95 Apr 14, 2026
70d4e15
Make existing tests compatible with changes
psx95 Apr 14, 2026
6b97875
Add docs and update existing tests
psx95 Apr 14, 2026
b2b78c3
Fix MetricExportBatcher logic and add tests
psx95 Apr 15, 2026
40226ae
Add support for EXPONENTIAL_HISTOGRAM and SUMMARY data types
psx95 Apr 15, 2026
af9fad2
Fix metrics checkstyle issue
psx95 Apr 15, 2026
3c3bc1a
Add missing generated diff files
psx95 Apr 15, 2026
7b3feb8
Update unit tests for enhanced coverage
psx95 Apr 15, 2026
0083b54
Add unit tests for PeriodicMetricReader
psx95 Apr 15, 2026
1b3ad77
Fix checkstyle issues
psx95 Apr 15, 2026
5e18a7c
Clean up inline code comments
psx95 Apr 15, 2026
a5b0120
Fix bug for miscalculating remaining capacity of a batch
psx95 Apr 15, 2026
1751b57
Add missing Javadoc for public facing API
psx95 Apr 15, 2026
6ea146e
Refactor logic in prepareExportBatches to remove redundancy
psx95 Apr 15, 2026
c5356f6
Address comment about defensive copy for original point sublist
psx95 Apr 16, 2026
c02c3c8
Prevent copying MetricData for 0 points
psx95 Apr 16, 2026
1e8c645
Add test case to verify there are no batches with empty metric points
psx95 Apr 16, 2026
48575c2
Switch to sequential export
psx95 Apr 19, 2026
e90ab8d
Add tests to verify sequential export for PeriodicMetricReader
psx95 Apr 19, 2026
6538745
Fix batched forceFlush failure propagation
zeitlinger Apr 21, 2026
1b7874d
Make metric export batching linear
zeitlinger Apr 21, 2026
741a8a4
Restore forceFlush partial-success behavior
zeitlinger Apr 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of opentelemetry-sdk-metrics-1.62.0-SNAPSHOT.jar against opentelemetry-sdk-metrics-1.61.0.jar
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.export.PeriodicMetricReaderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.export.PeriodicMetricReaderBuilder setMaxExportBatchSize(int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.export;

import io.opentelemetry.sdk.metrics.data.Data;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramData;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramPointData;
import io.opentelemetry.sdk.metrics.data.HistogramData;
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.PointData;
import io.opentelemetry.sdk.metrics.data.SumData;
import io.opentelemetry.sdk.metrics.data.SummaryPointData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableExponentialHistogramData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* Batches metric data into multiple batches based on the maximum export batch size. This is used by
* the {@link PeriodicMetricReader} to batch metric data before exporting it.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
class MetricExportBatcher {
private final int maxExportBatchSize;

/**
* Creates a new {@link MetricExportBatcher} with the given maximum export batch size.
*
* @param maxExportBatchSize The maximum number of {@link Data#getPoints()} in each export.
*/
MetricExportBatcher(int maxExportBatchSize) {
if (maxExportBatchSize <= 0) {
throw new IllegalArgumentException("maxExportBatchSize must be positive");
}
this.maxExportBatchSize = maxExportBatchSize;
}

@Override
public String toString() {
return "MetricExportBatcher{maxExportBatchSize=" + maxExportBatchSize + "}";
}

/**
* Batches the given metric data into multiple batches based on the maximum export batch size.
*
* @param metrics The collection of metric data objects to batch based on the number of data
* points they contain.
* @return A collection of batches of metric data.
*/
Collection<Collection<MetricData>> batchMetrics(Collection<MetricData> metrics) {
if (metrics.isEmpty()) {
return Collections.emptyList();
}
Collection<Collection<MetricData>> preparedBatchesForExport = new ArrayList<>();
BatchState currentBatch = new BatchState(new ArrayList<>(maxExportBatchSize), 0);

// Fill active batch and split overlapping metric points if needed
for (MetricData metricData : metrics) {
MetricDataSplitOperationResult splitResult = prepareExportBatches(metricData, currentBatch);
preparedBatchesForExport.addAll(splitResult.getPreparedBatches());
currentBatch = splitResult.getLastInProgressBatch();
}

// Push trailing capacity block
if (!currentBatch.metrics.isEmpty()) {
preparedBatchesForExport.add(currentBatch.metrics);
}
return Collections.unmodifiableCollection(preparedBatchesForExport);
}

/**
* Prepares export batches from a single metric data object. This function only operates on a
* single metric data object, fills up the current batch with as many points as possible from the
* metric data object, and then creates new metric data objects for the remaining points.
*
* @param metricData The metric data object to split.
* @param currentBatch The current batch of metric data objects.
* @return A result containing the prepared batches and the last in-progress batch.
*/
private MetricDataSplitOperationResult prepareExportBatches(
MetricData metricData, BatchState currentBatch) {
int remainingCapacityInCurrentBatch = maxExportBatchSize - currentBatch.points;
int totalPointsInMetricData = metricData.getData().getPoints().size();

if (remainingCapacityInCurrentBatch >= totalPointsInMetricData) {
currentBatch.metrics.add(metricData);
currentBatch.points += totalPointsInMetricData;
return new MetricDataSplitOperationResult(Collections.emptyList(), currentBatch);
} else {
// Remaining capacity can't hold all points, partition existing metric data object
List<PointData> originalPointsList = new ArrayList<>(metricData.getData().getPoints());
Collection<Collection<MetricData>> preparedBatches = new ArrayList<>();
int currentIndex = 0;

while (currentIndex < totalPointsInMetricData) {
int pointsToTake =
Math.min(totalPointsInMetricData - currentIndex, remainingCapacityInCurrentBatch);

if (pointsToTake > 0) {
currentBatch.metrics.add(
copyMetricData(metricData, originalPointsList, currentIndex, pointsToTake));
currentBatch.points += pointsToTake;
currentIndex += pointsToTake;
remainingCapacityInCurrentBatch -= pointsToTake;
}

if (remainingCapacityInCurrentBatch == 0) {
preparedBatches.add(currentBatch.metrics);
currentBatch = new BatchState(new ArrayList<>(maxExportBatchSize), 0);
remainingCapacityInCurrentBatch = maxExportBatchSize;
}
}
return new MetricDataSplitOperationResult(preparedBatches, currentBatch);
}
}

private static MetricData copyMetricData(
MetricData original,
List<PointData> originalPointsList,
int dataPointsOffset,
int dataPointsToTake) {
List<PointData> points =
Collections.unmodifiableList(
new ArrayList<>(
originalPointsList.subList(dataPointsOffset, dataPointsOffset + dataPointsToTake)));
return createMetricDataWithPoints(original, points);
}

/**
* Creates a new MetricData with the given points.
*
* @param original The original MetricData.
* @param points The points to use for the new MetricData.
* @return A new MetricData with the given points.
*/
@SuppressWarnings("unchecked")
private static MetricData createMetricDataWithPoints(
MetricData original, Collection<PointData> points) {
switch (original.getType()) {
case DOUBLE_GAUGE:
return ImmutableMetricData.createDoubleGauge(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableGaugeData.create((Collection<DoublePointData>) (Collection<?>) points));
case LONG_GAUGE:
return ImmutableMetricData.createLongGauge(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableGaugeData.create((Collection<LongPointData>) (Collection<?>) points));
case DOUBLE_SUM:
SumData<DoublePointData> doubleSumData = original.getDoubleSumData();
return ImmutableMetricData.createDoubleSum(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableSumData.create(
doubleSumData.isMonotonic(),
doubleSumData.getAggregationTemporality(),
(Collection<DoublePointData>) (Collection<?>) points));
case LONG_SUM:
SumData<LongPointData> longSumData = original.getLongSumData();
return ImmutableMetricData.createLongSum(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableSumData.create(
longSumData.isMonotonic(),
longSumData.getAggregationTemporality(),
(Collection<LongPointData>) (Collection<?>) points));
case HISTOGRAM:
HistogramData histogramData = original.getHistogramData();
return ImmutableMetricData.createDoubleHistogram(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableHistogramData.create(
histogramData.getAggregationTemporality(),
(Collection<HistogramPointData>) (Collection<?>) points));
case EXPONENTIAL_HISTOGRAM:
ExponentialHistogramData expHistogramData = original.getExponentialHistogramData();
return ImmutableMetricData.createExponentialHistogram(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableExponentialHistogramData.create(
expHistogramData.getAggregationTemporality(),
(Collection<ExponentialHistogramPointData>) (Collection<?>) points));
case SUMMARY:
return ImmutableMetricData.createDoubleSummary(
original.getResource(),
original.getInstrumentationScopeInfo(),
original.getName(),
original.getDescription(),
original.getUnit(),
ImmutableSummaryData.create((Collection<SummaryPointData>) (Collection<?>) points));
}
throw new UnsupportedOperationException("Unsupported metric type: " + original.getType());
}

/**
* A data class to store the result of a split operation performed on a single {@link MetricData}
* object.
*/
private static class MetricDataSplitOperationResult {
private final Collection<Collection<MetricData>> preparedBatches;
private final BatchState lastInProgressBatch;

/**
* Creates a new MetricDataSplitOperationResult.
*
* @param preparedBatches The collection of prepared batches of metric data for export. Each
* batch of {@link MetricData} objects is guaranteed to have at most {@link
* #maxExportBatchSize} points.
* @param lastInProgressBatch The last batch that is still in progress. This batch may have less
* than {@link #maxExportBatchSize} points.
*/
MetricDataSplitOperationResult(
Collection<Collection<MetricData>> preparedBatches,
BatchState lastInProgressBatch) {
this.preparedBatches = preparedBatches;
this.lastInProgressBatch = lastInProgressBatch;
}

Collection<Collection<MetricData>> getPreparedBatches() {
return preparedBatches;
}

BatchState getLastInProgressBatch() {
return lastInProgressBatch;
}
}

/**
* Tracks the active batch while batching stays linear: {@code metrics} is the current export
* payload being assembled and {@code points} is its running point count, so callers do not need
* to rescan the batch on every append.
*/
private static final class BatchState {
private final Collection<MetricData> metrics;
private int points;

/**
* Creates the mutable state for the current in-progress batch.
*
* @param metrics metric entries collected into the current export batch
* @param points running total of data points across {@code metrics}
*/
private BatchState(Collection<MetricData> metrics, int points) {
this.metrics = metrics;
this.points = points;
}
}
}
Loading
Loading