Skip to content

Commit 1b2d2e5

Browse files
committed
is sampler enabled
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 0506165 commit 1b2d2e5

File tree

8 files changed

+79
-80
lines changed

8 files changed

+79
-80
lines changed

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/exemplars/ExemplarSampler.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.function.LongSupplier;
1313
import javax.annotation.Nullable;
1414

15+
import static java.util.Objects.requireNonNull;
16+
1517
/**
1618
* The ExemplarSampler selects Spans as exemplars.
1719
*
@@ -55,7 +57,7 @@ public ExemplarSampler(ExemplarSamplerConfig config) {
5557
* io.prometheus.metrics.tracer.initializer.SpanContextSupplier#getSpanContext()
5658
* SpanContextSupplier.getSpanContext()} is called to find a span context.
5759
*/
58-
public ExemplarSampler(ExemplarSamplerConfig config, @Nullable SpanContext spanContext) {
60+
public ExemplarSampler(ExemplarSamplerConfig config, @Nullable SpanContext spanContext) {
5961
this.config = config;
6062
this.exemplars = new Exemplar[config.getNumberOfExemplars()];
6163
this.customExemplars = new Exemplar[exemplars.length];
@@ -116,10 +118,13 @@ public void observeWithExemplar(double value, Labels labels) {
116118
private long doObserve(double value) {
117119
if (exemplars.length == 1) {
118120
return doObserveSingleExemplar(value);
119-
} else if (config.getHistogramClassicUpperBounds() != null) {
120-
return doObserveWithUpperBounds(value);
121121
} else {
122-
return doObserveWithoutUpperBounds(value);
122+
double[] classicUpperBounds = config.getHistogramClassicUpperBounds();
123+
if (classicUpperBounds != null) {
124+
return doObserveWithUpperBounds(value, classicUpperBounds);
125+
} else {
126+
return doObserveWithoutUpperBounds(value);
127+
}
123128
}
124129
}
125130

@@ -143,11 +148,10 @@ private long doObserveSingleExemplar(double amount, Labels labels) {
143148
return 0;
144149
}
145150

146-
private long doObserveWithUpperBounds(double value) {
151+
private long doObserveWithUpperBounds(double value, double[] classicUpperBounds) {
147152
long now = System.currentTimeMillis();
148-
double[] upperBounds = config.getHistogramClassicUpperBounds();
149-
for (int i = 0; i < upperBounds.length; i++) {
150-
if (value <= upperBounds[i]) {
153+
for (int i = 0; i < classicUpperBounds.length; i++) {
154+
if (value <= classicUpperBounds[i]) {
151155
Exemplar previous = exemplars[i];
152156
if (previous == null
153157
|| now - previous.getTimestampMillis() > config.getMinRetentionPeriodMillis()) {
@@ -188,11 +192,11 @@ private long doObserveWithoutUpperBounds(double value) {
188192
if (nullIndex >= 0) {
189193
return updateExemplar(nullIndex, value, now);
190194
}
191-
if (now - smallest.getTimestampMillis() > config.getMinRetentionPeriodMillis()
195+
if (now - requireNonNull(smallest).getTimestampMillis() > config.getMinRetentionPeriodMillis()
192196
&& value < smallest.getValue()) {
193197
return updateExemplar(smallestIndex, value, now);
194198
}
195-
if (now - largest.getTimestampMillis() > config.getMinRetentionPeriodMillis()
199+
if (now - requireNonNull(largest).getTimestampMillis() > config.getMinRetentionPeriodMillis()
196200
&& value > largest.getValue()) {
197201
return updateExemplar(largestIndex, value, now);
198202
}
@@ -218,18 +222,21 @@ private long doObserveWithoutUpperBounds(double value) {
218222
private long doObserveWithExemplar(double amount, Labels labels) {
219223
if (customExemplars.length == 1) {
220224
return doObserveSingleExemplar(amount, labels);
221-
} else if (config.getHistogramClassicUpperBounds() != null) {
222-
return doObserveWithExemplarWithUpperBounds(amount, labels);
223225
} else {
224-
return doObserveWithExemplarWithoutUpperBounds(amount, labels);
226+
double[] classicUpperBounds = config.getHistogramClassicUpperBounds();
227+
if (classicUpperBounds != null) {
228+
return doObserveWithExemplarWithUpperBounds(amount, labels, classicUpperBounds);
229+
} else {
230+
return doObserveWithExemplarWithoutUpperBounds(amount, labels);
231+
}
225232
}
226233
}
227234

228-
private long doObserveWithExemplarWithUpperBounds(double value, Labels labels) {
235+
private long doObserveWithExemplarWithUpperBounds(
236+
double value, Labels labels, double[] classicUpperBounds) {
229237
long now = System.currentTimeMillis();
230-
double[] upperBounds = config.getHistogramClassicUpperBounds();
231-
for (int i = 0; i < upperBounds.length; i++) {
232-
if (value <= upperBounds[i]) {
238+
for (int i = 0; i < classicUpperBounds.length; i++) {
239+
if (value <= classicUpperBounds[i]) {
233240
Exemplar previous = customExemplars[i];
234241
if (previous == null
235242
|| now - previous.getTimestampMillis() > config.getMinRetentionPeriodMillis()) {
@@ -263,7 +270,8 @@ private long doObserveWithExemplarWithoutUpperBounds(double amount, Labels label
263270
}
264271
if (nullPos != -1) {
265272
return updateCustomExemplar(nullPos, amount, labels, now);
266-
} else if (now - oldest.getTimestampMillis() > config.getMinRetentionPeriodMillis()) {
273+
} else if (now - requireNonNull(oldest).getTimestampMillis()
274+
> config.getMinRetentionPeriodMillis()) {
267275
return updateCustomExemplar(oldestPos, amount, labels, now);
268276
} else {
269277
return 0;

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/exemplars/ExemplarSamplerConfig.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.prometheus.metrics.config.ExemplarsProperties;
44
import io.prometheus.metrics.config.PrometheusProperties;
5+
6+
import javax.annotation.Nullable;
57
import java.util.concurrent.TimeUnit;
68

79
public class ExemplarSamplerConfig {
@@ -18,11 +20,13 @@ public class ExemplarSamplerConfig {
1820
private final long minRetentionPeriodMillis;
1921
private final long maxRetentionPeriodMillis;
2022
private final long sampleIntervalMillis;
23+
24+
@Nullable
2125
private final double[] histogramClassicUpperBounds; // null unless it's a classic histogram
22-
private final int
23-
numberOfExemplars; // if histogramClassicUpperBounds != null, then numberOfExemplars ==
2426

25-
// histogramClassicUpperBounds.length
27+
// if histogramClassicUpperBounds != null,
28+
// then numberOfExemplars == histogramClassicUpperBounds.length
29+
private final int numberOfExemplars;
2630

2731
/**
2832
* Constructor for all metric types except classic histograms.
@@ -49,7 +53,9 @@ public ExemplarSamplerConfig(
4953
}
5054

5155
private ExemplarSamplerConfig(
52-
ExemplarsProperties properties, int numberOfExemplars, double[] histogramClassicUpperBounds) {
56+
ExemplarsProperties properties,
57+
int numberOfExemplars,
58+
@Nullable double[] histogramClassicUpperBounds) {
5359
this(
5460
TimeUnit.SECONDS.toMillis(
5561
getOrDefault(
@@ -68,7 +74,7 @@ private ExemplarSamplerConfig(
6874
long maxRetentionPeriodMillis,
6975
long sampleIntervalMillis,
7076
int numberOfExemplars,
71-
double[] histogramClassicUpperBounds) {
77+
@Nullable double[] histogramClassicUpperBounds) {
7278
this.minRetentionPeriodMillis = minRetentionPeriodMillis;
7379
this.maxRetentionPeriodMillis = maxRetentionPeriodMillis;
7480
this.sampleIntervalMillis = sampleIntervalMillis;
@@ -110,11 +116,11 @@ private void validate() {
110116
}
111117
}
112118

113-
private static <T> T getOrDefault(T result, T defaultValue) {
119+
private static <T> T getOrDefault(@Nullable T result, T defaultValue) {
114120
return result != null ? result : defaultValue;
115121
}
116122

117-
/** May be {@code null}. */
123+
@Nullable
118124
public double[] getHistogramClassicUpperBounds() {
119125
return histogramClassicUpperBounds;
120126
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Counter.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
99
import io.prometheus.metrics.model.snapshots.Exemplar;
1010
import io.prometheus.metrics.model.snapshots.Labels;
11+
12+
import javax.annotation.Nullable;
1113
import java.util.ArrayList;
1214
import java.util.Collections;
1315
import java.util.List;
@@ -32,7 +34,7 @@
3234
public class Counter extends StatefulMetric<CounterDataPoint, Counter.DataPoint>
3335
implements CounterDataPoint {
3436

35-
private final ExemplarSamplerConfig exemplarSamplerConfig;
37+
@Nullable private final ExemplarSamplerConfig exemplarSamplerConfig;
3638

3739
private Counter(Builder builder, PrometheusProperties prometheusProperties) {
3840
super(builder);
@@ -91,14 +93,9 @@ protected CounterSnapshot collect(List<Labels> labels, List<DataPoint> metricDat
9193
return new CounterSnapshot(getMetadata(), data);
9294
}
9395

94-
@Override
95-
protected boolean isExemplarsEnabled() {
96-
return exemplarSamplerConfig != null;
97-
}
98-
9996
@Override
10097
protected DataPoint newDataPoint() {
101-
if (isExemplarsEnabled()) {
98+
if (exemplarSamplerConfig != null) {
10299
return new DataPoint(new ExemplarSampler(exemplarSamplerConfig));
103100
} else {
104101
return new DataPoint(null);
@@ -120,9 +117,11 @@ static class DataPoint implements CounterDataPoint {
120117
// we will be using the LongAdder and get the best performance.
121118
private final LongAdder longValue = new LongAdder();
122119
private final long createdTimeMillis = System.currentTimeMillis();
120+
121+
@Nullable
123122
private final ExemplarSampler exemplarSampler; // null if isExemplarsEnabled() is false
124123

125-
private DataPoint(ExemplarSampler exemplarSampler) {
124+
private DataPoint(@Nullable ExemplarSampler exemplarSampler) {
126125
this.exemplarSampler = exemplarSampler;
127126
}
128127

@@ -139,39 +138,35 @@ public long getLongValue() {
139138
@Override
140139
public void inc(long amount) {
141140
validateAndAdd(amount);
142-
if (isExemplarsEnabled()) {
141+
if (exemplarSampler != null) {
143142
exemplarSampler.observe((double) amount);
144143
}
145144
}
146145

147146
@Override
148147
public void inc(double amount) {
149148
validateAndAdd(amount);
150-
if (isExemplarsEnabled()) {
149+
if (exemplarSampler != null) {
151150
exemplarSampler.observe(amount);
152151
}
153152
}
154153

155154
@Override
156155
public void incWithExemplar(long amount, Labels labels) {
157156
validateAndAdd(amount);
158-
if (isExemplarsEnabled()) {
157+
if (exemplarSampler != null) {
159158
exemplarSampler.observeWithExemplar((double) amount, labels);
160159
}
161160
}
162161

163162
@Override
164163
public void incWithExemplar(double amount, Labels labels) {
165164
validateAndAdd(amount);
166-
if (isExemplarsEnabled()) {
165+
if (exemplarSampler != null) {
167166
exemplarSampler.observeWithExemplar(amount, labels);
168167
}
169168
}
170169

171-
private boolean isExemplarsEnabled() {
172-
return exemplarSampler != null;
173-
}
174-
175170
private void validateAndAdd(long amount) {
176171
if (amount < 0) {
177172
throw new IllegalArgumentException(

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Gauge.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import io.prometheus.metrics.model.snapshots.Exemplar;
99
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
1010
import io.prometheus.metrics.model.snapshots.Labels;
11+
12+
import javax.annotation.Nullable;
1113
import java.util.ArrayList;
1214
import java.util.Collections;
1315
import java.util.List;
@@ -39,7 +41,7 @@
3941
public class Gauge extends StatefulMetric<GaugeDataPoint, Gauge.DataPoint>
4042
implements GaugeDataPoint {
4143

42-
private final ExemplarSamplerConfig exemplarSamplerConfig;
44+
@Nullable private final ExemplarSamplerConfig exemplarSamplerConfig;
4345

4446
private Gauge(Builder builder, PrometheusProperties prometheusProperties) {
4547
super(builder);
@@ -95,23 +97,19 @@ protected GaugeSnapshot collect(List<Labels> labels, List<DataPoint> metricData)
9597

9698
@Override
9799
protected DataPoint newDataPoint() {
98-
if (isExemplarsEnabled()) {
100+
if (exemplarSamplerConfig != null) {
99101
return new DataPoint(new ExemplarSampler(exemplarSamplerConfig));
100102
} else {
101103
return new DataPoint(null);
102104
}
103105
}
104106

105-
@Override
106-
protected boolean isExemplarsEnabled() {
107-
return exemplarSamplerConfig != null;
108-
}
109-
110107
static class DataPoint implements GaugeDataPoint {
111108

109+
@Nullable
112110
private final ExemplarSampler exemplarSampler; // null if isExemplarsEnabled() is false
113111

114-
private DataPoint(ExemplarSampler exemplarSampler) {
112+
private DataPoint(@Nullable ExemplarSampler exemplarSampler) {
115113
this.exemplarSampler = exemplarSampler;
116114
}
117115

@@ -121,7 +119,7 @@ private DataPoint(ExemplarSampler exemplarSampler) {
121119
public void inc(double amount) {
122120
long next =
123121
value.updateAndGet(l -> Double.doubleToRawLongBits(Double.longBitsToDouble(l) + amount));
124-
if (isExemplarsEnabled()) {
122+
if (exemplarSampler != null) {
125123
exemplarSampler.observe(Double.longBitsToDouble(next));
126124
}
127125
}
@@ -130,15 +128,15 @@ public void inc(double amount) {
130128
public void incWithExemplar(double amount, Labels labels) {
131129
long next =
132130
value.updateAndGet(l -> Double.doubleToRawLongBits(Double.longBitsToDouble(l) + amount));
133-
if (isExemplarsEnabled()) {
131+
if (exemplarSampler != null) {
134132
exemplarSampler.observeWithExemplar(Double.longBitsToDouble(next), labels);
135133
}
136134
}
137135

138136
@Override
139137
public void set(double value) {
140138
this.value.set(Double.doubleToRawLongBits(value));
141-
if (isExemplarsEnabled()) {
139+
if (exemplarSampler != null) {
142140
exemplarSampler.observe(value);
143141
}
144142
}
@@ -151,7 +149,7 @@ public double get() {
151149
@Override
152150
public void setWithExemplar(double value, Labels labels) {
153151
this.value.set(Double.doubleToRawLongBits(value));
154-
if (isExemplarsEnabled()) {
152+
if (exemplarSampler != null) {
155153
exemplarSampler.observeWithExemplar(value, labels);
156154
}
157155
}
@@ -162,7 +160,7 @@ private GaugeSnapshot.GaugeDataPointSnapshot collect(Labels labels) {
162160
// If there are multiple Exemplars (by default it's just one), use the oldest
163161
// so that we don't violate min age.
164162
Exemplar oldest = null;
165-
if (isExemplarsEnabled()) {
163+
if (exemplarSampler != null) {
166164
for (Exemplar exemplar : exemplarSampler.collect()) {
167165
if (oldest == null || exemplar.getTimestampMillis() < oldest.getTimestampMillis()) {
168166
oldest = exemplar;
@@ -172,9 +170,6 @@ private GaugeSnapshot.GaugeDataPointSnapshot collect(Labels labels) {
172170
return new GaugeSnapshot.GaugeDataPointSnapshot(get(), labels, oldest);
173171
}
174172

175-
private boolean isExemplarsEnabled() {
176-
return exemplarSampler != null;
177-
}
178173
}
179174

180175
public static Builder builder() {

0 commit comments

Comments
 (0)