Skip to content

Commit 2a0ad44

Browse files
authored
Don't allow this to escape constructor (#7785)
1 parent 101dd16 commit 2a0ad44

File tree

9 files changed

+19
-72
lines changed

9 files changed

+19
-72
lines changed

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/AggregatorHandle.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public abstract class AggregatorHandle<T extends PointData> {
4141
private final boolean isDoubleType;
4242
private volatile boolean valuesRecorded = false;
4343

44-
protected AggregatorHandle(ExemplarReservoirFactory reservoirFactory) {
45-
this.isDoubleType = isDoubleType();
44+
protected AggregatorHandle(ExemplarReservoirFactory reservoirFactory, boolean isDoubleType) {
45+
this.isDoubleType = isDoubleType;
4646
if (isDoubleType) {
4747
this.doubleReservoirFactory = reservoirFactory.createDoubleExemplarReservoir();
4848
this.longReservoirFactory = null;
@@ -80,19 +80,6 @@ public final T aggregateThenMaybeReset(
8080
reset);
8181
}
8282

83-
/**
84-
* Indicates whether this {@link AggregatorHandle} supports double or long values.
85-
*
86-
* <p>If it supports doubles, it MUST implement {@link #doAggregateThenMaybeResetDoubles(long,
87-
* long, Attributes, List, boolean)} and {@link #doRecordDouble(double)}.
88-
*
89-
* <p>If it supports long, it MUST implement {@link #doAggregateThenMaybeResetLongs(long, long,
90-
* Attributes, List, boolean)} and {@link #doRecordLong(long)}.
91-
*
92-
* @return true if it supports doubles, false if it supports longs.
93-
*/
94-
protected abstract boolean isDoubleType();
95-
9683
/** Implementation of the {@link #aggregateThenMaybeReset(long, long, Attributes, boolean)} . */
9784
protected T doAggregateThenMaybeResetDoubles(
9885
long startEpochNanos,

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DoubleBase2ExponentialHistogramAggregator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static final class Handle extends AggregatorHandle<ExponentialHistogramPointData
100100
int maxBuckets,
101101
int maxScale,
102102
MemoryMode memoryMode) {
103-
super(reservoirFactory);
103+
super(reservoirFactory, /* isDoubleType= */ true);
104104
this.maxBuckets = maxBuckets;
105105
this.maxScale = maxScale;
106106
this.sum = 0;
@@ -260,11 +260,6 @@ protected synchronized void doRecordDouble(double value) {
260260
}
261261
}
262262

263-
@Override
264-
protected boolean isDoubleType() {
265-
return true;
266-
}
267-
268263
@Override
269264
public void recordLong(long value, Attributes attributes, Context context) {
270265
// Since there is no LongExplicitBucketHistogramAggregator and we need to support measurements

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DoubleExplicitBucketHistogramAggregator.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ static final class Handle extends AggregatorHandle<HistogramPointData> {
110110
private final long[] counts;
111111

112112
// Used only when MemoryMode = REUSABLE_DATA
113-
@Nullable private MutableHistogramPointData reusablePoint;
113+
@Nullable private final MutableHistogramPointData reusablePoint;
114114

115115
Handle(
116116
List<Double> boundaryList,
117117
double[] boundaries,
118118
ExemplarReservoirFactory reservoirFactory,
119119
MemoryMode memoryMode) {
120-
super(reservoirFactory);
120+
super(reservoirFactory, /* isDoubleType= */ true);
121121
this.boundaryList = boundaryList;
122122
this.boundaries = boundaries;
123123
this.counts = new long[this.boundaries.length + 1];
@@ -127,6 +127,8 @@ static final class Handle extends AggregatorHandle<HistogramPointData> {
127127
this.count = 0;
128128
if (memoryMode == MemoryMode.REUSABLE_DATA) {
129129
this.reusablePoint = new MutableHistogramPointData(counts.length);
130+
} else {
131+
this.reusablePoint = null;
130132
}
131133
}
132134

@@ -138,11 +140,6 @@ public void recordLong(long value, Attributes attributes, Context context) {
138140
super.recordDouble((double) value, attributes, context);
139141
}
140142

141-
@Override
142-
protected boolean isDoubleType() {
143-
return true;
144-
}
145-
146143
@Override
147144
protected HistogramPointData doAggregateThenMaybeResetDoubles(
148145
long startEpochNanos,

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DoubleLastValueAggregator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static final class Handle extends AggregatorHandle<DoublePointData> {
100100
@Nullable private final MutableDoublePointData reusablePoint;
101101

102102
private Handle(ExemplarReservoirFactory reservoirFactory, MemoryMode memoryMode) {
103-
super(reservoirFactory);
103+
super(reservoirFactory, /* isDoubleType= */ true);
104104
if (memoryMode == MemoryMode.REUSABLE_DATA) {
105105
reusablePoint = new MutableDoublePointData();
106106
} else {
@@ -127,11 +127,6 @@ protected DoublePointData doAggregateThenMaybeResetDoubles(
127127
}
128128
}
129129

130-
@Override
131-
protected boolean isDoubleType() {
132-
return true;
133-
}
134-
135130
@Override
136131
protected void doRecordDouble(double value) {
137132
valueBits.set(Double.doubleToLongBits(value));

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DoubleSumAggregator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,10 @@ static final class Handle extends AggregatorHandle<DoublePointData> {
113113
@Nullable private final MutableDoublePointData reusablePoint;
114114

115115
Handle(ExemplarReservoirFactory reservoirFactory, MemoryMode memoryMode) {
116-
super(reservoirFactory);
116+
super(reservoirFactory, /* isDoubleType= */ true);
117117
reusablePoint = memoryMode == MemoryMode.REUSABLE_DATA ? new MutableDoublePointData() : null;
118118
}
119119

120-
@Override
121-
protected boolean isDoubleType() {
122-
return true;
123-
}
124-
125120
@Override
126121
protected DoublePointData doAggregateThenMaybeResetDoubles(
127122
long startEpochNanos,

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DropAggregator.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ public List<? extends ExemplarData> getExemplars() {
5353
public static final Aggregator<PointData> INSTANCE = new DropAggregator();
5454

5555
private static final AggregatorHandle<PointData> HANDLE =
56-
new AggregatorHandle<PointData>(ExemplarReservoirFactory.noSamples()) {
57-
@Override
58-
protected boolean isDoubleType() {
59-
return true;
60-
}
61-
56+
new AggregatorHandle<PointData>(
57+
ExemplarReservoirFactory.noSamples(), /* isDoubleType= */ true) {
6258
@Override
6359
protected PointData doAggregateThenMaybeResetDoubles(
6460
long startEpochNanos,

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/LongLastValueAggregator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,14 @@ static final class Handle extends AggregatorHandle<LongPointData> {
9595
@Nullable private final MutableLongPointData reusablePoint;
9696

9797
Handle(ExemplarReservoirFactory reservoirFactory, MemoryMode memoryMode) {
98-
super(reservoirFactory);
98+
super(reservoirFactory, /* isDoubleType= */ false);
9999
if (memoryMode == MemoryMode.REUSABLE_DATA) {
100100
reusablePoint = new MutableLongPointData();
101101
} else {
102102
reusablePoint = null;
103103
}
104104
}
105105

106-
@Override
107-
protected boolean isDoubleType() {
108-
return false;
109-
}
110-
111106
@Override
112107
protected LongPointData doAggregateThenMaybeResetLongs(
113108
long startEpochNanos,

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/LongSumAggregator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static final class Handle extends AggregatorHandle<LongPointData> {
106106
@Nullable private final MutableLongPointData reusablePointData;
107107

108108
Handle(ExemplarReservoirFactory reservoirFactory, MemoryMode memoryMode) {
109-
super(reservoirFactory);
109+
super(reservoirFactory, /* isDoubleType= */ false);
110110
reusablePointData =
111111
memoryMode == MemoryMode.REUSABLE_DATA ? new MutableLongPointData() : null;
112112
}
@@ -128,11 +128,6 @@ protected LongPointData doAggregateThenMaybeResetLongs(
128128
}
129129
}
130130

131-
@Override
132-
protected boolean isDoubleType() {
133-
return false;
134-
}
135-
136131
@Override
137132
public void doRecordLong(long value) {
138133
current.add(value);

sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/internal/aggregator/AggregatorHandleTest.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,14 @@ void testGenerateExemplarsOnCollect() {
102102
private static class TestDoubleAggregatorHandle extends TestAggregatorHandle {
103103

104104
TestDoubleAggregatorHandle(DoubleExemplarReservoir reservoir) {
105-
super(reservoir, null);
106-
}
107-
108-
@Override
109-
protected boolean isDoubleType() {
110-
return true;
105+
super(reservoir, null, /* isDoubleType= */ true);
111106
}
112107
}
113108

114109
private static class TestLongAggregatorHandle extends TestAggregatorHandle {
115110

116111
TestLongAggregatorHandle(LongExemplarReservoir reservoir) {
117-
super(null, reservoir);
118-
}
119-
120-
@Override
121-
protected boolean isDoubleType() {
122-
return false;
112+
super(null, reservoir, /* isDoubleType= */ false);
123113
}
124114
}
125115

@@ -130,7 +120,8 @@ private abstract static class TestAggregatorHandle extends AggregatorHandle<Poin
130120

131121
TestAggregatorHandle(
132122
@Nullable DoubleExemplarReservoir doubleReservoir,
133-
@Nullable LongExemplarReservoir longReservoir) {
123+
@Nullable LongExemplarReservoir longReservoir,
124+
boolean isDoubleType) {
134125
super(
135126
new ExemplarReservoirFactory() {
136127
@Override
@@ -142,7 +133,8 @@ public DoubleExemplarReservoir createDoubleExemplarReservoir() {
142133
public LongExemplarReservoir createLongExemplarReservoir() {
143134
return Objects.requireNonNull(longReservoir);
144135
}
145-
});
136+
},
137+
isDoubleType);
146138
}
147139

148140
@Nullable

0 commit comments

Comments
 (0)