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 54858de commit 926f052
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const NODE_JS_VERSION_ATTRIBUTE = "nodejsruntime.version"
export const V8_HEAP_SIZE_STATE_ATTRIBUTE = 'heap.size.state';
export const V8_HEAP_SIZE = 'heap.size';
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const metricNames: Record<NodeJsEventLoopDelay, { description: string }>
}
,
[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 @@ -17,9 +17,8 @@ import { RuntimeNodeInstrumentationConfig } from '../types';
import { Meter } from '@opentelemetry/api';
import { BaseCollector } from './baseCollector';
import {HeapSizes} from "../types/heapSizes";
import {V8_HEAP_SIZE, V8_HEAP_SIZE_STATE_ATTRIBUTE} from "../consts/attributes";

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 @@ -42,11 +41,11 @@ export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage>
const data = this._scrapeQueue.shift();
if (data === undefined) return;
observableResult.observe(data.heapTotal, {
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE}`]: HeapSizes.Total,
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE_ATTRIBUTE}`]: HeapSizes.Total,
...this.versionAttribute
});
observableResult.observe(data.heapUsed, {
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE}`]: HeapSizes.Used,
[`${this.namePrefix}.${V8_HEAP_SIZE_STATE_ATTRIBUTE}`]: HeapSizes.Used,
...this.versionAttribute
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ import {Meter} from '@opentelemetry/api';
import {BaseCollector} from './baseCollector';
import * as v8 from 'node:v8';
import {HeapSpaceInfo} from 'v8';
import {V8_HEAP_SIZE_STATE_ATTRIBUTE} from "../consts/attributes";


enum V8HeapSpaceMetrics {
export enum V8HeapSpaceMetrics {
spaceSize = 'heap.space_size',
used = 'heap.space_used_size',
available = 'heap.space_available_size',
physical = 'heap.physical_space_size',
}



export const metricNames: Record<V8HeapSpaceMetrics, { description: string }> = {
[V8HeapSpaceMetrics.spaceSize]: {
description:
Expand Down Expand Up @@ -85,7 +88,7 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
unit: 'bytes',
}
);
const heapSpaceNameAttributeName = `${this.namePrefix}.heap.space.name`
const heapSpaceNameAttributeName = `${this.namePrefix}.${V8_HEAP_SIZE_STATE_ATTRIBUTE}`

meter.addBatchObservableCallback(
observableResult => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import { metricNames } from '../src/metrics/eventLoopDelayCollector';
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
import {NODE_JS_VERSION_ATTRIBUTE} from "../src/consts/attributes";

const MEASUREMENT_INTERVAL = 10;

Expand Down Expand Up @@ -72,5 +73,36 @@ describe(`${ConventionalNamePrefix.NodeJsRuntime}.eventloop`, function () {
'descriptor.name'
);
});

it(`should write ${ConventionalNamePrefix.NodeJsRuntime}.${metricName} version attribute`, 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.strictEqual(
metric!.dataPoints[0].attributes[NODE_JS_VERSION_ATTRIBUTE],
process.version,
`version attribute ${NODE_JS_VERSION_ATTRIBUTE} not found`
);
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
import {NODEJS_EVENT_LOOP_UTILIZATION} from "../src/metrics/eventLoopUtilizationCollector";
import {NODE_JS_VERSION_ATTRIBUTE} from "../src/consts/attributes";

const MEASUREMENT_INTERVAL = 10;

Expand Down Expand Up @@ -51,7 +52,7 @@ describe(`${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATIO
assert.strictEqual(scopeMetrics.length, 0);
});

it('should write event loop utilization metrics after monitoringPrecision', async function () {
it(`should write ${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION} after monitoringPrecision`, async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
monitoringPrecision: MEASUREMENT_INTERVAL,
Expand Down Expand Up @@ -108,4 +109,35 @@ describe(`${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATIO
assert.strictEqual(val > 0, true, `val (${val}) > 0`);
assert.strictEqual(val <= 1, true, `val (${val}) <= 1`);
});

it(`should write ${ConventionalNamePrefix.NodeJsRuntime}.${NODEJS_EVENT_LOOP_UTILIZATION} version attribute`, 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}.${NODEJS_EVENT_LOOP_UTILIZATION}`
);

assert.strictEqual(
metric!.dataPoints[0].attributes[NODE_JS_VERSION_ATTRIBUTE],
process.version,
`version attribute ${NODE_JS_VERSION_ATTRIBUTE} not found`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {RuntimeNodeInstrumentation} from '../src';
import * as assert from 'assert';
import {TestMetricReader} from './testMetricsReader';
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
import {V8_HEAP_SIZE} from "../src/metrics/heapSizeAndUsedCollector";
import {NODE_JS_VERSION_ATTRIBUTE, V8_HEAP_SIZE, V8_HEAP_SIZE_STATE_ATTRIBUTE} from "../src/consts/attributes";
import {HeapSizes} from "../src/types/heapSizes";

const MEASUREMENT_INTERVAL = 10;

Expand Down Expand Up @@ -71,4 +72,97 @@ describe(`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE}`, function (
'descriptor.name'
);
});

it(`should write ${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE} version attribute`, 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.V8EnjineRuntime}.${V8_HEAP_SIZE}`
);

assert.strictEqual(
metric!.dataPoints[0].attributes[NODE_JS_VERSION_ATTRIBUTE],
process.version,
`version attribute ${NODE_JS_VERSION_ATTRIBUTE} not found`
);
});

it(`should write ${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE} ${HeapSizes.Total} attribute`, 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.V8EnjineRuntime}.${V8_HEAP_SIZE}`
);

assert.strictEqual(
metric!.dataPoints[0].attributes[`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE_STATE_ATTRIBUTE}`],
HeapSizes.Total,
`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE_STATE_ATTRIBUTE} attribute ${NODE_JS_VERSION_ATTRIBUTE} not found`
);
});

it(`should write ${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE} ${HeapSizes.Used} attribute`, 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.V8EnjineRuntime}.${V8_HEAP_SIZE}`
);

assert.strictEqual(
metric!.dataPoints[1].attributes[`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE_STATE_ATTRIBUTE}`],
HeapSizes.Used,
`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE_STATE_ATTRIBUTE} attribute not found`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as assert from 'assert';
import { TestMetricReader } from './testMetricsReader';
import { metricNames } from '../src/metrics/heapSpacesSizeAndUsedCollector';
import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix";
import {V8_HEAP_SIZE_STATE_ATTRIBUTE} from "../src/consts/attributes";

const MEASUREMENT_INTERVAL = 10;

Expand Down Expand Up @@ -72,5 +73,36 @@ describe('nodejs.heap_space', function () {
'descriptor.name'
);
});

for (const space of ["new_space", "old_space", "code_space", "large_object_space"]) {
it(`should write ${ConventionalNamePrefix.V8EnjineRuntime}.${metricName} ${space} attribute`, async function () {
// arrange
const instrumentation = new RuntimeNodeInstrumentation({
monitoringPrecision: MEASUREMENT_INTERVAL,
});
instrumentation.setMeterProvider(meterProvider);
const map = [...Array(10).keys()].map(x => x + 10)
map.indexOf(1)
// 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.V8EnjineRuntime}.${metricName}`
);
const spaceAttribute = metric!.dataPoints.find(x => x.attributes[`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE_STATE_ATTRIBUTE}`] === space)

assert.notEqual(spaceAttribute, undefined, `${ConventionalNamePrefix.V8EnjineRuntime}.${metricName} space: ${space} not found`);
});
}
}
});

0 comments on commit 926f052

Please sign in to comment.