forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instruments.ts
188 lines (174 loc) · 4.9 KB
/
Instruments.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
/*
* 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 as contextApi,
diag,
Context,
MetricAttributes,
ValueType,
UpDownCounter,
Counter,
Histogram,
Observable,
ObservableCallback,
ObservableCounter,
ObservableGauge,
ObservableUpDownCounter,
} from '@opentelemetry/api';
import { millisToHrTime } from '@opentelemetry/core';
import { InstrumentDescriptor } from './InstrumentDescriptor';
import { ObservableRegistry } from './state/ObservableRegistry';
import {
AsyncWritableMetricStorage,
WritableMetricStorage,
} from './state/WritableMetricStorage';
import { Gauge } from './types';
export class SyncInstrument {
constructor(
private _writableMetricStorage: WritableMetricStorage,
protected _descriptor: InstrumentDescriptor
) {}
protected _record(
value: number,
attributes: MetricAttributes = {},
context: Context = contextApi.active()
) {
if (typeof value !== 'number') {
diag.warn(
`non-number value provided to metric ${this._descriptor.name}: ${value}`
);
return;
}
if (
this._descriptor.valueType === ValueType.INT &&
!Number.isInteger(value)
) {
diag.warn(
`INT value type cannot accept a floating-point value for ${this._descriptor.name}, ignoring the fractional digits.`
);
value = Math.trunc(value);
// ignore non-finite values.
if (!Number.isInteger(value)) {
return;
}
}
this._writableMetricStorage.record(
value,
attributes,
context,
millisToHrTime(Date.now())
);
}
}
/**
* The class implements {@link UpDownCounter} interface.
*/
export class UpDownCounterInstrument
extends SyncInstrument
implements UpDownCounter
{
/**
* Increment value of counter by the input. Inputs may be negative.
*/
add(value: number, attributes?: MetricAttributes, ctx?: Context): void {
this._record(value, attributes, ctx);
}
}
/**
* The class implements {@link Counter} interface.
*/
export class CounterInstrument extends SyncInstrument implements Counter {
/**
* Increment value of counter by the input. Inputs may not be negative.
*/
add(value: number, attributes?: MetricAttributes, ctx?: Context): void {
if (value < 0) {
diag.warn(
`negative value provided to counter ${this._descriptor.name}: ${value}`
);
return;
}
this._record(value, attributes, ctx);
}
}
/**
* The class implements {@link Gauge} interface.
*/
export class GaugeInstrument extends SyncInstrument implements Gauge {
/**
* Records a measurement.
*/
record(value: number, attributes?: MetricAttributes, ctx?: Context): void {
this._record(value, attributes, ctx);
}
}
/**
* The class implements {@link Histogram} interface.
*/
export class HistogramInstrument extends SyncInstrument implements Histogram {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: MetricAttributes, ctx?: Context): void {
if (value < 0) {
diag.warn(
`negative value provided to histogram ${this._descriptor.name}: ${value}`
);
return;
}
this._record(value, attributes, ctx);
}
}
export class ObservableInstrument implements Observable {
/** @internal */
_metricStorages: AsyncWritableMetricStorage[];
/** @internal */
_descriptor: InstrumentDescriptor;
constructor(
descriptor: InstrumentDescriptor,
metricStorages: AsyncWritableMetricStorage[],
private _observableRegistry: ObservableRegistry
) {
this._descriptor = descriptor;
this._metricStorages = metricStorages;
}
/**
* @see {Observable.addCallback}
*/
addCallback(callback: ObservableCallback) {
this._observableRegistry.addCallback(callback, this);
}
/**
* @see {Observable.removeCallback}
*/
removeCallback(callback: ObservableCallback) {
this._observableRegistry.removeCallback(callback, this);
}
}
export class ObservableCounterInstrument
extends ObservableInstrument
implements ObservableCounter {}
export class ObservableGaugeInstrument
extends ObservableInstrument
implements ObservableGauge {}
export class ObservableUpDownCounterInstrument
extends ObservableInstrument
implements ObservableUpDownCounter {}
export function isObservableInstrument(
it: unknown
): it is ObservableInstrument {
return it instanceof ObservableInstrument;
}