Skip to content

Commit 046072e

Browse files
committed
test(prom-client) add tests for check implementation
1 parent ed6596f commit 046072e

File tree

6 files changed

+378
-56
lines changed

6 files changed

+378
-56
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/eventLoopLagCollector';
22+
23+
const MEASUREMENT_INTERVAL = 10;
24+
25+
describe('nodejs.event_loop.lag', function () {
26+
let metricReader: TestMetricReader;
27+
let meterProvider: MeterProvider;
28+
29+
beforeEach(() => {
30+
metricReader = new TestMetricReader();
31+
meterProvider = new MeterProvider();
32+
meterProvider.addMetricReader(metricReader);
33+
});
34+
35+
for (const metricName of metricNames) {
36+
it(`should write nodejs.${metricName.name} 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 === 'nodejs.' + metricName.name
58+
);
59+
60+
assert.notEqual(metric, undefined, `nodejs.${metricName.name} 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+
'nodejs.' + metricName.name,
71+
'descriptor.name'
72+
);
73+
});
74+
}
75+
});

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

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {
17-
MeterProvider,
18-
DataPointType,
19-
MetricReader,
20-
} from '@opentelemetry/sdk-metrics';
16+
import { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';
2117

2218
import { RuntimeNodeInstrumentation } from '../src';
2319
import * as assert from 'assert';
20+
import { TestMetricReader } from './testMetricsReader';
2421

2522
const MEASUREMENT_INTERVAL = 10;
2623

27-
class TestMetricReader extends MetricReader {
28-
constructor() {
29-
super();
30-
}
31-
32-
protected async onForceFlush(): Promise<void> {}
33-
34-
protected async onShutdown(): Promise<void> {}
35-
}
36-
3724
describe('nodejs.event_loop.utilization', function () {
3825
let metricReader: TestMetricReader;
3926
let meterProvider: MeterProvider;
@@ -47,7 +34,7 @@ describe('nodejs.event_loop.utilization', function () {
4734
it('should not export before being enabled', async function () {
4835
// arrange
4936
const instrumentation = new RuntimeNodeInstrumentation({
50-
eventLoopUtilizationMeasurementInterval: MEASUREMENT_INTERVAL,
37+
monitoringPrecision: MEASUREMENT_INTERVAL,
5138
enabled: false,
5239
});
5340
instrumentation.setMeterProvider(meterProvider);
@@ -62,32 +49,10 @@ describe('nodejs.event_loop.utilization', function () {
6249
assert.strictEqual(scopeMetrics.length, 0);
6350
});
6451

65-
it('should not record result when collecting immediately with custom config', async function () {
66-
const instrumentation = new RuntimeNodeInstrumentation({
67-
eventLoopUtilizationMeasurementInterval: MEASUREMENT_INTERVAL,
68-
});
69-
instrumentation.setMeterProvider(meterProvider);
70-
71-
assert.deepEqual(
72-
(await metricReader.collect()).resourceMetrics.scopeMetrics,
73-
[]
74-
);
75-
});
76-
77-
it('should not record result when collecting immediately with default config', async function () {
78-
const instrumentation = new RuntimeNodeInstrumentation();
79-
instrumentation.setMeterProvider(meterProvider);
80-
81-
assert.deepEqual(
82-
(await metricReader.collect()).resourceMetrics.scopeMetrics,
83-
[]
84-
);
85-
});
86-
87-
it('should write event loop utilization metrics after eventLoopUtilizationMeasurementInterval', async function () {
52+
it('should write event loop utilization metrics after monitoringPrecision', async function () {
8853
// arrange
8954
const instrumentation = new RuntimeNodeInstrumentation({
90-
eventLoopUtilizationMeasurementInterval: MEASUREMENT_INTERVAL,
55+
monitoringPrecision: MEASUREMENT_INTERVAL,
9156
});
9257
instrumentation.setMeterProvider(meterProvider);
9358

@@ -102,42 +67,42 @@ describe('nodejs.event_loop.utilization', function () {
10267
'expected no errors from the callback during collection'
10368
);
10469
const scopeMetrics = resourceMetrics.scopeMetrics;
105-
assert.strictEqual(
106-
scopeMetrics.length,
107-
1,
108-
'expected one scope (one meter created by instrumentation)'
109-
);
110-
const metrics = scopeMetrics[0].metrics;
111-
assert.strictEqual(
112-
metrics.length,
113-
1,
114-
'expected one metric (one metric created by instrumentation)'
70+
const utilizationMetric = scopeMetrics[0].metrics.find(
71+
x => x.descriptor.name === 'nodejs.event_loop.utilization'
11572
);
73+
74+
assert.notEqual(utilizationMetric, undefined, 'metric not found');
75+
11676
assert.strictEqual(
117-
metrics[0].dataPointType,
77+
utilizationMetric!.dataPointType,
11878
DataPointType.GAUGE,
11979
'expected gauge'
12080
);
81+
12182
assert.strictEqual(
122-
metrics[0].descriptor.name,
83+
utilizationMetric!.descriptor.name,
12384
'nodejs.event_loop.utilization',
12485
'descriptor.name'
12586
);
87+
12688
assert.strictEqual(
127-
metrics[0].descriptor.description,
89+
utilizationMetric!.descriptor.description,
12890
'Event loop utilization'
12991
);
92+
13093
assert.strictEqual(
131-
metrics[0].descriptor.unit,
94+
utilizationMetric!.descriptor.unit,
13295
'1',
13396
'expected default unit'
13497
);
98+
13599
assert.strictEqual(
136-
metrics[0].dataPoints.length,
100+
utilizationMetric!.dataPoints.length,
137101
1,
138102
'expected one data point'
139103
);
140-
const val = metrics[0].dataPoints[0].value;
104+
105+
const val = utilizationMetric!.dataPoints[0].value;
141106
assert.strictEqual(val > 0, true, `val (${val}) > 0`);
142107
assert.strictEqual(val <= 1, true, `val (${val}) <= 1`);
143108
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/heapSizeAndUsedCollector';
22+
23+
const MEASUREMENT_INTERVAL = 10;
24+
25+
describe('nodejs.heap_size', function () {
26+
let metricReader: TestMetricReader;
27+
let meterProvider: MeterProvider;
28+
29+
beforeEach(() => {
30+
metricReader = new TestMetricReader();
31+
meterProvider = new MeterProvider();
32+
meterProvider.addMetricReader(metricReader);
33+
});
34+
35+
for (const metricName of metricNames) {
36+
it(`should write nodejs.${metricName.name} 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 === 'nodejs.' + metricName.name
58+
);
59+
60+
assert.notEqual(metric, undefined, `nodejs.${metricName.name} 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+
'nodejs.' + metricName.name,
71+
'descriptor.name'
72+
);
73+
});
74+
}
75+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/heapSpacesSizeAndUsedCollector';
22+
23+
const MEASUREMENT_INTERVAL = 10;
24+
25+
describe('nodejs.heap_space', function () {
26+
let metricReader: TestMetricReader;
27+
let meterProvider: MeterProvider;
28+
29+
beforeEach(() => {
30+
metricReader = new TestMetricReader();
31+
meterProvider = new MeterProvider();
32+
meterProvider.addMetricReader(metricReader);
33+
});
34+
35+
for (const metricName of metricNames) {
36+
it(`should write nodejs.${metricName.name} 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 === 'nodejs.' + metricName.name
58+
);
59+
60+
assert.notEqual(metric, undefined, `nodejs.${metricName.name} 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+
'nodejs.' + metricName.name,
71+
'descriptor.name'
72+
);
73+
});
74+
}
75+
});

0 commit comments

Comments
 (0)