Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
klippx committed Oct 11, 2023
1 parent 2110967 commit 8b7966f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 69 deletions.
7 changes: 4 additions & 3 deletions plugins/node/instrumentation-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ registerInstrumentations({

Runtime instrumentation has currently one option. You can set the following:

| Options | Type | Description |
| --------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `monitorEventLoopDelayResolution` | `number` | Sampling rate for data collection, in milliseconds. [perf_hooks.monitorEventLoopDelay](https://nodejs.org/api/perf_hooks.html#perf_hooksmonitoreventloopdelayoptions) |
| Options | Type | Description |
| --------------------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `monitorEventLoopDelayResolution` | `number` | Sampling rate for data collection, in milliseconds. [perf_hooks.monitorEventLoopDelay](https://nodejs.org/api/perf_hooks.html#perf_hooksmonitoreventloopdelayoptions) |
| `customMetricAttributes` | `() => Attributes` | Function for adding custom metric attributes on all recorded metrics |

## Useful links

Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"build/src/**/*.js",
"build/src/**/*.js.map",
"build/src/**/*.d.ts",
"doc",
"LICENSE",
"README.md"
],
Expand All @@ -59,6 +58,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.43.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
Expand Down
32 changes: 0 additions & 32 deletions plugins/node/instrumentation-runtime/src/enums/AttributeNames.ts

This file was deleted.

32 changes: 32 additions & 0 deletions plugins/node/instrumentation-runtime/src/enums/GaugeNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/

/**
* https://github.com/open-telemetry/semantic-conventions/blob/main/docs/system/runtime-environment-metrics.md
*/
export enum GaugeNames {
NODE_EVENT_LOOP_DELAY = 'nodejs.event_loop_delay',
NODE_EVENT_LOOP_DELAY_MIN = 'nodejs.event_loop_delay.min',
NODE_EVENT_LOOP_DELAY_MAX = 'nodejs.event_loop_delay.max',
NODE_EVENT_LOOP_DELAY_MEAN = 'nodejs.event_loop_delay.mean',
NODE_EVENT_LOOP_DELAY_STDDEV = 'nodejs.event_loop_delay.stddev',
NODE_EVENT_LOOP_DELAY_P50 = 'nodejs.event_loop_delay.p50',
NODE_EVENT_LOOP_DELAY_P95 = 'nodejs.event_loop_delay.p95',
NODE_EVENT_LOOP_DELAY_P99 = 'nodejs.event_loop_delay.p99',
NODE_EVENT_LOOP_UTILIZATION = 'nodejs.event_loop_utilization',
NODE_EVENT_LOOP_UTILIZATION_IDLE = 'nodejs.event_loop_utilization.idle',
NODE_EVENT_LOOP_UTILIZATION_ACTIVE = 'nodejs.event_loop_utilization.active',
}
1 change: 1 addition & 0 deletions plugins/node/instrumentation-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
* limitations under the License.
*/

export * from './enums/GaugeNames';
export * from './instrumentation';
export * from './types';
55 changes: 33 additions & 22 deletions plugins/node/instrumentation-runtime/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
hrTimeDuration,
hrTimeToMilliseconds,
} from '@opentelemetry/core';
import { AttributeNames } from './enums/AttributeNames';
import { GaugeNames } from './enums/GaugeNames';
import { Attributes } from '@opentelemetry/api';

/**
Expand Down Expand Up @@ -58,46 +58,53 @@ export class RuntimeInstrumentation extends InstrumentationBase {
}

private _getCustomMetricAttributes(): Attributes {
return this.config.customMetricAttributes
? this.config.customMetricAttributes()
: {};
if (!this.config.customMetricAttributes) {
return {};
}

try {
return this.config.customMetricAttributes();
} catch (e) {
this._diag.warn('an error was thrown in customMetricAttributes', e);
return {};
}
}

protected override _updateMetricInstruments() {
const eventLoopDelayGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY,
GaugeNames.NODE_EVENT_LOOP_DELAY,
{
description: 'Delay of event loop.',
unit: 'ms',
}
);

const eventLoopDelayMinGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_MIN,
GaugeNames.NODE_EVENT_LOOP_DELAY_MIN,
{
description: 'The minimum recorded event loop delay.',
unit: 'ms',
}
);

const eventLoopDelayMaxGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_MAX,
GaugeNames.NODE_EVENT_LOOP_DELAY_MAX,
{
description: 'The maximum recorded event loop delay.',
unit: 'ms',
}
);

const eventLoopDelayMeanGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_MEAN,
GaugeNames.NODE_EVENT_LOOP_DELAY_MEAN,
{
description: 'The mean of the recorded event loop delays.',
unit: 'ms',
}
);

const eventLoopDelayStddevGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_STDDEV,
GaugeNames.NODE_EVENT_LOOP_DELAY_STDDEV,
{
description:
'The standard deviation of the recorded event loop delays.',
Expand All @@ -106,23 +113,23 @@ export class RuntimeInstrumentation extends InstrumentationBase {
);

const eventLoopDelayP50Gauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_P50,
GaugeNames.NODE_EVENT_LOOP_DELAY_P50,
{
description: 'The 50th percentile of the recorded event loop delays.',
unit: 'ms',
}
);

const eventLoopDelayP95Gauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_P95,
GaugeNames.NODE_EVENT_LOOP_DELAY_P95,
{
description: 'The 95th percentile of the recorded event loop delays.',
unit: 'ms',
}
);

const eventLoopDelayP99Gauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_DELAY_P99,
GaugeNames.NODE_EVENT_LOOP_DELAY_P99,
{
description: 'The 99th percentile of the recorded event loop delays.',
unit: 'ms',
Expand All @@ -146,44 +153,48 @@ export class RuntimeInstrumentation extends InstrumentationBase {
);
observable.observe(eventLoopDelayGauge, duration, attributes);

function nanoSecondToMilliSecond(nanoSecond: number) {
return nanoSecond / 1e6;
}

observable.observe(
eventLoopDelayMinGauge,
this.eventLoopDelayHistogram.min / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.min),
attributes
);
observable.observe(
eventLoopDelayMaxGauge,
this.eventLoopDelayHistogram.max / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.max),
attributes
);

observable.observe(
eventLoopDelayMeanGauge,
this.eventLoopDelayHistogram.mean / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.mean),
attributes
);

observable.observe(
eventLoopDelayStddevGauge,
this.eventLoopDelayHistogram.stddev / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.stddev),
attributes
);

observable.observe(
eventLoopDelayP50Gauge,
this.eventLoopDelayHistogram.percentile(50) / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.percentile(50)),
attributes
);

observable.observe(
eventLoopDelayP95Gauge,
this.eventLoopDelayHistogram.percentile(95) / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.percentile(95)),
attributes
);

observable.observe(
eventLoopDelayP99Gauge,
this.eventLoopDelayHistogram.percentile(99) / 1e6,
nanoSecondToMilliSecond(this.eventLoopDelayHistogram.percentile(99)),
attributes
);
},
Expand All @@ -200,23 +211,23 @@ export class RuntimeInstrumentation extends InstrumentationBase {
);

const loopUtilizationGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_UTILIZATION,
GaugeNames.NODE_EVENT_LOOP_UTILIZATION,
{
description: 'The percentage utilization of the event loop.',
unit: 'percent',
}
);

const loopIdleGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_UTILIZATION_IDLE,
GaugeNames.NODE_EVENT_LOOP_UTILIZATION_IDLE,
{
description: 'The idle time utilization of event loop.',
unit: 'ms',
}
);

const loopActiveGauge = this.meter.createObservableGauge(
AttributeNames.NODE_EVENT_LOOP_UTILIZATION_ACTIVE,
GaugeNames.NODE_EVENT_LOOP_UTILIZATION_ACTIVE,
{
description: 'The active time utilization of event loop.',
unit: 'ms',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ describe('metrics', () => {
eventLoopDelay.descriptor.description,
'Delay of event loop.'
);
assert.strictEqual(eventLoopDelay.descriptor.name, 'node.event_loop_delay');
assert.strictEqual(
eventLoopDelay.descriptor.name,
'nodejs.event_loop_delay'
);
assert.strictEqual(eventLoopDelay.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelay.dataPoints.length, 1);
assert.deepStrictEqual(eventLoopDelay.dataPoints[0].attributes, {
Expand All @@ -88,7 +91,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayMin.descriptor.name,
'node.event_loop_delay.min'
'nodejs.event_loop_delay.min'
);
assert.strictEqual(eventLoopDelayMin.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayMin.dataPoints.length, 1);
Expand All @@ -104,7 +107,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayMax.descriptor.name,
'node.event_loop_delay.max'
'nodejs.event_loop_delay.max'
);
assert.strictEqual(eventLoopDelayMax.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayMax.dataPoints.length, 1);
Expand All @@ -120,7 +123,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayMean.descriptor.name,
'node.event_loop_delay.mean'
'nodejs.event_loop_delay.mean'
);
assert.strictEqual(eventLoopDelayMean.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayMean.dataPoints.length, 1);
Expand All @@ -133,7 +136,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayStddev.descriptor.name,
'node.event_loop_delay.stddev'
'nodejs.event_loop_delay.stddev'
);
assert.strictEqual(eventLoopDelayStddev.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayStddev.dataPoints.length, 1);
Expand All @@ -149,7 +152,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayP50.descriptor.name,
'node.event_loop_delay.p50'
'nodejs.event_loop_delay.p50'
);
assert.strictEqual(eventLoopDelayP50.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayP50.dataPoints.length, 1);
Expand All @@ -165,7 +168,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayP95.descriptor.name,
'node.event_loop_delay.p95'
'nodejs.event_loop_delay.p95'
);
assert.strictEqual(eventLoopDelayP95.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayP95.dataPoints.length, 1);
Expand All @@ -181,7 +184,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopDelayP99.descriptor.name,
'node.event_loop_delay.p99'
'nodejs.event_loop_delay.p99'
);
assert.strictEqual(eventLoopDelayP99.descriptor.unit, 'ms');
assert.strictEqual(eventLoopDelayP99.dataPoints.length, 1);
Expand All @@ -197,7 +200,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopUtilization.descriptor.name,
'node.event_loop_utilization'
'nodejs.event_loop_utilization'
);
assert.strictEqual(eventLoopUtilization.descriptor.unit, 'percent');
assert.strictEqual(eventLoopUtilization.dataPoints.length, 1);
Expand All @@ -216,7 +219,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopUtilizationIdle.descriptor.name,
'node.event_loop_utilization.idle'
'nodejs.event_loop_utilization.idle'
);
assert.strictEqual(eventLoopUtilizationIdle.descriptor.unit, 'ms');
assert.strictEqual(eventLoopUtilizationIdle.dataPoints.length, 1);
Expand All @@ -235,7 +238,7 @@ describe('metrics', () => {
);
assert.strictEqual(
eventLoopUtilizationActive.descriptor.name,
'node.event_loop_utilization.active'
'nodejs.event_loop_utilization.active'
);
assert.strictEqual(eventLoopUtilizationActive.descriptor.unit, 'ms');
assert.strictEqual(eventLoopUtilizationActive.dataPoints.length, 1);
Expand Down

0 comments on commit 8b7966f

Please sign in to comment.