Skip to content

Commit c61f1fe

Browse files
Add closing rollover behaviour for Step Histogram flavours
1 parent d01a5a1 commit c61f1fe

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpHistogramSupport.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18+
import io.micrometer.common.lang.Nullable;
1819
import io.micrometer.registry.otlp.internal.ExponentialHistogramSnapShot;
1920

2021
interface OtlpHistogramSupport {
2122

23+
@Nullable
2224
ExponentialHistogramSnapShot getExponentialHistogramSnapShot();
2325

2426
}

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpStepDistributionSummary.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18-
import io.micrometer.common.lang.Nullable;
1918
import io.micrometer.core.instrument.AbstractDistributionSummary;
2019
import io.micrometer.core.instrument.Clock;
2120
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
@@ -78,7 +77,6 @@ public double max() {
7877
}
7978

8079
@Override
81-
@Nullable
8280
public ExponentialHistogramSnapShot getExponentialHistogramSnapShot() {
8381
if (histogramFlavour == HistogramFlavour.BASE2_EXPONENTIAL_BUCKET_HISTOGRAM) {
8482
return ((Base2ExponentialHistogram) histogram).getLatestExponentialHistogramSnapshot();
@@ -98,6 +96,9 @@ void _closingRollover() {
9896
if (histogram instanceof OtlpStepBucketHistogram) { // can be noop
9997
((OtlpStepBucketHistogram) histogram)._closingRollover();
10098
}
99+
else if (histogram instanceof Base2ExponentialHistogram) {
100+
histogram.close();
101+
}
101102
}
102103

103104
}

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpStepTimer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18-
import io.micrometer.common.lang.Nullable;
1918
import io.micrometer.core.instrument.AbstractTimer;
2019
import io.micrometer.core.instrument.Clock;
2120
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
@@ -94,10 +93,12 @@ void _closingRollover() {
9493
if (histogram instanceof OtlpStepBucketHistogram) { // can be noop
9594
((OtlpStepBucketHistogram) histogram)._closingRollover();
9695
}
96+
else if (histogram instanceof Base2ExponentialHistogram) {
97+
histogram.close();
98+
}
9799
}
98100

99101
@Override
100-
@Nullable
101102
public ExponentialHistogramSnapShot getExponentialHistogramSnapShot() {
102103
if (histogramFlavour == HistogramFlavour.BASE2_EXPONENTIAL_BUCKET_HISTOGRAM) {
103104
return ((Base2ExponentialHistogram) histogram).getLatestExponentialHistogramSnapshot();

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/internal/Base2ExponentialHistogram.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ int getScale() {
104104
* Provides a bridge to Micrometer {@link HistogramSnapshot}.
105105
*/
106106
@Override
107-
public synchronized HistogramSnapshot takeSnapshot(final long count, final double total, final double max) {
107+
public HistogramSnapshot takeSnapshot(final long count, final double total, final double max) {
108108
this.takeExponentialHistogramSnapShot();
109109
return new HistogramSnapshot(count, total, max, null, null, null);
110110
}
111111

112112
/**
113-
* Returns the snapshot of current recorded values..
113+
* Returns the snapshot of current recorded values.
114114
*/
115115
ExponentialHistogramSnapShot getCurrentValuesSnapshot() {
116116
return (circularCountHolder.isEmpty() && zeroCount.longValue() == 0)
@@ -149,9 +149,12 @@ public void recordDouble(double value) {
149149

150150
int index = base2IndexProvider.getIndexForValue(value);
151151
if (!circularCountHolder.increment(index, 1)) {
152-
downScale(getDownScaleFactor(index));
153-
index = base2IndexProvider.getIndexForValue(value);
154-
circularCountHolder.increment(index, 1);
152+
synchronized (this) {
153+
int downScaleFactor = getDownScaleFactor(index);
154+
downScale(downScaleFactor);
155+
index = base2IndexProvider.getIndexForValue(value);
156+
circularCountHolder.increment(index, 1);
157+
}
155158
}
156159
}
157160

@@ -160,7 +163,7 @@ public void recordDouble(double value) {
160163
* align with the exponential scale.
161164
* @param downScaleFactor - the factor to downscale this histogram.
162165
*/
163-
private synchronized void downScale(int downScaleFactor) {
166+
private void downScale(int downScaleFactor) {
164167
if (downScaleFactor == 0) {
165168
return;
166169
}
@@ -195,7 +198,7 @@ private void updateScale(int newScale) {
195198
* @return a factor by which {@link Base2ExponentialHistogram#scale} should be
196199
* decreased.
197200
*/
198-
private synchronized int getDownScaleFactor(final long index) {
201+
private int getDownScaleFactor(final long index) {
199202
long newStart = Math.min(index, circularCountHolder.getStartIndex());
200203
long newEnd = Math.max(index, circularCountHolder.getEndIndex());
201204

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/internal/DefaultExponentialHistogramSnapShot.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616
package io.micrometer.registry.otlp.internal;
1717

1818
import java.util.Collections;
19-
import java.util.HashMap;
19+
import java.util.LinkedHashMap;
2020
import java.util.List;
2121
import java.util.Map;
2222

2323
class DefaultExponentialHistogramSnapShot implements ExponentialHistogramSnapShot {
2424

25-
private static final Map<Integer, ExponentialHistogramSnapShot> emptySnapshotCache = new HashMap<>();
25+
private static final int MAX_ENTRIES = 50;
26+
27+
private static final Map<Integer, ExponentialHistogramSnapShot> emptySnapshotCache = new LinkedHashMap<Integer, ExponentialHistogramSnapShot>() {
28+
@Override
29+
protected boolean removeEldestEntry(final Map.Entry eldest) {
30+
return size() > MAX_ENTRIES;
31+
}
32+
};
2633

2734
private final int scale;
2835

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/internal/DeltaBase2ExponentialHistogram.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public class DeltaBase2ExponentialHistogram extends Base2ExponentialHistogram {
3333

34-
private final StepValue<ExponentialHistogramSnapShot> stepExponentialHistogramSnapShot;
34+
private final StepExponentialHistogramSnapShot stepExponentialHistogramSnapShot;
3535

3636
/**
3737
* Creates an Base2ExponentialHistogram that record positive values and resets for
@@ -66,14 +66,19 @@ synchronized void takeExponentialHistogramSnapShot() {
6666
stepExponentialHistogramSnapShot.poll();
6767
}
6868

69+
@Override
70+
public void close() {
71+
stepExponentialHistogramSnapShot._closingRollover();
72+
}
73+
6974
private class StepExponentialHistogramSnapShot extends StepValue<ExponentialHistogramSnapShot> {
7075

7176
public StepExponentialHistogramSnapShot(final Clock clock, final long stepMillis, final int maxScale) {
7277
super(clock, stepMillis, DefaultExponentialHistogramSnapShot.getEmptySnapshotForScale(maxScale));
7378
}
7479

7580
@Override
76-
protected Supplier<ExponentialHistogramSnapShot> valueSupplier() {
81+
protected synchronized Supplier<ExponentialHistogramSnapShot> valueSupplier() {
7782
return () -> {
7883
ExponentialHistogramSnapShot latestSnapShot = getCurrentValuesSnapshot();
7984
reset();
@@ -86,6 +91,11 @@ protected ExponentialHistogramSnapShot noValue() {
8691
return DefaultExponentialHistogramSnapShot.getEmptySnapshotForScale(getScale());
8792
}
8893

94+
@Override
95+
protected void _closingRollover() {
96+
super._closingRollover();
97+
}
98+
8999
}
90100

91101
}

0 commit comments

Comments
 (0)