-
Notifications
You must be signed in to change notification settings - Fork 969
Add support for batching in PeriodicMetricReader #8296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
psx95
wants to merge
55
commits into
open-telemetry:main
Choose a base branch
from
psx95:issue-8245
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
91b087c
WIP: Add metrics batcher in the SDK
psx95 9534dea
Allow exporting metricData batches
psx95 70d4e15
Make existing tests compatible with changes
psx95 6b97875
Add docs and update existing tests
psx95 b2b78c3
Fix MetricExportBatcher logic and add tests
psx95 40226ae
Add support for EXPONENTIAL_HISTOGRAM and SUMMARY data types
psx95 af9fad2
Fix metrics checkstyle issue
psx95 3c3bc1a
Add missing generated diff files
psx95 7b3feb8
Update unit tests for enhanced coverage
psx95 0083b54
Add unit tests for PeriodicMetricReader
psx95 1b3ad77
Fix checkstyle issues
psx95 5e18a7c
Clean up inline code comments
psx95 a5b0120
Fix bug for miscalculating remaining capacity of a batch
psx95 1751b57
Add missing Javadoc for public facing API
psx95 6ea146e
Refactor logic in prepareExportBatches to remove redundancy
psx95 c5356f6
Address comment about defensive copy for original point sublist
psx95 c02c3c8
Prevent copying MetricData for 0 points
psx95 1e8c645
Add test case to verify there are no batches with empty metric points
psx95 48575c2
Switch to sequential export
psx95 e90ab8d
Add tests to verify sequential export for PeriodicMetricReader
psx95 6538745
Fix batched forceFlush failure propagation
zeitlinger 1b7874d
Make metric export batching linear
zeitlinger 741a8a4
Restore forceFlush partial-success behavior
zeitlinger af2be1e
WIP: Add metrics batcher in the SDK
psx95 21e56ea
Allow exporting metricData batches
psx95 2be7726
Make existing tests compatible with changes
psx95 f4ff90d
Add docs and update existing tests
psx95 b243d64
Fix MetricExportBatcher logic and add tests
psx95 7e9fe9e
Add support for EXPONENTIAL_HISTOGRAM and SUMMARY data types
psx95 3106e91
Fix metrics checkstyle issue
psx95 8491ce5
Add missing generated diff files
psx95 6027e52
Update unit tests for enhanced coverage
psx95 dc322fd
Add unit tests for PeriodicMetricReader
psx95 8027bdd
Fix checkstyle issues
psx95 549c08f
Clean up inline code comments
psx95 1da7d24
Fix bug for miscalculating remaining capacity of a batch
psx95 628a35f
Add missing Javadoc for public facing API
psx95 d080d1d
Refactor logic in prepareExportBatches to remove redundancy
psx95 51c5297
Address comment about defensive copy for original point sublist
psx95 fc08f5b
Prevent copying MetricData for 0 points
psx95 d393c96
Add test case to verify there are no batches with empty metric points
psx95 56c9385
Switch to sequential export
psx95 d87f785
Add tests to verify sequential export for PeriodicMetricReader
psx95 55c10c7
Resolve lane A review follow-ups
zeitlinger 19546a6
Merge remote-tracking branch 'psx95/issue-8245' into lane-a-pr-8296
zeitlinger 2628fe4
Merge remote-tracking branch 'gregor/gregor/8296-forceflush-failure-p…
zeitlinger 8aff51d
Restore forceFlush doRun failure handling
zeitlinger 3bfb526
Merge pull request #5 from zeitlinger/gregor/8296-forceflush-failure-…
psx95 4c2b9bd
Make setMaxExportBatchSize package private, use SdkMeterProviderUtil
psx95 2e9efb7
Use LogCapturer extension for log assetion in tests
psx95 b3e4db7
Simplify the batchMetrics function
psx95 f4e996f
Update the apidiffs
psx95 79adac4
Extract batch export logic to helper function
psx95 e8d041d
Merge branch 'main' into issue-8245
psx95 b6edfb7
Make batchMetrics static
psx95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/export/MetricExportBatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.metrics.export; | ||
|
|
||
| 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 MetricExportBatcher() {} | ||
|
|
||
| private static void validateMaxExportBatchSize(int maxExportBatchSize) { | ||
| if (maxExportBatchSize <= 0) { | ||
| throw new IllegalArgumentException("maxExportBatchSize must be positive"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
| static Collection<Collection<MetricData>> batchMetrics( | ||
| Collection<MetricData> metrics, int maxExportBatchSize) { | ||
| validateMaxExportBatchSize(maxExportBatchSize); | ||
| if (metrics.isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
| Collection<Collection<MetricData>> preparedBatchesForExport = new ArrayList<>(); | ||
| List<MetricData> currentBatch = new ArrayList<>(maxExportBatchSize); | ||
| int currentPointsInBatch = 0; | ||
| for (MetricData metricData : metrics) { | ||
| int totalPointsInMetric = metricData.getData().getPoints().size(); | ||
| if (currentPointsInBatch + totalPointsInMetric <= maxExportBatchSize) { | ||
| currentBatch.add(metricData); | ||
| currentPointsInBatch += totalPointsInMetric; | ||
| continue; | ||
| } | ||
| int currentIndex = 0; | ||
| List<PointData> originalPointsList = new ArrayList<>(metricData.getData().getPoints()); | ||
| while (currentIndex < totalPointsInMetric) { | ||
| if (currentPointsInBatch == maxExportBatchSize) { | ||
| preparedBatchesForExport.add(currentBatch); | ||
| currentBatch = new ArrayList<>(maxExportBatchSize); | ||
| currentPointsInBatch = 0; | ||
| } | ||
| int pointsToTake = | ||
| Math.min(maxExportBatchSize - currentPointsInBatch, totalPointsInMetric - currentIndex); | ||
| currentBatch.add( | ||
| copyMetricData(metricData, originalPointsList, currentIndex, pointsToTake)); | ||
| currentPointsInBatch += pointsToTake; | ||
| currentIndex += pointsToTake; | ||
| } | ||
| } | ||
| if (!currentBatch.isEmpty()) { | ||
| preparedBatchesForExport.add(currentBatch); | ||
| } | ||
| return Collections.unmodifiableCollection(preparedBatchesForExport); | ||
| } | ||
|
|
||
| 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()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.