forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservableResult.test.ts
More file actions
108 lines (97 loc) · 4.09 KB
/
ObservableResult.test.ts
File metadata and controls
108 lines (97 loc) · 4.09 KB
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
/*
* 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 { ValueType } from '@opentelemetry/api-metrics';
import * as assert from 'assert';
import { InstrumentType } from '../src';
import { ObservableInstrument } from '../src/Instruments';
import {
BatchObservableResultImpl,
ObservableResultImpl
} from '../src/ObservableResult';
import { ObservableRegistry } from '../src/state/ObservableRegistry';
import { commonAttributes, commonValues, defaultInstrumentDescriptor } from './util';
describe('ObservableResultImpl', () => {
describe('observe', () => {
it('should observe common values', () => {
const observableResult = new ObservableResultImpl(defaultInstrumentDescriptor);
for (const value of commonValues) {
for (const attributes of commonAttributes) {
observableResult.observe(value, attributes);
}
}
});
it('should deduplicate observations', () => {
const observableResult = new ObservableResultImpl(defaultInstrumentDescriptor);
observableResult.observe(1, {});
observableResult.observe(2, {});
assert.strictEqual(observableResult._buffer.size, 1);
assert(observableResult._buffer.has({}));
assert.strictEqual(observableResult._buffer.get({}), 2);
});
it('should trunc value if ValueType is INT', () => {
const observableResult = new ObservableResultImpl({
name: 'test',
description: '',
type: InstrumentType.COUNTER,
unit: '',
valueType: ValueType.INT,
});
observableResult.observe(1.1, {});
assert.strictEqual(observableResult._buffer.get({}), 1);
});
});
});
describe('BatchObservableResultImpl', () => {
describe('observe', () => {
it('should observe common values', () => {
const observableResult = new BatchObservableResultImpl();
const observable = new ObservableInstrument(defaultInstrumentDescriptor, [], new ObservableRegistry());
for (const value of commonValues) {
for (const attributes of commonAttributes) {
observableResult.observe(observable, value, attributes);
}
}
});
it('should deduplicate observations', () => {
const observableResult = new BatchObservableResultImpl();
const observableRegistry = new ObservableRegistry();
const observable1 = new ObservableInstrument(defaultInstrumentDescriptor, [], observableRegistry);
const observable2 = new ObservableInstrument(defaultInstrumentDescriptor, [], observableRegistry);
observableResult.observe(observable1, 1, {});
observableResult.observe(observable1, 2, {});
observableResult.observe(observable2, 4, {});
assert.strictEqual(observableResult._buffer.size, 2);
assert(observableResult._buffer.has(observable1));
assert(observableResult._buffer.has(observable2));
const observable1Buffer = observableResult._buffer.get(observable1);
const observable2Buffer = observableResult._buffer.get(observable2);
assert.strictEqual(observable1Buffer?.get({}), 2);
assert.strictEqual(observable2Buffer?.get({}), 4);
});
it('should trunc value if ValueType is INT', () => {
const observableResult = new BatchObservableResultImpl();
const observable = new ObservableInstrument({
name: 'test',
description: '',
type: InstrumentType.COUNTER,
unit: '',
valueType: ValueType.INT,
}, [], new ObservableRegistry());
observableResult.observe(observable, 1.1, {});
assert.strictEqual(observableResult._buffer.get(observable)?.get({}), 1);
});
});
});