Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 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
af2be1e
WIP: Add metrics batcher in the SDK
psx95 Apr 13, 2026
21e56ea
Allow exporting metricData batches
psx95 Apr 14, 2026
2be7726
Make existing tests compatible with changes
psx95 Apr 14, 2026
f4ff90d
Add docs and update existing tests
psx95 Apr 14, 2026
b243d64
Fix MetricExportBatcher logic and add tests
psx95 Apr 15, 2026
7e9fe9e
Add support for EXPONENTIAL_HISTOGRAM and SUMMARY data types
psx95 Apr 15, 2026
3106e91
Fix metrics checkstyle issue
psx95 Apr 15, 2026
8491ce5
Add missing generated diff files
psx95 Apr 15, 2026
6027e52
Update unit tests for enhanced coverage
psx95 Apr 15, 2026
dc322fd
Add unit tests for PeriodicMetricReader
psx95 Apr 15, 2026
8027bdd
Fix checkstyle issues
psx95 Apr 15, 2026
549c08f
Clean up inline code comments
psx95 Apr 15, 2026
1da7d24
Fix bug for miscalculating remaining capacity of a batch
psx95 Apr 15, 2026
628a35f
Add missing Javadoc for public facing API
psx95 Apr 15, 2026
d080d1d
Refactor logic in prepareExportBatches to remove redundancy
psx95 Apr 15, 2026
51c5297
Address comment about defensive copy for original point sublist
psx95 Apr 16, 2026
fc08f5b
Prevent copying MetricData for 0 points
psx95 Apr 16, 2026
d393c96
Add test case to verify there are no batches with empty metric points
psx95 Apr 16, 2026
56c9385
Switch to sequential export
psx95 Apr 19, 2026
d87f785
Add tests to verify sequential export for PeriodicMetricReader
psx95 Apr 19, 2026
55c10c7
Resolve lane A review follow-ups
zeitlinger Apr 27, 2026
19546a6
Merge remote-tracking branch 'psx95/issue-8245' into lane-a-pr-8296
zeitlinger Apr 27, 2026
2628fe4
Merge remote-tracking branch 'gregor/gregor/8296-forceflush-failure-p…
zeitlinger Apr 27, 2026
8aff51d
Restore forceFlush doRun failure handling
zeitlinger Apr 27, 2026
3bfb526
Merge pull request #5 from zeitlinger/gregor/8296-forceflush-failure-…
psx95 Apr 27, 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,280 @@
/*
* 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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i expected prepareExportBatches to take the Collection so that all of the batching logic is contained within the batcher (and we don't have to pass + update currentBatch).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we are not technically updating the currentBatch in this loop - the contents within the currentBatch is updated by the prepareExportBatches.

My intention behind the prepareExportBatches was a function that splits a given MetricData into appropriate export batches. Since the batches need to be completely filled (except the last one), this function requires the currentBatch as well.

Maybe if the function was renamed to splitMetricData it would make more sense?

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;
}
Comment thread
psx95 marked this conversation as resolved.
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -51,6 +52,7 @@ public final class PeriodicMetricReader implements MetricReader {
private volatile CollectionRegistration collectionRegistration = CollectionRegistration.noop();

@Nullable private volatile ScheduledFuture<?> scheduledFuture;
@Nullable private final MetricExportBatcher metricsBatcher;

/**
* Returns a new {@link PeriodicMetricReader} which exports to the {@code exporter} once every
Expand All @@ -66,10 +68,14 @@ public static PeriodicMetricReaderBuilder builder(MetricExporter exporter) {
}

PeriodicMetricReader(
MetricExporter exporter, long intervalNanos, ScheduledExecutorService scheduler) {
MetricExporter exporter,
long intervalNanos,
ScheduledExecutorService scheduler,
@Nullable MetricExportBatcher metricsBatcher) {
this.exporter = exporter;
this.intervalNanos = intervalNanos;
this.scheduler = scheduler;
this.metricsBatcher = metricsBatcher;
this.scheduled = new Scheduled();
}

Expand Down Expand Up @@ -163,6 +169,8 @@ public String toString() {
+ exporter
+ ", intervalNanos="
+ intervalNanos
+ ", metricsBatcher="
+ metricsBatcher
+ '}';
}

Expand Down Expand Up @@ -217,7 +225,47 @@ CompletableResultCode doRun() {
exportAvailable.set(true);
flushResult.succeed();
} else {
CompletableResultCode result = exporter.export(metricData);
CompletableResultCode result;
if (metricsBatcher != null) {
Collection<Collection<MetricData>> batches = metricsBatcher.batchMetrics(metricData);
CompletableResultCode sequentialResult = new CompletableResultCode();
AtomicBoolean anyFailed = new AtomicBoolean(false);
Iterator<Collection<MetricData>> batchIterator = batches.iterator();

Runnable exportNext =
new Runnable() {
@Override
public void run() {
while (batchIterator.hasNext()) {
Collection<MetricData> currentBatch = batchIterator.next();
CompletableResultCode currentResult = exporter.export(currentBatch);
if (currentResult.isDone()) {
if (!currentResult.isSuccess()) {
anyFailed.set(true);
}
} else {
currentResult.whenComplete(
() -> {
if (!currentResult.isSuccess()) {
anyFailed.set(true);
}
this.run();
});
return;
}
}
if (anyFailed.get()) {
sequentialResult.fail();
} else {
sequentialResult.succeed();
}
}
};
exportNext.run();
result = sequentialResult;
} else {
Comment on lines +229 to +266
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MetricExporter#export documents that the caller (PeriodicMetricReader) will not call export again until the previous call completes. With batching enabled, this loop invokes exporter.export(batch) multiple times back-to-back without waiting for each returned CompletableResultCode to complete, which can lead to concurrent in-flight exports for exporters that are not re-entrant. Please change the batching export logic to export batches sequentially (start the next export only in the previous export's completion callback) and aggregate the final result from that chain.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

@psx95 psx95 Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When batching is enabled and the export is not synchronous, the control flow goes to the else block on line 247, registering a callback instead. The function then immediately returns, breaking out of the loop so that the consecutive batches are not exported until after the current batch is done exporting.

result = exporter.export(metricData);
}
result.whenComplete(
() -> {
if (!result.isSuccess()) {
Expand Down
Loading
Loading