Skip to content

Commit b3ec16b

Browse files
committed
test(instrumentation-runtime-node): add tests for v8js.memory.heap.max
Verify that the metric is emitted as a gauge with a positive value and does not carry the per-space v8js.heap.space.name attribute.
1 parent 8c29fce commit b3ec16b

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { DataPointType, MeterProvider } from '@opentelemetry/sdk-metrics';
19+
import { RuntimeNodeInstrumentation } from '../src';
20+
import { TestMetricReader } from './testMetricsReader';
21+
import { METRIC_V8JS_MEMORY_HEAP_MAX } from '../src/semconv';
22+
23+
const MEASUREMENT_INTERVAL = 10;
24+
25+
describe('v8js.memory.heap.max', function () {
26+
let metricReader: TestMetricReader;
27+
let meterProvider: MeterProvider;
28+
29+
beforeEach(() => {
30+
metricReader = new TestMetricReader();
31+
meterProvider = new MeterProvider({
32+
readers: [metricReader],
33+
});
34+
});
35+
36+
it(`should write ${METRIC_V8JS_MEMORY_HEAP_MAX} after monitoringPrecision`, async function () {
37+
// arrange
38+
const instrumentation = new RuntimeNodeInstrumentation({
39+
monitoringPrecision: MEASUREMENT_INTERVAL,
40+
});
41+
instrumentation.setMeterProvider(meterProvider);
42+
43+
// act
44+
await new Promise(resolve =>
45+
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
46+
);
47+
const { resourceMetrics, errors } = await metricReader.collect();
48+
49+
// assert
50+
assert.deepEqual(
51+
errors,
52+
[],
53+
'expected no errors from the callback during collection'
54+
);
55+
const scopeMetrics = resourceMetrics.scopeMetrics;
56+
const metric = scopeMetrics[0].metrics.find(
57+
x => x.descriptor.name === METRIC_V8JS_MEMORY_HEAP_MAX
58+
);
59+
60+
assert.notEqual(metric, undefined, `${METRIC_V8JS_MEMORY_HEAP_MAX} not found`);
61+
62+
assert.strictEqual(
63+
metric!.dataPointType,
64+
DataPointType.GAUGE,
65+
'expected gauge'
66+
);
67+
68+
assert.strictEqual(
69+
metric!.descriptor.name,
70+
METRIC_V8JS_MEMORY_HEAP_MAX,
71+
'descriptor.name'
72+
);
73+
});
74+
75+
it('should have a positive value representing the heap size limit', async function () {
76+
// arrange
77+
const instrumentation = new RuntimeNodeInstrumentation({
78+
monitoringPrecision: MEASUREMENT_INTERVAL,
79+
});
80+
instrumentation.setMeterProvider(meterProvider);
81+
82+
// act
83+
await new Promise(resolve =>
84+
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
85+
);
86+
const { resourceMetrics, errors } = await metricReader.collect();
87+
88+
// assert
89+
assert.deepEqual(errors, []);
90+
const scopeMetrics = resourceMetrics.scopeMetrics;
91+
const metric = scopeMetrics[0].metrics.find(
92+
x => x.descriptor.name === METRIC_V8JS_MEMORY_HEAP_MAX
93+
);
94+
95+
assert.notEqual(metric, undefined, `${METRIC_V8JS_MEMORY_HEAP_MAX} not found`);
96+
97+
if (metric!.dataPointType === DataPointType.GAUGE) {
98+
assert.strictEqual(metric!.dataPoints.length, 1, 'expected exactly one data point (global, not per-space)');
99+
const value = metric!.dataPoints[0].value as number;
100+
assert.ok(value > 0, `expected positive heap_size_limit, got ${value}`);
101+
}
102+
});
103+
104+
it('should not have v8js.heap.space.name attribute (global metric)', async function () {
105+
// arrange
106+
const instrumentation = new RuntimeNodeInstrumentation({
107+
monitoringPrecision: MEASUREMENT_INTERVAL,
108+
});
109+
instrumentation.setMeterProvider(meterProvider);
110+
111+
// act
112+
await new Promise(resolve =>
113+
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
114+
);
115+
const { resourceMetrics, errors } = await metricReader.collect();
116+
117+
// assert
118+
assert.deepEqual(errors, []);
119+
const scopeMetrics = resourceMetrics.scopeMetrics;
120+
const metric = scopeMetrics[0].metrics.find(
121+
x => x.descriptor.name === METRIC_V8JS_MEMORY_HEAP_MAX
122+
);
123+
124+
assert.notEqual(metric, undefined);
125+
126+
if (metric!.dataPointType === DataPointType.GAUGE) {
127+
for (const dp of metric!.dataPoints) {
128+
assert.strictEqual(
129+
dp.attributes['v8js.heap.space.name'],
130+
undefined,
131+
'v8js.memory.heap.max should not have v8js.heap.space.name attribute'
132+
);
133+
}
134+
}
135+
});
136+
});

0 commit comments

Comments
 (0)