Skip to content

Commit 54858de

Browse files
committed
test(instrumentation-runtime-node): fix tests
1 parent 3cba596 commit 54858de

File tree

9 files changed

+246
-303
lines changed

9 files changed

+246
-303
lines changed

plugins/node/instrumentation-runtime-node/src/metrics/eventLoopDelayCollector.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,40 @@ enum NodeJsEventLoopDelay {
3131
}
3232

3333
export const metricNames: Record<NodeJsEventLoopDelay, { description: string }> = {
34-
[NodeJsEventLoopDelay.delay]:
35-
{
34+
[NodeJsEventLoopDelay.delay]: {
3635
description:
3736
'Lag of event loop in seconds.'
3837
},
39-
[NodeJsEventLoopDelay.min]:
40-
{
38+
[NodeJsEventLoopDelay.min]: {
4139
description:
4240
'The minimum recorded event loop delay.',
4341
},
44-
[NodeJsEventLoopDelay.max]:
45-
{
42+
[NodeJsEventLoopDelay.max]: {
4643
description:
4744
'The maximum recorded event loop delay.',
4845
}
4946
,
50-
[NodeJsEventLoopDelay.mean]:
51-
{
47+
[NodeJsEventLoopDelay.mean]: {
5248
description:
5349
'The mean of the recorded event loop delays.',
5450
}
5551
,
56-
[NodeJsEventLoopDelay.stddev]:
57-
{
52+
[NodeJsEventLoopDelay.stddev]: {
5853
description:
5954
'The standard deviation of the recorded event loop delays.',
6055
}
6156
,
62-
[NodeJsEventLoopDelay.p50]:
63-
{
57+
[NodeJsEventLoopDelay.p50]: {
6458
description:
6559
'The 50th percentile of the recorded event loop delays.',
6660
}
6761
,
68-
[NodeJsEventLoopDelay.p90]:
69-
{
62+
[NodeJsEventLoopDelay.p90]: {
7063
description:
7164
'The 90th percentile of the recorded event loop delays.',
7265
}
7366
,
74-
[NodeJsEventLoopDelay.p99]:
75-
{
67+
[NodeJsEventLoopDelay.p99]: {
7668

7769
description:
7870
'The 99th percentile of the recorded event loop delays.',

plugins/node/instrumentation-runtime-node/src/metrics/eventLoopUtilizationCollector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BaseCollector } from './baseCollector';
2020

2121
const { eventLoopUtilization: eventLoopUtilizationCollector } = performance;
2222

23-
const NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';
23+
export const NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';
2424

2525
export class EventLoopUtilizationCollector extends BaseCollector<EventLoopUtilization> {
2626
constructor(

plugins/node/instrumentation-runtime-node/src/metrics/heapSizeAndUsedCollector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import { Meter } from '@opentelemetry/api';
1818
import { BaseCollector } from './baseCollector';
1919
import {HeapSizes} from "../types/heapSizes";
2020

21-
const NODEJS_HEAP_SIZE = 'heap.size';
22-
const NODEJS_HEAP_SIZE_STATE = 'heap.size.state';
21+
export const V8_HEAP_SIZE = 'heap.size';
22+
const V8_HEAP_SIZE_STATE = 'heap.size.state';
2323

2424
export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage> {
2525
constructor(
@@ -31,7 +31,7 @@ export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage>
3131

3232
updateMetricInstruments(meter: Meter): void {
3333
meter.createObservableGauge(
34-
`${this.namePrefix}.${NODEJS_HEAP_SIZE}`,
34+
`${this.namePrefix}.${V8_HEAP_SIZE}`,
3535
{
3636
description: "Process heap size from Node.js in bytes.",
3737
unit: 'By',
@@ -42,11 +42,11 @@ export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage>
4242
const data = this._scrapeQueue.shift();
4343
if (data === undefined) return;
4444
observableResult.observe(data.heapTotal, {
45-
[`${this.namePrefix}.${NODEJS_HEAP_SIZE_STATE}`]: HeapSizes.Total,
45+
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE}`]: HeapSizes.Total,
4646
...this.versionAttribute
4747
});
4848
observableResult.observe(data.heapUsed, {
49-
[`${this.namePrefix}.${NODEJS_HEAP_SIZE_STATE}`]: HeapSizes.Used,
49+
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE}`]: HeapSizes.Used,
5050
...this.versionAttribute
5151
});
5252
});

plugins/node/instrumentation-runtime-node/src/metrics/heapSpacesSizeAndUsedCollector.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ import {Meter} from '@opentelemetry/api';
1818
import {BaseCollector} from './baseCollector';
1919
import * as v8 from 'node:v8';
2020
import {HeapSpaceInfo} from 'v8';
21-
import {HeapSpaces} from "../types/heapSpaces";
2221

2322

2423
enum V8HeapSpaceMetrics {
25-
size = 'heap.size',
2624
spaceSize = 'heap.space_size',
2725
used = 'heap.space_used_size',
2826
available = 'heap.space_available_size',
@@ -34,10 +32,6 @@ export const metricNames: Record<V8HeapSpaceMetrics, { description: string }> =
3432
description:
3533
'Process heap space size total from Node.js in bytes.',
3634
},
37-
[V8HeapSpaceMetrics.size]: {
38-
description:
39-
'Process heap space size total from Node.js in bytes.',
40-
},
4135
[V8HeapSpaceMetrics.used]: {
4236
description:
4337
'Process heap space size used from Node.js in bytes.',
@@ -70,13 +64,6 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
7064
unit: 'bytes',
7165
}
7266
);
73-
const heapSize = meter.createObservableGauge(
74-
`${this.namePrefix}.${V8HeapSpaceMetrics.size}`,
75-
{
76-
description: metricNames[V8HeapSpaceMetrics.size].description,
77-
unit: 'bytes',
78-
}
79-
);
8067
const heapSpaceUsed = meter.createObservableGauge(
8168
`${this.namePrefix}.${V8HeapSpaceMetrics.used}`,
8269
{
@@ -99,8 +86,6 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
9986
}
10087
);
10188
const heapSpaceNameAttributeName = `${this.namePrefix}.heap.space.name`
102-
const heapSizeStateAttributeName = `${this.namePrefix}.heap.size.state`
103-
10489

10590
meter.addBatchObservableCallback(
10691
observableResult => {
@@ -112,14 +97,6 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
11297

11398
const spaceName = space.space_name
11499

115-
observableResult.observe(heapSize, space.space_size, {
116-
[heapSizeStateAttributeName]: HeapSpaces.Total,
117-
});
118-
119-
observableResult.observe(heapSize, space.space_used_size, {
120-
[heapSizeStateAttributeName]: HeapSpaces.Used,
121-
});
122-
123100
observableResult.observe(heapSpaceSize, space.space_size, {
124101
[heapSpaceNameAttributeName]: spaceName,
125102
});
@@ -141,7 +118,7 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
141118
});
142119
}
143120
},
144-
[heapSize, heapSpaceSize, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical]
121+
[heapSpaceSize, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical]
145122
);
146123
}
147124

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
import { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';
17+
18+
import { RuntimeNodeInstrumentation } from '../src';
19+
import * as assert from 'assert';
20+
import { TestMetricReader } from './testMetricsReader';
21+
import { metricNames } from '../src/metrics/eventLoopDelayCollector';
22+
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
23+
24+
const MEASUREMENT_INTERVAL = 10;
25+
26+
describe(`${ConventionalNamePrefix.NodeJsRuntime}.eventloop`, function () {
27+
let metricReader: TestMetricReader;
28+
let meterProvider: MeterProvider;
29+
30+
beforeEach(() => {
31+
metricReader = new TestMetricReader();
32+
meterProvider = new MeterProvider();
33+
meterProvider.addMetricReader(metricReader);
34+
});
35+
36+
for (const metricName in metricNames) {
37+
it(`should write ${ConventionalNamePrefix.NodeJsRuntime}.${metricName} after monitoringPrecision`, async function () {
38+
// arrange
39+
const instrumentation = new RuntimeNodeInstrumentation({
40+
monitoringPrecision: MEASUREMENT_INTERVAL,
41+
});
42+
instrumentation.setMeterProvider(meterProvider);
43+
44+
// act
45+
await new Promise(resolve =>
46+
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
47+
);
48+
const { resourceMetrics, errors } = await metricReader.collect();
49+
50+
// assert
51+
assert.deepEqual(
52+
errors,
53+
[],
54+
'expected no errors from the callback during collection'
55+
);
56+
const scopeMetrics = resourceMetrics.scopeMetrics;
57+
const metric = scopeMetrics[0].metrics.find(
58+
x => x.descriptor.name === `${ConventionalNamePrefix.NodeJsRuntime}.${ metricName}`
59+
);
60+
61+
assert.notEqual(metric, undefined, `${ConventionalNamePrefix.NodeJsRuntime}.${metricName} not found`);
62+
63+
assert.strictEqual(
64+
metric!.dataPointType,
65+
DataPointType.GAUGE,
66+
'expected gauge'
67+
);
68+
69+
assert.strictEqual(
70+
metric!.descriptor.name,
71+
`${ConventionalNamePrefix.NodeJsRuntime}.${ metricName}`,
72+
'descriptor.name'
73+
);
74+
});
75+
}
76+
});

plugins/node/instrumentation-runtime-node/test/event_loop_lag.test.ts

Lines changed: 0 additions & 104 deletions
This file was deleted.

plugins/node/instrumentation-runtime-node/test/event_loop_utilization.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';
1818
import { RuntimeNodeInstrumentation } from '../src';
1919
import * as assert from 'assert';
2020
import { TestMetricReader } from './testMetricsReader';
21+
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
22+
import {NODEJS_EVENT_LOOP_UTILIZATION} from "../src/metrics/eventLoopUtilizationCollector";
2123

2224
const MEASUREMENT_INTERVAL = 10;
2325

24-
describe('jsruntime.eventloop.utilization', function () {
26+
describe(`${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION}`, function () {
2527
let metricReader: TestMetricReader;
2628
let meterProvider: MeterProvider;
2729

@@ -68,7 +70,7 @@ describe('jsruntime.eventloop.utilization', function () {
6870
);
6971
const scopeMetrics = resourceMetrics.scopeMetrics;
7072
const utilizationMetric = scopeMetrics[0].metrics.find(
71-
x => x.descriptor.name === 'jsruntime.eventloop.utilization'
73+
x => x.descriptor.name === `${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION}`
7274
);
7375

7476
assert.notEqual(utilizationMetric, undefined, 'metric not found');
@@ -81,7 +83,7 @@ describe('jsruntime.eventloop.utilization', function () {
8183

8284
assert.strictEqual(
8385
utilizationMetric!.descriptor.name,
84-
'jsruntime.eventloop.utilization',
86+
`${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION}`,
8587
'descriptor.name'
8688
);
8789

0 commit comments

Comments
 (0)