forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoubleExponentialHistogramAggregator.java
234 lines (211 loc) · 8.27 KB
/
DoubleExponentialHistogramAggregator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.metrics.internal.aggregator;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoubleExemplarData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.exponentialhistogram.ExponentialHistogramData;
import io.opentelemetry.sdk.metrics.internal.descriptor.MetricDescriptor;
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarReservoir;
import io.opentelemetry.sdk.metrics.internal.state.ExponentialCounterFactory;
import io.opentelemetry.sdk.resources.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
* Aggregator that generates exponential histograms.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class DoubleExponentialHistogramAggregator
implements Aggregator<ExponentialHistogramAccumulation, DoubleExemplarData> {
private final Supplier<ExemplarReservoir<DoubleExemplarData>> reservoirSupplier;
private final ExponentialBucketStrategy bucketStrategy;
/**
* Constructs an exponential histogram aggregator.
*
* @param reservoirSupplier Supplier of exemplar reservoirs per-stream.
*/
public DoubleExponentialHistogramAggregator(
Supplier<ExemplarReservoir<DoubleExemplarData>> reservoirSupplier, int maxBuckets) {
this(
reservoirSupplier,
ExponentialBucketStrategy.newStrategy(
maxBuckets, ExponentialCounterFactory.circularBufferCounter()));
}
DoubleExponentialHistogramAggregator(
Supplier<ExemplarReservoir<DoubleExemplarData>> reservoirSupplier,
ExponentialBucketStrategy bucketStrategy) {
this.reservoirSupplier = reservoirSupplier;
this.bucketStrategy = bucketStrategy;
}
@Override
public AggregatorHandle<ExponentialHistogramAccumulation, DoubleExemplarData> createHandle() {
return new Handle(reservoirSupplier.get(), this.bucketStrategy);
}
/**
* Merge the exponential histogram accumulations. Mutates the {@link
* ExponentialHistogramAccumulation#getPositiveBuckets()} and {@link
* ExponentialHistogramAccumulation#getNegativeBuckets()} of {@code previous}. Mutating buckets is
* acceptable because copies are already made in {@link Handle#doAccumulateThenReset(List)}.
*/
@Override
public ExponentialHistogramAccumulation merge(
ExponentialHistogramAccumulation previous, ExponentialHistogramAccumulation current) {
// Create merged buckets
DoubleExponentialHistogramBuckets posBuckets =
merge(previous.getPositiveBuckets(), current.getPositiveBuckets());
DoubleExponentialHistogramBuckets negBuckets =
merge(previous.getNegativeBuckets(), current.getNegativeBuckets());
// resolve possible scale difference due to merge
int commonScale = Math.min(posBuckets.getScale(), negBuckets.getScale());
posBuckets.downscale(posBuckets.getScale() - commonScale);
negBuckets.downscale(negBuckets.getScale() - commonScale);
double min = -1;
double max = -1;
if (previous.hasMinMax() && current.hasMinMax()) {
min = Math.min(previous.getMin(), current.getMin());
max = Math.max(previous.getMax(), current.getMax());
} else if (previous.hasMinMax()) {
min = previous.getMin();
max = previous.getMax();
} else if (current.hasMinMax()) {
min = current.getMin();
max = current.getMax();
}
return ExponentialHistogramAccumulation.create(
commonScale,
previous.getSum() + current.getSum(),
previous.hasMinMax() || current.hasMinMax(),
min,
max,
posBuckets,
negBuckets,
previous.getZeroCount() + current.getZeroCount(),
current.getExemplars());
}
/**
* Merge the exponential histogram buckets. If {@code a} is empty, return {@code b}. If {@code b}
* is empty, return {@code a}. Else merge {@code b} into {@code a}.
*/
private static DoubleExponentialHistogramBuckets merge(
DoubleExponentialHistogramBuckets a, DoubleExponentialHistogramBuckets b) {
if (b.getTotalCount() == 0) {
return a;
} else if (a.getTotalCount() == 0) {
return b;
}
a.mergeInto(b);
return a;
}
@Override
public MetricData toMetricData(
Resource resource,
InstrumentationScopeInfo instrumentationScopeInfo,
MetricDescriptor metricDescriptor,
Map<Attributes, ExponentialHistogramAccumulation> accumulationByLabels,
AggregationTemporality temporality,
long startEpochNanos,
long lastCollectionEpoch,
long epochNanos) {
return ImmutableMetricData.createExponentialHistogram(
resource,
instrumentationScopeInfo,
metricDescriptor.getName(),
metricDescriptor.getDescription(),
metricDescriptor.getSourceInstrument().getUnit(),
ExponentialHistogramData.create(
temporality,
MetricDataUtils.toExponentialHistogramPointList(
accumulationByLabels,
(temporality == AggregationTemporality.CUMULATIVE)
? startEpochNanos
: lastCollectionEpoch,
epochNanos)));
}
static final class Handle
extends AggregatorHandle<ExponentialHistogramAccumulation, DoubleExemplarData> {
private final DoubleExponentialHistogramBuckets positiveBuckets;
private final DoubleExponentialHistogramBuckets negativeBuckets;
private long zeroCount;
private double sum;
private double min;
private double max;
private long count;
Handle(
ExemplarReservoir<DoubleExemplarData> reservoir, ExponentialBucketStrategy bucketStrategy) {
super(reservoir);
this.sum = 0;
this.zeroCount = 0;
this.min = Double.MAX_VALUE;
this.max = -1;
this.count = 0;
this.positiveBuckets = bucketStrategy.newBuckets();
this.negativeBuckets = bucketStrategy.newBuckets();
}
@Override
protected synchronized ExponentialHistogramAccumulation doAccumulateThenReset(
List<DoubleExemplarData> exemplars) {
ExponentialHistogramAccumulation acc =
ExponentialHistogramAccumulation.create(
this.positiveBuckets.getScale(),
sum,
this.count > 0,
this.count > 0 ? this.min : -1,
this.count > 0 ? this.max : -1,
positiveBuckets.copy(),
negativeBuckets.copy(),
zeroCount,
exemplars);
this.sum = 0;
this.zeroCount = 0;
this.min = Double.MAX_VALUE;
this.max = -1;
this.count = 0;
this.positiveBuckets.clear();
this.negativeBuckets.clear();
return acc;
}
@Override
protected synchronized void doRecordDouble(double value) {
// ignore NaN and infinity
if (!Double.isFinite(value)) {
return;
}
sum += value;
this.min = Math.min(this.min, value);
this.max = Math.max(this.max, value);
count++;
int c = Double.compare(value, 0);
if (c == 0) {
zeroCount++;
return;
}
// Record; If recording fails, calculate scale reduction and scale down to fit new value.
// 2nd attempt at recording should work with new scale
DoubleExponentialHistogramBuckets buckets = (c > 0) ? positiveBuckets : negativeBuckets;
// TODO: We should experiment with downscale on demand during sync execution and only
// unifying scale factor between positive/negative at collection time (doAccumulate).
if (!buckets.record(value)) {
// getScaleReduction() used with downScale() will scale down as required to record value,
// fit inside max allowed buckets, and make sure index can be represented by int.
downScale(buckets.getScaleReduction(value));
buckets.record(value);
}
}
@Override
protected void doRecordLong(long value) {
doRecordDouble((double) value);
}
void downScale(int by) {
positiveBuckets.downscale(by);
negativeBuckets.downscale(by);
}
}
}