Skip to content

Commit

Permalink
test(instrumentation-runtime-node): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pikalovArtemN committed May 17, 2024
1 parent 3cba596 commit 54858de
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,40 @@ enum NodeJsEventLoopDelay {
}

export const metricNames: Record<NodeJsEventLoopDelay, { description: string }> = {
[NodeJsEventLoopDelay.delay]:
{
[NodeJsEventLoopDelay.delay]: {
description:
'Lag of event loop in seconds.'
},
[NodeJsEventLoopDelay.min]:
{
[NodeJsEventLoopDelay.min]: {
description:
'The minimum recorded event loop delay.',
},
[NodeJsEventLoopDelay.max]:
{
[NodeJsEventLoopDelay.max]: {
description:
'The maximum recorded event loop delay.',
}
,
[NodeJsEventLoopDelay.mean]:
{
[NodeJsEventLoopDelay.mean]: {
description:
'The mean of the recorded event loop delays.',
}
,
[NodeJsEventLoopDelay.stddev]:
{
[NodeJsEventLoopDelay.stddev]: {
description:
'The standard deviation of the recorded event loop delays.',
}
,
[NodeJsEventLoopDelay.p50]:
{
[NodeJsEventLoopDelay.p50]: {
description:
'The 50th percentile of the recorded event loop delays.',
}
,
[NodeJsEventLoopDelay.p90]:
{
[NodeJsEventLoopDelay.p90]: {
description:
'The 90th percentile of the recorded event loop delays.',
}
,
[NodeJsEventLoopDelay.p99]:
{
[NodeJsEventLoopDelay.p99]: {

description:
'The 99th percentile of the recorded event loop delays.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { BaseCollector } from './baseCollector';

const { eventLoopUtilization: eventLoopUtilizationCollector } = performance;

const NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';
export const NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';

export class EventLoopUtilizationCollector extends BaseCollector<EventLoopUtilization> {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { Meter } from '@opentelemetry/api';
import { BaseCollector } from './baseCollector';
import {HeapSizes} from "../types/heapSizes";

const NODEJS_HEAP_SIZE = 'heap.size';
const NODEJS_HEAP_SIZE_STATE = 'heap.size.state';
export const V8_HEAP_SIZE = 'heap.size';
const V8_HEAP_SIZE_STATE = 'heap.size.state';

export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage> {
constructor(
Expand All @@ -31,7 +31,7 @@ export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage>

updateMetricInstruments(meter: Meter): void {
meter.createObservableGauge(
`${this.namePrefix}.${NODEJS_HEAP_SIZE}`,
`${this.namePrefix}.${V8_HEAP_SIZE}`,
{
description: "Process heap size from Node.js in bytes.",
unit: 'By',
Expand All @@ -42,11 +42,11 @@ export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage>
const data = this._scrapeQueue.shift();
if (data === undefined) return;
observableResult.observe(data.heapTotal, {
[`${this.namePrefix}.${NODEJS_HEAP_SIZE_STATE}`]: HeapSizes.Total,
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE}`]: HeapSizes.Total,
...this.versionAttribute
});
observableResult.observe(data.heapUsed, {
[`${this.namePrefix}.${NODEJS_HEAP_SIZE_STATE}`]: HeapSizes.Used,
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE}`]: HeapSizes.Used,
...this.versionAttribute
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import {Meter} from '@opentelemetry/api';
import {BaseCollector} from './baseCollector';
import * as v8 from 'node:v8';
import {HeapSpaceInfo} from 'v8';
import {HeapSpaces} from "../types/heapSpaces";


enum V8HeapSpaceMetrics {
size = 'heap.size',
spaceSize = 'heap.space_size',
used = 'heap.space_used_size',
available = 'heap.space_available_size',
Expand All @@ -34,10 +32,6 @@ export const metricNames: Record<V8HeapSpaceMetrics, { description: string }> =
description:
'Process heap space size total from Node.js in bytes.',
},
[V8HeapSpaceMetrics.size]: {
description:
'Process heap space size total from Node.js in bytes.',
},
[V8HeapSpaceMetrics.used]: {
description:
'Process heap space size used from Node.js in bytes.',
Expand Down Expand Up @@ -70,13 +64,6 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
unit: 'bytes',
}
);
const heapSize = meter.createObservableGauge(
`${this.namePrefix}.${V8HeapSpaceMetrics.size}`,
{
description: metricNames[V8HeapSpaceMetrics.size].description,
unit: 'bytes',
}
);
const heapSpaceUsed = meter.createObservableGauge(
`${this.namePrefix}.${V8HeapSpaceMetrics.used}`,
{
Expand All @@ -99,8 +86,6 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
}
);
const heapSpaceNameAttributeName = `${this.namePrefix}.heap.space.name`
const heapSizeStateAttributeName = `${this.namePrefix}.heap.size.state`


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

const spaceName = space.space_name

observableResult.observe(heapSize, space.space_size, {
[heapSizeStateAttributeName]: HeapSpaces.Total,
});

observableResult.observe(heapSize, space.space_used_size, {
[heapSizeStateAttributeName]: HeapSpaces.Used,
});

observableResult.observe(heapSpaceSize, space.space_size, {
[heapSpaceNameAttributeName]: spaceName,
});
Expand All @@ -141,7 +118,7 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
});
}
},
[heapSize, heapSpaceSize, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical]
[heapSpaceSize, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical]
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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/eventLoopDelayCollector';
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";

const MEASUREMENT_INTERVAL = 10;

describe(`${ConventionalNamePrefix.NodeJsRuntime}.eventloop`, function () {
let metricReader: TestMetricReader;
let meterProvider: MeterProvider;

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

for (const metricName in metricNames) {
it(`should write ${ConventionalNamePrefix.NodeJsRuntime}.${metricName} 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 === `${ConventionalNamePrefix.NodeJsRuntime}.${ metricName}`
);

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

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

assert.strictEqual(
metric!.descriptor.name,
`${ConventionalNamePrefix.NodeJsRuntime}.${ metricName}`,
'descriptor.name'
);
});
}
});
104 changes: 0 additions & 104 deletions plugins/node/instrumentation-runtime-node/test/event_loop_lag.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import { MeterProvider, DataPointType } from '@opentelemetry/sdk-metrics';
import { RuntimeNodeInstrumentation } from '../src';
import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
import {NODEJS_EVENT_LOOP_UTILIZATION} from "../src/metrics/eventLoopUtilizationCollector";

const MEASUREMENT_INTERVAL = 10;

describe('jsruntime.eventloop.utilization', function () {
describe(`${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION}`, function () {
let metricReader: TestMetricReader;
let meterProvider: MeterProvider;

Expand Down Expand Up @@ -68,7 +70,7 @@ describe('jsruntime.eventloop.utilization', function () {
);
const scopeMetrics = resourceMetrics.scopeMetrics;
const utilizationMetric = scopeMetrics[0].metrics.find(
x => x.descriptor.name === 'jsruntime.eventloop.utilization'
x => x.descriptor.name === `${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION}`
);

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

assert.strictEqual(
utilizationMetric!.descriptor.name,
'jsruntime.eventloop.utilization',
`${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION}`,
'descriptor.name'
);

Expand Down
Loading

0 comments on commit 54858de

Please sign in to comment.