Skip to content

Commit 7115187

Browse files
committed
reusable handles
1 parent 944b3ef commit 7115187

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public final class AsynchronousMetricStorage<T extends PointData, U extends Exem
7070

7171
// Only populated if memoryMode == REUSABLE_DATA
7272
private final ObjectPool<T> reusablePointsPool;
73+
private final ObjectPool<AggregatorHandle<T, U>> reusableHandlesPool;
74+
7375
private final List<T> reusablePointsList = new ArrayList<>();
7476
// If aggregationTemporality == DELTA, this reference and lastPoints will be swapped at every
7577
// collection
@@ -100,6 +102,7 @@ private AsynchronousMetricStorage(
100102
this.attributesProcessor = attributesProcessor;
101103
this.maxCardinality = maxCardinality - 1;
102104
this.reusablePointsPool = new ObjectPool<>(aggregator::createReusablePoint);
105+
this.reusableHandlesPool = new ObjectPool<>(aggregator::createHandle);
103106
this.handlesDeleter = new HandlesDeleter();
104107

105108
if (memoryMode == REUSABLE_DATA) {
@@ -141,15 +144,15 @@ AsynchronousMetricStorage<T, U> create(
141144
void record(Attributes attributes, long value) {
142145
attributes = validateAndProcessAttributes(attributes);
143146
AggregatorHandle<T, U> handle =
144-
aggregatorHandles.computeIfAbsent(attributes, key -> aggregator.createHandle());
147+
aggregatorHandles.computeIfAbsent(attributes, key -> reusableHandlesPool.borrowObject());
145148
handle.recordLong(value, attributes, Context.current());
146149
}
147150

148151
/** Record callback measurement from {@link ObservableDoubleMeasurement}. */
149152
void record(Attributes attributes, double value) {
150153
attributes = validateAndProcessAttributes(attributes);
151154
AggregatorHandle<T, U> handle =
152-
aggregatorHandles.computeIfAbsent(attributes, key -> aggregator.createHandle());
155+
aggregatorHandles.computeIfAbsent(attributes, key -> reusableHandlesPool.borrowObject());
153156
handle.recordDouble(value, attributes, Context.current());
154157
}
155158

@@ -336,7 +339,8 @@ private class HandlesDeleter implements BiConsumer<Attributes, AggregatorHandle<
336339
@Override
337340
public void accept(Attributes attributes, AggregatorHandle<T, U> handle) {
338341
if (active && !handle.hasRecordedValues()) {
339-
aggregatorHandles.remove(attributes);
342+
AggregatorHandle<T, U> aggregatorHandle = aggregatorHandles.remove(attributes);
343+
reusableHandlesPool.returnObject(aggregatorHandle);
340344
active = false;
341345
}
342346
}

0 commit comments

Comments
 (0)