Skip to content

Commit 9dcaf3b

Browse files
committed
dont delete all of them
1 parent 0dca607 commit 9dcaf3b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/AsynchronousMetricStorage.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.function.BiConsumer;
3738
import java.util.logging.Level;
3839
import java.util.logging.Logger;
3940

@@ -79,6 +80,20 @@ public final class AsynchronousMetricStorage<T extends PointData, U extends Exem
7980
private long startEpochNanos;
8081
private long epochNanos;
8182

83+
// Delete the first empty handle to reclaim space, and return quickly for all subsequent entries
84+
private final BiConsumer<Attributes, AggregatorHandle<T, U>> handlesDeleter =
85+
new BiConsumer<Attributes, AggregatorHandle<T, U>>() {
86+
private boolean active = true;
87+
88+
@Override
89+
public void accept(Attributes attributes, AggregatorHandle<T, U> handle) {
90+
if (active && !handle.hasRecordedValues()) {
91+
aggregatorHandles.remove(attributes);
92+
active = false;
93+
}
94+
}
95+
};
96+
8297
private AsynchronousMetricStorage(
8398
RegisteredReader registeredReader,
8499
MetricDescriptor metricDescriptor,
@@ -160,14 +175,9 @@ private Attributes validateAndProcessAttributes(Attributes attributes) {
160175
Context context = Context.current();
161176
attributes = attributesProcessor.process(attributes, context);
162177

163-
if (aggregatorHandles.size() >= maxCardinality) {
164-
aggregatorHandles.forEach(
165-
(attr, handle) -> {
166-
if (!handle.hasRecordedValues()) {
167-
aggregatorHandles.remove(attr);
168-
}
169-
});
170-
if (aggregatorHandles.size() >= maxCardinality) {
178+
if (aggregatorHandles.size() == maxCardinality) {
179+
aggregatorHandles.forEach(handlesDeleter);
180+
if (aggregatorHandles.size() == maxCardinality) {
171181
throttlingLogger.log(
172182
Level.WARNING,
173183
"Instrument "

0 commit comments

Comments
 (0)