-
Notifications
You must be signed in to change notification settings - Fork 744
/
ExemplarReservoir.ts
141 lines (130 loc) · 3.69 KB
/
ExemplarReservoir.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
/*
* 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 {
Context,
HrTime,
isSpanContextValid,
trace,
MetricAttributes,
} from '@opentelemetry/api';
import { Exemplar } from './Exemplar';
/**
* An interface for an exemplar reservoir of samples.
*/
export interface ExemplarReservoir {
/** Offers a measurement to be sampled. */
offer(
value: number,
timestamp: HrTime,
attributes: MetricAttributes,
ctx: Context
): void;
/**
* Returns accumulated Exemplars and also resets the reservoir
* for the next sampling period
*
* @param pointAttributes The attributes associated with metric point.
*
* @returns a list of {@link Exemplar}s. Returned exemplars contain the attributes that were filtered out by the
* aggregator, but recorded alongside the original measurement.
*/
collect(pointAttributes: MetricAttributes): Exemplar[];
}
class ExemplarBucket {
private value: number = 0;
private attributes: MetricAttributes = {};
private timestamp: HrTime = [0, 0];
private spanId?: string;
private traceId?: string;
private _offered: boolean = false;
offer(
value: number,
timestamp: HrTime,
attributes: MetricAttributes,
ctx: Context
) {
this.value = value;
this.timestamp = timestamp;
this.attributes = attributes;
const spanContext = trace.getSpanContext(ctx);
if (spanContext && isSpanContextValid(spanContext)) {
this.spanId = spanContext.spanId;
this.traceId = spanContext.traceId;
}
this._offered = true;
}
collect(pointAttributes: MetricAttributes): Exemplar | null {
if (!this._offered) return null;
const currentAttributes = this.attributes;
// filter attributes
Object.keys(pointAttributes).forEach(key => {
if (pointAttributes[key] === currentAttributes[key]) {
delete currentAttributes[key];
}
});
const retVal: Exemplar = {
filteredAttributes: currentAttributes,
value: this.value,
timestamp: this.timestamp,
spanId: this.spanId,
traceId: this.traceId,
};
this.attributes = {};
this.value = 0;
this.timestamp = [0, 0];
this.spanId = undefined;
this.traceId = undefined;
this._offered = false;
return retVal;
}
}
export abstract class FixedSizeExemplarReservoirBase
implements ExemplarReservoir
{
protected _reservoirStorage: ExemplarBucket[];
protected _size: number;
constructor(size: number) {
this._size = size;
this._reservoirStorage = new Array<ExemplarBucket>(size);
for (let i = 0; i < this._size; i++) {
this._reservoirStorage[i] = new ExemplarBucket();
}
}
abstract offer(
value: number,
timestamp: HrTime,
attributes: MetricAttributes,
ctx: Context
): void;
maxSize(): number {
return this._size;
}
/**
* Resets the reservoir
*/
protected reset(): void {}
collect(pointAttributes: MetricAttributes): Exemplar[] {
const exemplars: Exemplar[] = [];
this._reservoirStorage.forEach(storageItem => {
const res = storageItem.collect(pointAttributes);
if (res !== null) {
exemplars.push(res);
}
});
this.reset();
return exemplars;
}
}