-
Notifications
You must be signed in to change notification settings - Fork 858
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
Reuse MetricData #5178
Closed
Closed
Reuse MetricData #5178
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a47519c
Reuse MetricData
jack-berg 4c9013d
Fix test
jack-berg 42c1f8b
Fix test
jack-berg 2bd5295
spotless
jack-berg 9b79448
Iterate over map without allocations
jack-berg ff85519
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 4d3dd0d
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 5ead921
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 3db1e81
Fix merge
jack-berg 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 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 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 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 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 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 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
59 changes: 59 additions & 0 deletions
59
...n/java/io/opentelemetry/sdk/metrics/internal/data/MutableExponentialHistogramBuckets.java
This file contains 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,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics.internal.data; | ||
|
||
import io.opentelemetry.sdk.internal.PrimitiveLongList; | ||
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramBuckets; | ||
import io.opentelemetry.sdk.metrics.internal.aggregator.AdaptingCircularBufferCounter; | ||
import io.opentelemetry.sdk.metrics.internal.aggregator.DoubleBase2ExponentialHistogramBuckets; | ||
import java.util.List; | ||
|
||
public class MutableExponentialHistogramBuckets implements ExponentialHistogramBuckets { | ||
|
||
private int scale; | ||
private int offset; | ||
private final long[] bucketCounts; | ||
private final List<Long> bucketCountsList; | ||
private long totalCount; | ||
|
||
public MutableExponentialHistogramBuckets(int maxBuckets) { | ||
this.bucketCounts = new long[maxBuckets]; | ||
this.bucketCountsList = PrimitiveLongList.wrap(bucketCounts); | ||
} | ||
|
||
@Override | ||
public int getScale() { | ||
return scale; | ||
} | ||
|
||
@Override | ||
public int getOffset() { | ||
return offset; | ||
} | ||
|
||
@Override | ||
public List<Long> getBucketCounts() { | ||
return bucketCountsList; | ||
} | ||
|
||
@Override | ||
public long getTotalCount() { | ||
return totalCount; | ||
} | ||
|
||
/** Set the values. */ | ||
public void set(DoubleBase2ExponentialHistogramBuckets exponentialHistogramBuckets) { | ||
this.scale = exponentialHistogramBuckets.getScale(); | ||
this.offset = exponentialHistogramBuckets.getOffset(); | ||
AdaptingCircularBufferCounter counts = exponentialHistogramBuckets.getCounts(); | ||
for (int i = 0; i < bucketCounts.length; i++) { | ||
this.bucketCounts[i] = counts.get(i + counts.getIndexStart()); | ||
} | ||
PrimitiveLongList.setSize( | ||
this.bucketCountsList, counts.getIndexEnd() - counts.getIndexStart() + 1); | ||
this.totalCount = exponentialHistogramBuckets.getTotalCount(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...main/java/io/opentelemetry/sdk/metrics/internal/data/MutableExponentialHistogramData.java
This file contains 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,43 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics.internal.data; | ||
|
||
import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramData; | ||
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramPointData; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* Auto value implementation of {@link ExponentialHistogramData}. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public class MutableExponentialHistogramData implements ExponentialHistogramData { | ||
|
||
private AggregationTemporality temporality = AggregationTemporality.CUMULATIVE; | ||
private Collection<ExponentialHistogramPointData> pointData = Collections.emptyList(); | ||
|
||
public MutableExponentialHistogramData() {} | ||
|
||
@Override | ||
public AggregationTemporality getAggregationTemporality() { | ||
return temporality; | ||
} | ||
|
||
@Override | ||
public Collection<ExponentialHistogramPointData> getPoints() { | ||
return pointData; | ||
} | ||
|
||
/** Set the values. */ | ||
public void set( | ||
AggregationTemporality temporality, Collection<ExponentialHistogramPointData> pointData) { | ||
this.temporality = temporality; | ||
this.pointData = pointData; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was one idea I entertained for awhile performance gain. How are you avoiding multi-threads touching this data?
Is it because you're only returning this to ONE metric-reader at a time and the "hot path" of writes is still writing to the underlying data allocated in this handle?
If so, VERY clever. We should document this in the handle class how it works and why it's safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. While we support multiple readers, we don't support concurrent reads. As long as readers don't hold on to references to MetricData and try to read after they're done reading, they shouldn't get any weird behavior. Right now this won't work with multiple readers since once PeriodicMetricReader calls MetricProducer#collectAllMetrics(), another reader will be able to start reading and MetricData will be mutated out from under the PeriodicMetricReader. Ouch. But this is solvable by providing readers a way to communicate to MetricProducer that they're done consuming the data. For example, by adjusting
collectAllMetrics
to accept aCompletableResultCode
which the reader completes when finished consuming the data, i.e.MetricProducer#collectAllMetrics(CompleteableResultCode)
.As you noticed, this also relies on different objects for writes vs. reads (writes use AggregationHandle, reads use some some mutuable MetricData).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch that part about readers needing to communicate when they're finished consuming the data. Each reader has its own copies of metric storage, and the mutable MetricData, so its much simpler: It should be safe as long as a MetricReader doesn't hold on to the MetricData references and try to consume them during a subsequent collect.