diff --git a/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopDelayCollector.ts b/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopDelayCollector.ts index b970806f89..273f2bb1f4 100644 --- a/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopDelayCollector.ts +++ b/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopDelayCollector.ts @@ -31,48 +31,40 @@ enum NodeJsEventLoopDelay { } export const metricNames: Record = { - [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.', diff --git a/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopUtilizationCollector.ts b/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopUtilizationCollector.ts index 80e42aef97..35114c41fd 100644 --- a/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopUtilizationCollector.ts +++ b/plugins/node/instrumentation-runtime-node/src/metrics/eventLoopUtilizationCollector.ts @@ -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 { constructor( diff --git a/plugins/node/instrumentation-runtime-node/src/metrics/heapSizeAndUsedCollector.ts b/plugins/node/instrumentation-runtime-node/src/metrics/heapSizeAndUsedCollector.ts index d35cd66eb5..f1cac3fd9d 100644 --- a/plugins/node/instrumentation-runtime-node/src/metrics/heapSizeAndUsedCollector.ts +++ b/plugins/node/instrumentation-runtime-node/src/metrics/heapSizeAndUsedCollector.ts @@ -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 { constructor( @@ -31,7 +31,7 @@ export class HeapSizeAndUsedCollector extends BaseCollector 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', @@ -42,11 +42,11 @@ export class HeapSizeAndUsedCollector extends BaseCollector 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 }); }); diff --git a/plugins/node/instrumentation-runtime-node/src/metrics/heapSpacesSizeAndUsedCollector.ts b/plugins/node/instrumentation-runtime-node/src/metrics/heapSpacesSizeAndUsedCollector.ts index 711f5c1559..f563220f3a 100644 --- a/plugins/node/instrumentation-runtime-node/src/metrics/heapSpacesSizeAndUsedCollector.ts +++ b/plugins/node/instrumentation-runtime-node/src/metrics/heapSpacesSizeAndUsedCollector.ts @@ -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', @@ -34,10 +32,6 @@ export const metricNames: Record = 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.', @@ -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}`, { @@ -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 => { @@ -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, }); @@ -141,7 +118,7 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector< }); } }, - [heapSize, heapSpaceSize, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical] + [heapSpaceSize, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical] ); } diff --git a/plugins/node/instrumentation-runtime-node/test/event_loop_delay.test.ts b/plugins/node/instrumentation-runtime-node/test/event_loop_delay.test.ts new file mode 100644 index 0000000000..cb34a61403 --- /dev/null +++ b/plugins/node/instrumentation-runtime-node/test/event_loop_delay.test.ts @@ -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' + ); + }); + } +}); diff --git a/plugins/node/instrumentation-runtime-node/test/event_loop_lag.test.ts b/plugins/node/instrumentation-runtime-node/test/event_loop_lag.test.ts deleted file mode 100644 index d56cc5d0f5..0000000000 --- a/plugins/node/instrumentation-runtime-node/test/event_loop_lag.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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 {NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE} from "../src/metrics/eventLoopLagCollector"; - -const MEASUREMENT_INTERVAL = 10; -const attributesToCheck = ['min', 'max', 'mean', 'stddev', 'p50', 'p90', 'p99'] - -describe('jsruntime.eventloop.lag', function () { - let metricReader: TestMetricReader; - let meterProvider: MeterProvider; - - beforeEach(() => { - metricReader = new TestMetricReader(); - meterProvider = new MeterProvider(); - meterProvider.addMetricReader(metricReader); - }); - - 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, - }); - 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' - ); - - 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`); - }); - } - -}); diff --git a/plugins/node/instrumentation-runtime-node/test/event_loop_utilization.test.ts b/plugins/node/instrumentation-runtime-node/test/event_loop_utilization.test.ts index 5cc1e45300..95b6d2699b 100644 --- a/plugins/node/instrumentation-runtime-node/test/event_loop_utilization.test.ts +++ b/plugins/node/instrumentation-runtime-node/test/event_loop_utilization.test.ts @@ -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; @@ -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'); @@ -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' ); diff --git a/plugins/node/instrumentation-runtime-node/test/heap_size_and_used.test.ts b/plugins/node/instrumentation-runtime-node/test/heap_size_and_used.test.ts index 9fee71fd5c..22f4e68855 100644 --- a/plugins/node/instrumentation-runtime-node/test/heap_size_and_used.test.ts +++ b/plugins/node/instrumentation-runtime-node/test/heap_size_and_used.test.ts @@ -1,75 +1,74 @@ -// /* -// * 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 {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix"; +import {V8_HEAP_SIZE} from "../src/metrics/heapSizeAndUsedCollector"; + +const MEASUREMENT_INTERVAL = 10; + +describe(`${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE}`, function () { + let metricReader: TestMetricReader; + let meterProvider: MeterProvider; + + beforeEach(() => { + metricReader = new TestMetricReader(); + meterProvider = new MeterProvider(); + meterProvider.addMetricReader(metricReader); + }); + + it(`should write ${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE} 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.V8EnjineRuntime}.${V8_HEAP_SIZE}` + ); + + assert.notEqual(metric, undefined, `${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE} not found`); + + assert.strictEqual( + metric!.dataPointType, + DataPointType.GAUGE, + 'expected gauge' + ); + + assert.strictEqual( + metric!.descriptor.name, + `${ConventionalNamePrefix.V8EnjineRuntime}.${V8_HEAP_SIZE}`, + 'descriptor.name' + ); + }); +}); diff --git a/plugins/node/instrumentation-runtime-node/test/heap_space_and_used.test.ts b/plugins/node/instrumentation-runtime-node/test/heap_space_and_used.test.ts index 3032c273fa..25db972330 100644 --- a/plugins/node/instrumentation-runtime-node/test/heap_space_and_used.test.ts +++ b/plugins/node/instrumentation-runtime-node/test/heap_space_and_used.test.ts @@ -1,75 +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/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' -// ); -// }); -// } -// }); +/* + * 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'; +import {ConventionalNamePrefix} from "../src/types/ConventionalNamePrefix"; + +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 in metricNames) { + it(`should write ${ConventionalNamePrefix.V8EnjineRuntime}.${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.V8EnjineRuntime}.${metricName}` + ); + + assert.notEqual(metric, undefined, `${ConventionalNamePrefix.V8EnjineRuntime}.${metricName} not found`); + + assert.strictEqual( + metric!.dataPointType, + DataPointType.GAUGE, + 'expected gauge' + ); + + assert.strictEqual( + metric!.descriptor.name, + `${ConventionalNamePrefix.V8EnjineRuntime}.${metricName}`, + 'descriptor.name' + ); + }); + } +});