Skip to content

Commit

Permalink
test(prom-client) add tests for check implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pikalovArtemN committed Apr 22, 2024
1 parent ed6596f commit 046072e
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';

import { RuntimeNodeInstrumentation } from '../src';
import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import { metricNames } from '../src/metrics/eventLoopLagCollector';

const MEASUREMENT_INTERVAL = 10;

describe('nodejs.event_loop.lag', function () {
let metricReader: TestMetricReader;
let meterProvider: MeterProvider;

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
});

for (const metricName of metricNames) {
it(`should write nodejs.${metricName.name} after monitoringPrecision`, async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
monitoringPrecision: MEASUREMENT_INTERVAL,
});
instrumentation.setMeterProvider(meterProvider);

// act
await new Promise(resolve =>
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
);
const { resourceMetrics, errors } = await metricReader.collect();

// assert
assert.deepEqual(
errors,
[],
'expected no errors from the callback during collection'
);
const scopeMetrics = resourceMetrics.scopeMetrics;
const metric = scopeMetrics[0].metrics.find(
x => x.descriptor.name === 'nodejs.' + metricName.name
);

assert.notEqual(metric, undefined, `nodejs.${metricName.name} not found`);

assert.strictEqual(
metric!.dataPointType,
DataPointType.GAUGE,
'expected gauge'
);

assert.strictEqual(
metric!.descriptor.name,
'nodejs.' + metricName.name,
'descriptor.name'
);
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
MeterProvider,
DataPointType,
MetricReader,
} from '@opentelemetry/sdk-metrics';
import { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';

import { RuntimeNodeInstrumentation } from '../src';
import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';

const MEASUREMENT_INTERVAL = 10;

class TestMetricReader extends MetricReader {
constructor() {
super();
}

protected async onForceFlush(): Promise<void> {}

protected async onShutdown(): Promise<void> {}
}

describe('nodejs.event_loop.utilization', function () {
let metricReader: TestMetricReader;
let meterProvider: MeterProvider;
Expand All @@ -47,7 +34,7 @@ describe('nodejs.event_loop.utilization', function () {
it('should not export before being enabled', async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
eventLoopUtilizationMeasurementInterval: MEASUREMENT_INTERVAL,
monitoringPrecision: MEASUREMENT_INTERVAL,
enabled: false,
});
instrumentation.setMeterProvider(meterProvider);
Expand All @@ -62,32 +49,10 @@ describe('nodejs.event_loop.utilization', function () {
assert.strictEqual(scopeMetrics.length, 0);
});

it('should not record result when collecting immediately with custom config', async function () {
const instrumentation = new RuntimeNodeInstrumentation({
eventLoopUtilizationMeasurementInterval: MEASUREMENT_INTERVAL,
});
instrumentation.setMeterProvider(meterProvider);

assert.deepEqual(
(await metricReader.collect()).resourceMetrics.scopeMetrics,
[]
);
});

it('should not record result when collecting immediately with default config', async function () {
const instrumentation = new RuntimeNodeInstrumentation();
instrumentation.setMeterProvider(meterProvider);

assert.deepEqual(
(await metricReader.collect()).resourceMetrics.scopeMetrics,
[]
);
});

it('should write event loop utilization metrics after eventLoopUtilizationMeasurementInterval', async function () {
it('should write event loop utilization metrics after monitoringPrecision', async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
eventLoopUtilizationMeasurementInterval: MEASUREMENT_INTERVAL,
monitoringPrecision: MEASUREMENT_INTERVAL,
});
instrumentation.setMeterProvider(meterProvider);

Expand All @@ -102,42 +67,42 @@ describe('nodejs.event_loop.utilization', function () {
'expected no errors from the callback during collection'
);
const scopeMetrics = resourceMetrics.scopeMetrics;
assert.strictEqual(
scopeMetrics.length,
1,
'expected one scope (one meter created by instrumentation)'
);
const metrics = scopeMetrics[0].metrics;
assert.strictEqual(
metrics.length,
1,
'expected one metric (one metric created by instrumentation)'
const utilizationMetric = scopeMetrics[0].metrics.find(
x => x.descriptor.name === 'nodejs.event_loop.utilization'
);

assert.notEqual(utilizationMetric, undefined, 'metric not found');

assert.strictEqual(
metrics[0].dataPointType,
utilizationMetric!.dataPointType,
DataPointType.GAUGE,
'expected gauge'
);

assert.strictEqual(
metrics[0].descriptor.name,
utilizationMetric!.descriptor.name,
'nodejs.event_loop.utilization',
'descriptor.name'
);

assert.strictEqual(
metrics[0].descriptor.description,
utilizationMetric!.descriptor.description,
'Event loop utilization'
);

assert.strictEqual(
metrics[0].descriptor.unit,
utilizationMetric!.descriptor.unit,
'1',
'expected default unit'
);

assert.strictEqual(
metrics[0].dataPoints.length,
utilizationMetric!.dataPoints.length,
1,
'expected one data point'
);
const val = metrics[0].dataPoints[0].value;

const val = utilizationMetric!.dataPoints[0].value;
assert.strictEqual(val > 0, true, `val (${val}) > 0`);
assert.strictEqual(val <= 1, true, `val (${val}) <= 1`);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';

import { RuntimeNodeInstrumentation } from '../src';
import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import { metricNames } from '../src/metrics/heapSizeAndUsedCollector';

const MEASUREMENT_INTERVAL = 10;

describe('nodejs.heap_size', function () {
let metricReader: TestMetricReader;
let meterProvider: MeterProvider;

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
});

for (const metricName of metricNames) {
it(`should write nodejs.${metricName.name} after monitoringPrecision`, async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
monitoringPrecision: MEASUREMENT_INTERVAL,
});
instrumentation.setMeterProvider(meterProvider);

// act
await new Promise(resolve =>
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
);
const { resourceMetrics, errors } = await metricReader.collect();

// assert
assert.deepEqual(
errors,
[],
'expected no errors from the callback during collection'
);
const scopeMetrics = resourceMetrics.scopeMetrics;
const metric = scopeMetrics[0].metrics.find(
x => x.descriptor.name === 'nodejs.' + metricName.name
);

assert.notEqual(metric, undefined, `nodejs.${metricName.name} not found`);

assert.strictEqual(
metric!.dataPointType,
DataPointType.GAUGE,
'expected gauge'
);

assert.strictEqual(
metric!.descriptor.name,
'nodejs.' + metricName.name,
'descriptor.name'
);
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';

import { RuntimeNodeInstrumentation } from '../src';
import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import { metricNames } from '../src/metrics/heapSpacesSizeAndUsedCollector';

const MEASUREMENT_INTERVAL = 10;

describe('nodejs.heap_space', function () {
let metricReader: TestMetricReader;
let meterProvider: MeterProvider;

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
});

for (const metricName of metricNames) {
it(`should write nodejs.${metricName.name} after monitoringPrecision`, async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
monitoringPrecision: MEASUREMENT_INTERVAL,
});
instrumentation.setMeterProvider(meterProvider);

// act
await new Promise(resolve =>
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
);
const { resourceMetrics, errors } = await metricReader.collect();

// assert
assert.deepEqual(
errors,
[],
'expected no errors from the callback during collection'
);
const scopeMetrics = resourceMetrics.scopeMetrics;
const metric = scopeMetrics[0].metrics.find(
x => x.descriptor.name === 'nodejs.' + metricName.name
);

assert.notEqual(metric, undefined, `nodejs.${metricName.name} not found`);

assert.strictEqual(
metric!.dataPointType,
DataPointType.GAUGE,
'expected gauge'
);

assert.strictEqual(
metric!.descriptor.name,
'nodejs.' + metricName.name,
'descriptor.name'
);
});
}
});
Loading

0 comments on commit 046072e

Please sign in to comment.