-
Notifications
You must be signed in to change notification settings - Fork 794
/
ExponentialHistogram.ts
612 lines (546 loc) · 16.8 KB
/
ExponentialHistogram.ts
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Accumulation,
AccumulationRecord,
Aggregator,
AggregatorKind,
ExponentialHistogram,
} from './types';
import {
DataPointType,
ExponentialHistogramMetricData,
MetricDescriptor,
} from '../export/MetricData';
import { diag, HrTime } from '@opentelemetry/api';
import { InstrumentType } from '../InstrumentDescriptor';
import { Maybe } from '../utils';
import { AggregationTemporality } from '../export/AggregationTemporality';
import { Buckets } from './exponential-histogram/Buckets';
import { getMapping } from './exponential-histogram/mapping/getMapping';
import { Mapping } from './exponential-histogram/mapping/types';
import { nextGreaterSquare } from './exponential-histogram/util';
/**
* Internal value type for ExponentialHistogramAggregation.
* Differs from the exported type as undefined sum/min/max complicate arithmetic
* performed by this aggregation, but are required to be undefined in the exported types.
*/
interface InternalHistogram extends ExponentialHistogram {
hasMinMax: boolean;
min: number;
max: number;
sum: number;
}
// HighLow is a utility class used for computing a common scale for
// two exponential histogram accumulations
class HighLow {
static combine(h1: HighLow, h2: HighLow): HighLow {
return new HighLow(Math.min(h1.low, h2.low), Math.max(h1.high, h2.high));
}
constructor(
public low: number,
public high: number
) {}
}
const MAX_SCALE = 20;
const DEFAULT_MAX_SIZE = 160;
const MIN_MAX_SIZE = 2;
export class ExponentialHistogramAccumulation implements Accumulation {
constructor(
public startTime: HrTime = startTime,
private _maxSize = DEFAULT_MAX_SIZE,
private _recordMinMax = true,
private _sum = 0,
private _count = 0,
private _zeroCount = 0,
private _min = Number.POSITIVE_INFINITY,
private _max = Number.NEGATIVE_INFINITY,
private _positive = new Buckets(),
private _negative = new Buckets(),
private _mapping: Mapping = getMapping(MAX_SCALE)
) {
if (this._maxSize < MIN_MAX_SIZE) {
diag.warn(`Exponential Histogram Max Size set to ${this._maxSize}, \
changing to the minimum size of: ${MIN_MAX_SIZE}`);
this._maxSize = MIN_MAX_SIZE;
}
}
/**
* record updates a histogram with a single count
* @param {Number} value
*/
record(value: number) {
this.updateByIncrement(value, 1);
}
/**
* Sets the start time for this accumulation
* @param {HrTime} startTime
*/
setStartTime(startTime: HrTime): void {
this.startTime = startTime;
}
/**
* Returns the datapoint representation of this accumulation
* @param {HrTime} startTime
*/
toPointValue(): InternalHistogram {
return {
hasMinMax: this._recordMinMax,
min: this.min,
max: this.max,
sum: this.sum,
positive: {
offset: this.positive.offset,
bucketCounts: this.positive.counts(),
},
negative: {
offset: this.negative.offset,
bucketCounts: this.negative.counts(),
},
count: this.count,
scale: this.scale,
zeroCount: this.zeroCount,
};
}
/**
* @returns {Number} The sum of values recorded by this accumulation
*/
get sum(): number {
return this._sum;
}
/**
* @returns {Number} The minimum value recorded by this accumulation
*/
get min(): number {
return this._min;
}
/**
* @returns {Number} The maximum value recorded by this accumulation
*/
get max(): number {
return this._max;
}
/**
* @returns {Number} The count of values recorded by this accumulation
*/
get count(): number {
return this._count;
}
/**
* @returns {Number} The number of 0 values recorded by this accumulation
*/
get zeroCount(): number {
return this._zeroCount;
}
/**
* @returns {Number} The scale used by this accumulation
*/
get scale(): number {
if (this._count === this._zeroCount) {
// all zeros! scale doesn't matter, use zero
return 0;
}
return this._mapping.scale;
}
/**
* positive holds the positive values
* @returns {Buckets}
*/
get positive(): Buckets {
return this._positive;
}
/**
* negative holds the negative values by their absolute value
* @returns {Buckets}
*/
get negative(): Buckets {
return this._negative;
}
/**
* updateByIncr supports updating a histogram with a non-negative
* increment.
* @param value
* @param increment
*/
updateByIncrement(value: number, increment: number) {
// NaN does not fall into any bucket, is not zero and should not be counted,
// NaN is never greater than max nor less than min, therefore return as there's nothing for us to do.
if (Number.isNaN(value)) {
return;
}
if (value > this._max) {
this._max = value;
}
if (value < this._min) {
this._min = value;
}
this._count += increment;
if (value === 0) {
this._zeroCount += increment;
return;
}
this._sum += value * increment;
if (value > 0) {
this._updateBuckets(this._positive, value, increment);
} else {
this._updateBuckets(this._negative, -value, increment);
}
}
/**
* merge combines data from previous value into self
* @param {ExponentialHistogramAccumulation} previous
*/
merge(previous: ExponentialHistogramAccumulation) {
if (this._count === 0) {
this._min = previous.min;
this._max = previous.max;
} else if (previous.count !== 0) {
if (previous.min < this.min) {
this._min = previous.min;
}
if (previous.max > this.max) {
this._max = previous.max;
}
}
this.startTime = previous.startTime;
this._sum += previous.sum;
this._count += previous.count;
this._zeroCount += previous.zeroCount;
const minScale = this._minScale(previous);
this._downscale(this.scale - minScale);
this._mergeBuckets(this.positive, previous, previous.positive, minScale);
this._mergeBuckets(this.negative, previous, previous.negative, minScale);
}
/**
* diff subtracts other from self
* @param {ExponentialHistogramAccumulation} other
*/
diff(other: ExponentialHistogramAccumulation) {
this._min = Infinity;
this._max = -Infinity;
this._sum -= other.sum;
this._count -= other.count;
this._zeroCount -= other.zeroCount;
const minScale = this._minScale(other);
this._downscale(this.scale - minScale);
this._diffBuckets(this.positive, other, other.positive, minScale);
this._diffBuckets(this.negative, other, other.negative, minScale);
}
/**
* clone returns a deep copy of self
* @returns {ExponentialHistogramAccumulation}
*/
clone(): ExponentialHistogramAccumulation {
return new ExponentialHistogramAccumulation(
this.startTime,
this._maxSize,
this._recordMinMax,
this._sum,
this._count,
this._zeroCount,
this._min,
this._max,
this.positive.clone(),
this.negative.clone(),
this._mapping
);
}
/**
* _updateBuckets maps the incoming value to a bucket index for the current
* scale. If the bucket index is outside of the range of the backing array,
* it will rescale the backing array and update the mapping for the new scale.
*/
private _updateBuckets(buckets: Buckets, value: number, increment: number) {
let index = this._mapping.mapToIndex(value);
// rescale the mapping if needed
let rescalingNeeded = false;
let high = 0;
let low = 0;
if (buckets.length === 0) {
buckets.indexStart = index;
buckets.indexEnd = buckets.indexStart;
buckets.indexBase = buckets.indexStart;
} else if (
index < buckets.indexStart &&
buckets.indexEnd - index >= this._maxSize
) {
rescalingNeeded = true;
low = index;
high = buckets.indexEnd;
} else if (
index > buckets.indexEnd &&
index - buckets.indexStart >= this._maxSize
) {
rescalingNeeded = true;
low = buckets.indexStart;
high = index;
}
// rescale and compute index at new scale
if (rescalingNeeded) {
const change = this._changeScale(high, low);
this._downscale(change);
index = this._mapping.mapToIndex(value);
}
this._incrementIndexBy(buckets, index, increment);
}
/**
* _incrementIndexBy increments the count of the bucket specified by `index`.
* If the index is outside of the range [buckets.indexStart, buckets.indexEnd]
* the boundaries of the backing array will be adjusted and more buckets will
* be added if needed.
*/
private _incrementIndexBy(
buckets: Buckets,
index: number,
increment: number
) {
if (increment === 0) {
// nothing to do for a zero increment, can happen during a merge operation
return;
}
if (buckets.length === 0) {
buckets.indexStart = buckets.indexEnd = buckets.indexBase = index;
}
if (index < buckets.indexStart) {
const span = buckets.indexEnd - index;
if (span >= buckets.backing.length) {
this._grow(buckets, span + 1);
}
buckets.indexStart = index;
} else if (index > buckets.indexEnd) {
const span = index - buckets.indexStart;
if (span >= buckets.backing.length) {
this._grow(buckets, span + 1);
}
buckets.indexEnd = index;
}
let bucketIndex = index - buckets.indexBase;
if (bucketIndex < 0) {
bucketIndex += buckets.backing.length;
}
buckets.incrementBucket(bucketIndex, increment);
}
/**
* grow resizes the backing array by doubling in size up to maxSize.
* This extends the array with a bunch of zeros and copies the
* existing counts to the same position.
*/
private _grow(buckets: Buckets, needed: number) {
const size = buckets.backing.length;
const bias = buckets.indexBase - buckets.indexStart;
const oldPositiveLimit = size - bias;
let newSize = nextGreaterSquare(needed);
if (newSize > this._maxSize) {
newSize = this._maxSize;
}
const newPositiveLimit = newSize - bias;
buckets.backing.growTo(newSize, oldPositiveLimit, newPositiveLimit);
}
/**
* _changeScale computes how much downscaling is needed by shifting the
* high and low values until they are separated by no more than size.
*/
private _changeScale(high: number, low: number): number {
let change = 0;
while (high - low >= this._maxSize) {
high >>= 1;
low >>= 1;
change++;
}
return change;
}
/**
* _downscale subtracts `change` from the current mapping scale.
*/
private _downscale(change: number) {
if (change === 0) {
return;
}
if (change < 0) {
// Note: this should be impossible. If we get here it's because
// there is a bug in the implementation.
throw new Error(`impossible change of scale: ${this.scale}`);
}
const newScale = this._mapping.scale - change;
this._positive.downscale(change);
this._negative.downscale(change);
this._mapping = getMapping(newScale);
}
/**
* _minScale is used by diff and merge to compute an ideal combined scale
*/
private _minScale(other: ExponentialHistogramAccumulation): number {
const minScale = Math.min(this.scale, other.scale);
const highLowPos = HighLow.combine(
this._highLowAtScale(this.positive, this.scale, minScale),
this._highLowAtScale(other.positive, other.scale, minScale)
);
const highLowNeg = HighLow.combine(
this._highLowAtScale(this.negative, this.scale, minScale),
this._highLowAtScale(other.negative, other.scale, minScale)
);
return Math.min(
minScale - this._changeScale(highLowPos.high, highLowPos.low),
minScale - this._changeScale(highLowNeg.high, highLowNeg.low)
);
}
/**
* _highLowAtScale is used by diff and merge to compute an ideal combined scale.
*/
private _highLowAtScale(
buckets: Buckets,
currentScale: number,
newScale: number
): HighLow {
if (buckets.length === 0) {
return new HighLow(0, -1);
}
const shift = currentScale - newScale;
return new HighLow(buckets.indexStart >> shift, buckets.indexEnd >> shift);
}
/**
* _mergeBuckets translates index values from another histogram and
* adds the values into the corresponding buckets of this histogram.
*/
private _mergeBuckets(
ours: Buckets,
other: ExponentialHistogramAccumulation,
theirs: Buckets,
scale: number
) {
const theirOffset = theirs.offset;
const theirChange = other.scale - scale;
for (let i = 0; i < theirs.length; i++) {
this._incrementIndexBy(
ours,
(theirOffset + i) >> theirChange,
theirs.at(i)
);
}
}
/**
* _diffBuckets translates index values from another histogram and
* subtracts the values in the corresponding buckets of this histogram.
*/
private _diffBuckets(
ours: Buckets,
other: ExponentialHistogramAccumulation,
theirs: Buckets,
scale: number
) {
const theirOffset = theirs.offset;
const theirChange = other.scale - scale;
for (let i = 0; i < theirs.length; i++) {
const ourIndex = (theirOffset + i) >> theirChange;
let bucketIndex = ourIndex - ours.indexBase;
if (bucketIndex < 0) {
bucketIndex += ours.backing.length;
}
ours.decrementBucket(bucketIndex, theirs.at(i));
}
ours.trim();
}
}
/**
* Aggregator for ExponentialHistogramAccumulations
*/
export class ExponentialHistogramAggregator
implements Aggregator<ExponentialHistogramAccumulation>
{
public kind: AggregatorKind.EXPONENTIAL_HISTOGRAM =
AggregatorKind.EXPONENTIAL_HISTOGRAM;
/**
* @param _maxSize Maximum number of buckets for each of the positive
* and negative ranges, exclusive of the zero-bucket.
* @param _recordMinMax If set to true, min and max will be recorded.
* Otherwise, min and max will not be recorded.
*/
constructor(
readonly _maxSize: number,
private readonly _recordMinMax: boolean
) {}
createAccumulation(startTime: HrTime) {
return new ExponentialHistogramAccumulation(
startTime,
this._maxSize,
this._recordMinMax
);
}
/**
* Return the result of the merge of two exponential histogram accumulations.
*/
merge(
previous: ExponentialHistogramAccumulation,
delta: ExponentialHistogramAccumulation
): ExponentialHistogramAccumulation {
const result = delta.clone();
result.merge(previous);
return result;
}
/**
* Returns a new DELTA aggregation by comparing two cumulative measurements.
*/
diff(
previous: ExponentialHistogramAccumulation,
current: ExponentialHistogramAccumulation
): ExponentialHistogramAccumulation {
const result = current.clone();
result.diff(previous);
return result;
}
toMetricData(
descriptor: MetricDescriptor,
aggregationTemporality: AggregationTemporality,
accumulationByAttributes: AccumulationRecord<ExponentialHistogramAccumulation>[],
endTime: HrTime
): Maybe<ExponentialHistogramMetricData> {
return {
descriptor,
aggregationTemporality,
dataPointType: DataPointType.EXPONENTIAL_HISTOGRAM,
dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {
const pointValue = accumulation.toPointValue();
// determine if instrument allows negative values.
const allowsNegativeValues =
descriptor.type === InstrumentType.GAUGE ||
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;
return {
attributes,
startTime: accumulation.startTime,
endTime,
value: {
min: pointValue.hasMinMax ? pointValue.min : undefined,
max: pointValue.hasMinMax ? pointValue.max : undefined,
sum: !allowsNegativeValues ? pointValue.sum : undefined,
positive: {
offset: pointValue.positive.offset,
bucketCounts: pointValue.positive.bucketCounts,
},
negative: {
offset: pointValue.negative.offset,
bucketCounts: pointValue.negative.bucketCounts,
},
count: pointValue.count,
scale: pointValue.scale,
zeroCount: pointValue.zeroCount,
},
};
}),
};
}
}