Skip to content

Commit feb0297

Browse files
authored
Add Base2 prefix to internal exponential histogram classes (#5179)
1 parent 07e5654 commit feb0297

File tree

13 files changed

+99
-94
lines changed

13 files changed

+99
-94
lines changed

sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/metric/viewconfig/ViewConfigTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
2222
import io.opentelemetry.sdk.metrics.View;
2323
import io.opentelemetry.sdk.metrics.internal.view.AttributesProcessor;
24+
import io.opentelemetry.sdk.metrics.internal.view.Base2ExponentialHistogramAggregation;
2425
import io.opentelemetry.sdk.metrics.internal.view.ExplicitBucketHistogramAggregation;
25-
import io.opentelemetry.sdk.metrics.internal.view.ExponentialHistogramAggregation;
2626
import io.opentelemetry.sdk.metrics.internal.view.RegisteredView;
2727
import java.io.FileInputStream;
2828
import java.io.FileNotFoundException;
@@ -230,7 +230,7 @@ void toAggregation_GoodConfig() {
230230
assertThat(
231231
ViewConfig.toAggregation(
232232
"base2_exponential_bucket_histogram", ImmutableMap.of("max_buckets", 20)))
233-
.isInstanceOf(ExponentialHistogramAggregation.class)
233+
.isInstanceOf(Base2ExponentialHistogramAggregation.class)
234234
.extracting("maxBuckets", as(InstanceOfAssertFactories.INTEGER))
235235
.isEqualTo(20);
236236
}

sdk/metrics/src/jmh/java/io/opentelemetry/sdk/metrics/internal/aggregator/ExponentialHistogramIndexerBenchmark.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static class ThreadState {
3636

3737
private double[] values;
3838
private final AtomicLong valueIndex = new AtomicLong();
39-
private ExponentialHistogramIndexer indexer;
39+
private Base2ExponentialHistogramIndexer indexer;
4040

4141
@Setup(Level.Trial)
4242
public final void setup() {
@@ -46,7 +46,7 @@ public final void setup() {
4646
for (int i = 0; i < numValues; i++) {
4747
values[i] = random.nextDouble() * 1000;
4848
}
49-
indexer = ExponentialHistogramIndexer.get(scale);
49+
indexer = Base2ExponentialHistogramIndexer.get(scale);
5050
}
5151

5252
public void compute() {

sdk/metrics/src/jmh/java/io/opentelemetry/sdk/metrics/internal/aggregator/HistogramAggregationParam.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public enum HistogramAggregationParam {
2121
ExplicitBucketHistogramUtils.createBoundaryArray(Collections.emptyList()),
2222
ExemplarReservoir::doubleNoSamples)),
2323
EXPONENTIAL_SMALL_CIRCULAR_BUFFER(
24-
new DoubleExponentialHistogramAggregator(ExemplarReservoir::doubleNoSamples, 20, 0)),
24+
new DoubleBase2ExponentialHistogramAggregator(ExemplarReservoir::doubleNoSamples, 20, 0)),
2525
EXPONENTIAL_CIRCULAR_BUFFER(
26-
new DoubleExponentialHistogramAggregator(ExemplarReservoir::doubleNoSamples, 160, 0));
26+
new DoubleBase2ExponentialHistogramAggregator(ExemplarReservoir::doubleNoSamples, 160, 0));
2727

2828
private final Aggregator<?, ?> aggregator;
2929

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/Aggregation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
package io.opentelemetry.sdk.metrics;
77

88
import io.opentelemetry.sdk.metrics.data.MetricDataType;
9+
import io.opentelemetry.sdk.metrics.internal.view.Base2ExponentialHistogramAggregation;
910
import io.opentelemetry.sdk.metrics.internal.view.DefaultAggregation;
1011
import io.opentelemetry.sdk.metrics.internal.view.DropAggregation;
1112
import io.opentelemetry.sdk.metrics.internal.view.ExplicitBucketHistogramAggregation;
12-
import io.opentelemetry.sdk.metrics.internal.view.ExponentialHistogramAggregation;
1313
import io.opentelemetry.sdk.metrics.internal.view.LastValueAggregation;
1414
import io.opentelemetry.sdk.metrics.internal.view.SumAggregation;
1515
import java.util.List;
@@ -74,7 +74,7 @@ static Aggregation explicitBucketHistogram(List<Double> bucketBoundaries) {
7474
* default {@code maxBuckets} and {@code maxScale}.
7575
*/
7676
static Aggregation base2ExponentialBucketHistogram() {
77-
return ExponentialHistogramAggregation.getDefault();
77+
return Base2ExponentialHistogramAggregation.getDefault();
7878
}
7979

8080
/**
@@ -88,6 +88,6 @@ static Aggregation base2ExponentialBucketHistogram() {
8888
* performance of computing bucket index is improved when scale is {@code <= 0}.
8989
*/
9090
static Aggregation base2ExponentialBucketHistogram(int maxBuckets, int maxScale) {
91-
return ExponentialHistogramAggregation.create(maxBuckets, maxScale);
91+
return Base2ExponentialHistogramAggregation.create(maxBuckets, maxScale);
9292
}
9393
}
+6-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import java.util.Map;
99
import java.util.concurrent.ConcurrentHashMap;
1010

11-
final class ExponentialHistogramIndexer {
11+
final class Base2ExponentialHistogramIndexer {
1212

13-
private static final Map<Integer, ExponentialHistogramIndexer> cache = new ConcurrentHashMap<>();
13+
private static final Map<Integer, Base2ExponentialHistogramIndexer> cache =
14+
new ConcurrentHashMap<>();
1415

1516
/** Bit mask used to isolate exponent of IEEE 754 double precision number. */
1617
private static final long EXPONENT_BIT_MASK = 0x7FF0000000000000L;
@@ -35,14 +36,14 @@ final class ExponentialHistogramIndexer {
3536
private final int scale;
3637
private final double scaleFactor;
3738

38-
private ExponentialHistogramIndexer(int scale) {
39+
private Base2ExponentialHistogramIndexer(int scale) {
3940
this.scale = scale;
4041
this.scaleFactor = computeScaleFactor(scale);
4142
}
4243

4344
/** Get an indexer for the given scale. Indexers are cached and reused for performance. */
44-
static ExponentialHistogramIndexer get(int scale) {
45-
return cache.computeIfAbsent(scale, unused -> new ExponentialHistogramIndexer(scale));
45+
static Base2ExponentialHistogramIndexer get(int scale) {
46+
return cache.computeIfAbsent(scale, unused -> new Base2ExponentialHistogramIndexer(scale));
4647
}
4748

4849
/**
+10-10
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
import javax.annotation.Nullable;
2929

3030
/**
31-
* Aggregator that generates exponential histograms.
31+
* Aggregator that generates base2 exponential histograms.
3232
*
3333
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
3434
* at any time.
3535
*/
36-
public final class DoubleExponentialHistogramAggregator
36+
public final class DoubleBase2ExponentialHistogramAggregator
3737
implements Aggregator<ExponentialHistogramPointData, DoubleExemplarData> {
3838

3939
private final Supplier<ExemplarReservoir<DoubleExemplarData>> reservoirSupplier;
@@ -45,7 +45,7 @@ public final class DoubleExponentialHistogramAggregator
4545
*
4646
* @param reservoirSupplier Supplier of exemplar reservoirs per-stream.
4747
*/
48-
public DoubleExponentialHistogramAggregator(
48+
public DoubleBase2ExponentialHistogramAggregator(
4949
Supplier<ExemplarReservoir<DoubleExemplarData>> reservoirSupplier,
5050
int maxBuckets,
5151
int maxScale) {
@@ -78,8 +78,8 @@ public MetricData toMetricData(
7878
static final class Handle
7979
extends AggregatorHandle<ExponentialHistogramPointData, DoubleExemplarData> {
8080
private final int maxBuckets;
81-
@Nullable private DoubleExponentialHistogramBuckets positiveBuckets;
82-
@Nullable private DoubleExponentialHistogramBuckets negativeBuckets;
81+
@Nullable private DoubleBase2ExponentialHistogramBuckets positiveBuckets;
82+
@Nullable private DoubleBase2ExponentialHistogramBuckets negativeBuckets;
8383
private long zeroCount;
8484
private double sum;
8585
private double min;
@@ -129,7 +129,7 @@ protected synchronized ExponentialHistogramPointData doAggregateThenMaybeReset(
129129
}
130130

131131
private static ExponentialHistogramBuckets resolveBuckets(
132-
@Nullable DoubleExponentialHistogramBuckets buckets, int scale, boolean reset) {
132+
@Nullable DoubleBase2ExponentialHistogramBuckets buckets, int scale, boolean reset) {
133133
if (buckets == null) {
134134
return EmptyExponentialHistogramBuckets.get(scale);
135135
}
@@ -154,20 +154,20 @@ protected synchronized void doRecordDouble(double value) {
154154
count++;
155155

156156
int c = Double.compare(value, 0);
157-
DoubleExponentialHistogramBuckets buckets;
157+
DoubleBase2ExponentialHistogramBuckets buckets;
158158
if (c == 0) {
159159
zeroCount++;
160160
return;
161161
} else if (c > 0) {
162162
// Initialize positive buckets at current scale, if needed
163163
if (positiveBuckets == null) {
164-
positiveBuckets = new DoubleExponentialHistogramBuckets(scale, maxBuckets);
164+
positiveBuckets = new DoubleBase2ExponentialHistogramBuckets(scale, maxBuckets);
165165
}
166166
buckets = positiveBuckets;
167167
} else {
168168
// Initialize negative buckets at current scale, if needed
169169
if (negativeBuckets == null) {
170-
negativeBuckets = new DoubleExponentialHistogramBuckets(scale, maxBuckets);
170+
negativeBuckets = new DoubleBase2ExponentialHistogramBuckets(scale, maxBuckets);
171171
}
172172
buckets = negativeBuckets;
173173
}
@@ -214,7 +214,7 @@ static ExponentialHistogramBuckets get(int scale) {
214214
return ZERO_BUCKETS.computeIfAbsent(
215215
scale,
216216
scale1 ->
217-
new AutoValue_DoubleExponentialHistogramAggregator_EmptyExponentialHistogramBuckets(
217+
new AutoValue_DoubleBase2ExponentialHistogramAggregator_EmptyExponentialHistogramBuckets(
218218
scale1, 0, Collections.emptyList(), 0));
219219
}
220220
}
+16-16
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@
1212
import javax.annotation.Nullable;
1313

1414
/**
15-
* This class handles the operations for recording, scaling, and exposing data related to the
15+
* This class handles the operations for recording, scaling, and exposing data related to the base2
1616
* exponential histogram.
1717
*
1818
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
1919
* at any time.
2020
*/
21-
final class DoubleExponentialHistogramBuckets implements ExponentialHistogramBuckets {
21+
final class DoubleBase2ExponentialHistogramBuckets implements ExponentialHistogramBuckets {
2222

2323
private AdaptingCircularBufferCounter counts;
2424
private int scale;
25-
private ExponentialHistogramIndexer exponentialHistogramIndexer;
25+
private Base2ExponentialHistogramIndexer base2ExponentialHistogramIndexer;
2626
private long totalCount;
2727

28-
DoubleExponentialHistogramBuckets(int scale, int maxBuckets) {
28+
DoubleBase2ExponentialHistogramBuckets(int scale, int maxBuckets) {
2929
this.counts = new AdaptingCircularBufferCounter(maxBuckets);
3030
this.scale = scale;
31-
this.exponentialHistogramIndexer = ExponentialHistogramIndexer.get(this.scale);
31+
this.base2ExponentialHistogramIndexer = Base2ExponentialHistogramIndexer.get(this.scale);
3232
this.totalCount = 0;
3333
}
3434

3535
// For copying
36-
DoubleExponentialHistogramBuckets(DoubleExponentialHistogramBuckets buckets) {
36+
DoubleBase2ExponentialHistogramBuckets(DoubleBase2ExponentialHistogramBuckets buckets) {
3737
this.counts = new AdaptingCircularBufferCounter(buckets.counts);
3838
this.scale = buckets.scale;
39-
this.exponentialHistogramIndexer = buckets.exponentialHistogramIndexer;
39+
this.base2ExponentialHistogramIndexer = buckets.base2ExponentialHistogramIndexer;
4040
this.totalCount = buckets.totalCount;
4141
}
4242

4343
/** Returns a copy of this bucket. */
44-
DoubleExponentialHistogramBuckets copy() {
45-
return new DoubleExponentialHistogramBuckets(this);
44+
DoubleBase2ExponentialHistogramBuckets copy() {
45+
return new DoubleBase2ExponentialHistogramBuckets(this);
4646
}
4747

4848
/** Resets all counters in this bucket set to zero, but preserves scale. */
@@ -56,7 +56,7 @@ boolean record(double value) {
5656
// Guarded by caller. If passed 0 it would be a bug in the SDK.
5757
throw new IllegalStateException("Illegal attempted recording of zero at bucket level.");
5858
}
59-
int index = exponentialHistogramIndexer.computeIndex(value);
59+
int index = base2ExponentialHistogramIndexer.computeIndex(value);
6060
boolean recordingSuccessful = this.counts.increment(index, 1);
6161
if (recordingSuccessful) {
6262
totalCount++;
@@ -121,7 +121,7 @@ void downscale(int by) {
121121
}
122122

123123
this.scale = this.scale - by;
124-
this.exponentialHistogramIndexer = ExponentialHistogramIndexer.get(this.scale);
124+
this.base2ExponentialHistogramIndexer = Base2ExponentialHistogramIndexer.get(this.scale);
125125
}
126126

127127
/**
@@ -135,7 +135,7 @@ void downscale(int by) {
135135
*
136136
* @param other the histogram that will be merged into this one
137137
*/
138-
void mergeInto(DoubleExponentialHistogramBuckets other) {
138+
void mergeInto(DoubleBase2ExponentialHistogramBuckets other) {
139139
if (other.counts.isEmpty()) {
140140
return;
141141
}
@@ -190,7 +190,7 @@ public int getScale() {
190190
* @return The required scale reduction in order to fit the value in these buckets.
191191
*/
192192
int getScaleReduction(double value) {
193-
long index = exponentialHistogramIndexer.computeIndex(value);
193+
long index = base2ExponentialHistogramIndexer.computeIndex(value);
194194
long newStart = Math.min(index, counts.getIndexStart());
195195
long newEnd = Math.max(index, counts.getIndexEnd());
196196
return getScaleReduction(newStart, newEnd);
@@ -209,10 +209,10 @@ int getScaleReduction(long newStart, long newEnd) {
209209

210210
@Override
211211
public boolean equals(@Nullable Object obj) {
212-
if (!(obj instanceof DoubleExponentialHistogramBuckets)) {
212+
if (!(obj instanceof DoubleBase2ExponentialHistogramBuckets)) {
213213
return false;
214214
}
215-
DoubleExponentialHistogramBuckets other = (DoubleExponentialHistogramBuckets) obj;
215+
DoubleBase2ExponentialHistogramBuckets other = (DoubleBase2ExponentialHistogramBuckets) obj;
216216
// Don't need to compare getTotalCount() because equivalent bucket counts
217217
// imply equivalent overall count.
218218
// Additionally, we compare the "semantics" of bucket counts, that is
@@ -232,7 +232,7 @@ public boolean equals(@Nullable Object obj) {
232232
* <li>Offset does NOT need to be the same
233233
* </ul>
234234
*/
235-
private boolean sameBucketCounts(DoubleExponentialHistogramBuckets other) {
235+
private boolean sameBucketCounts(DoubleBase2ExponentialHistogramBuckets other) {
236236
if (this.totalCount != other.totalCount) {
237237
return false;
238238
}
+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import io.opentelemetry.sdk.metrics.data.PointData;
1616
import io.opentelemetry.sdk.metrics.internal.aggregator.Aggregator;
1717
import io.opentelemetry.sdk.metrics.internal.aggregator.AggregatorFactory;
18-
import io.opentelemetry.sdk.metrics.internal.aggregator.DoubleExponentialHistogramAggregator;
18+
import io.opentelemetry.sdk.metrics.internal.aggregator.DoubleBase2ExponentialHistogramAggregator;
1919
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
2020
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter;
2121
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarReservoir;
@@ -26,18 +26,18 @@
2626
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
2727
* at any time.
2828
*/
29-
public final class ExponentialHistogramAggregation implements Aggregation, AggregatorFactory {
29+
public final class Base2ExponentialHistogramAggregation implements Aggregation, AggregatorFactory {
3030

3131
private static final int DEFAULT_MAX_BUCKETS = 160;
3232
private static final int DEFAULT_MAX_SCALE = 20;
3333

3434
private static final Aggregation DEFAULT =
35-
new ExponentialHistogramAggregation(DEFAULT_MAX_BUCKETS, DEFAULT_MAX_SCALE);
35+
new Base2ExponentialHistogramAggregation(DEFAULT_MAX_BUCKETS, DEFAULT_MAX_SCALE);
3636

3737
private final int maxBuckets;
3838
private final int maxScale;
3939

40-
private ExponentialHistogramAggregation(int maxBuckets, int maxScale) {
40+
private Base2ExponentialHistogramAggregation(int maxBuckets, int maxScale) {
4141
this.maxBuckets = maxBuckets;
4242
this.maxScale = maxScale;
4343
}
@@ -60,15 +60,15 @@ public static Aggregation getDefault() {
6060
public static Aggregation create(int maxBuckets, int maxScale) {
6161
checkArgument(maxBuckets >= 1, "maxBuckets must be > 0");
6262
checkArgument(maxScale <= 20 && maxScale >= -10, "maxScale must be -10 <= x <= 20");
63-
return new ExponentialHistogramAggregation(maxBuckets, maxScale);
63+
return new Base2ExponentialHistogramAggregation(maxBuckets, maxScale);
6464
}
6565

6666
@Override
6767
@SuppressWarnings("unchecked")
6868
public <T extends PointData, U extends ExemplarData> Aggregator<T, U> createAggregator(
6969
InstrumentDescriptor instrumentDescriptor, ExemplarFilter exemplarFilter) {
7070
return (Aggregator<T, U>)
71-
new DoubleExponentialHistogramAggregator(
71+
new DoubleBase2ExponentialHistogramAggregator(
7272
() ->
7373
ExemplarReservoir.filtered(
7474
exemplarFilter,
@@ -93,7 +93,7 @@ public boolean isCompatibleWithInstrument(InstrumentDescriptor instrumentDescrip
9393

9494
@Override
9595
public String toString() {
96-
return "ExponentialHistogramAggregation{maxBuckets="
96+
return "Base2ExponentialHistogramAggregation{maxBuckets="
9797
+ maxBuckets
9898
+ ",maxScale="
9999
+ maxScale

sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AggregationTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ void haveToString() {
2929
.contains("ExplicitBucketHistogramAggregation");
3030
assertThat(Aggregation.base2ExponentialBucketHistogram())
3131
.asString()
32-
.isEqualTo("ExponentialHistogramAggregation{maxBuckets=160,maxScale=20}");
32+
.isEqualTo("Base2ExponentialHistogramAggregation{maxBuckets=160,maxScale=20}");
3333
assertThat(Aggregation.base2ExponentialBucketHistogram(1, 0))
3434
.asString()
35-
.isEqualTo("ExponentialHistogramAggregation{maxBuckets=1,maxScale=0}");
35+
.isEqualTo("Base2ExponentialHistogramAggregation{maxBuckets=1,maxScale=0}");
3636
}
3737

3838
@Test
3939
void histogramUsesExplicitBucket() {
40-
// Note: This will change when exponential histograms are launched.
4140
assertThat(Aggregation.explicitBucketHistogram())
4241
.asString()
4342
.contains("ExplicitBucketHistogram");

0 commit comments

Comments
 (0)