Skip to content

Commit

Permalink
test(instrumentation-runtime-node):fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pikalovArtemN committed May 3, 2024
1 parent 184b212 commit 922d677
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
import {RuntimeNodeInstrumentationConfig} from '../types';
import {Meter} from '@opentelemetry/api';
import * as perf_hooks from 'node:perf_hooks';
import {version} from 'node:process';
import {IntervalHistogram} from 'node:perf_hooks';
import {BaseCollector} from './baseCollector';
import {NODE_JS_VERSION_ATTRIBUTE} from "../consts/attributes";

const NODEJS_EVENTLOOP_LAG = 'eventloop.lag';
const NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE = 'eventloop.lag.type';
export const NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE = 'eventloop.lag.type';


export interface EventLoopLagInformation {
Expand Down Expand Up @@ -68,14 +66,11 @@ export class EventLoopLagCollector extends BaseCollector<EventLoopLagInformation
}, start);
});

observableResult.observe(lagResult, {
[NODE_JS_VERSION_ATTRIBUTE]: version
});
observableResult.observe(lagResult);

for (const [value, attributeType] of Object.keys(data).entries()) {
observableResult.observe(value, {
[NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE]: attributeType,
[`${this.namePrefix}.${NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE}`]: version
[`${this.namePrefix}.${NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE}`]: attributeType
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';
import {MeterProvider, DataPointType} from '@opentelemetry/sdk-metrics';

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

const MEASUREMENT_INTERVAL = 10;
const attributesToCheck = ['min', 'max', 'mean', 'stddev', 'p50', 'p90', 'p99']

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

Expand All @@ -32,8 +33,46 @@ describe('nodejs.event_loop.lag', function () {
meterProvider.addMetricReader(metricReader);
});

for (const metricName of metricNames) {
it(`should write nodejs.${metricName.name} after monitoringPrecision`, async function () {
it(`should write jsruntime.eventloop.lag 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 === 'jsruntime.eventloop.lag'
);

assert.notEqual(metric, undefined, `jsruntime.eventloop.lag not found`);

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

assert.strictEqual(
metric!.descriptor.name,
'jsruntime.eventloop.lag',
'descriptor.name'
);
});
for (const attribute of attributesToCheck) {
it(`should write jsruntime.eventloop.lag ${attribute} attribute`, async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
monitoringPrecision: MEASUREMENT_INTERVAL,
Expand All @@ -44,7 +83,7 @@ describe('nodejs.event_loop.lag', function () {
await new Promise(resolve =>
setTimeout(resolve, MEASUREMENT_INTERVAL * 5)
);
const { resourceMetrics, errors } = await metricReader.collect();
const {resourceMetrics, errors} = await metricReader.collect();

// assert
assert.deepEqual(
Expand All @@ -54,22 +93,12 @@ describe('nodejs.event_loop.lag', function () {
);
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'
x => x.descriptor.name === 'jsruntime.eventloop.lag'
);

assert.strictEqual(
metric!.descriptor.name,
'nodejs.' + metricName.name,
'descriptor.name'
);
const metricAttribute = metric!.dataPoints.find(point => point.attributes[`jsruntime.${NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE}`] === attribute)
assert.notEqual(metricAttribute, undefined, `jsruntime.${NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE} with ${attribute} attribute not found`);
});
}

});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TestMetricReader } from './testMetricsReader';

const MEASUREMENT_INTERVAL = 10;

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

Expand Down Expand Up @@ -68,7 +68,7 @@ describe('nodejs.event_loop.utilization', function () {
);
const scopeMetrics = resourceMetrics.scopeMetrics;
const utilizationMetric = scopeMetrics[0].metrics.find(
x => x.descriptor.name === 'nodejs.event_loop.utilization'
x => x.descriptor.name === 'jsruntime.eventloop.utilization'
);

assert.notEqual(utilizationMetric, undefined, 'metric not found');
Expand All @@ -81,7 +81,7 @@ describe('nodejs.event_loop.utilization', function () {

assert.strictEqual(
utilizationMetric!.descriptor.name,
'nodejs.event_loop.utilization',
'jsruntime.eventloop.utilization',
'descriptor.name'
);

Expand All @@ -92,7 +92,7 @@ describe('nodejs.event_loop.utilization', function () {

assert.strictEqual(
utilizationMetric!.descriptor.unit,
'1',
's',
'expected default unit'
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +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'
);
});
}
});
// /*
// * 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'
// );
// });
// }
// });
Loading

0 comments on commit 922d677

Please sign in to comment.