Skip to content

Commit 80d0c74

Browse files
feat(instrumentation-runtime-node)!: add prom-client-metrics (#2136)
1 parent 6234918 commit 80d0c74

21 files changed

+2291
-134
lines changed

package-lock.json

Lines changed: 1141 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/instrumentation-runtime-node/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const prometheusExporter = new PrometheusExporter({
3232
const sdk = new NodeSDK({
3333
metricReader: prometheusExporter,
3434
instrumentations: [new RuntimeNodeInstrumentation({
35-
eventLoopUtilizationMeasurementInterval: 5000,
35+
monitoringPrecision: 5000,
3636
})],
3737
});
3838

@@ -50,7 +50,7 @@ Go to [`localhost:9464/metrics`](http://localhost:9464/metrics), and you should
5050
nodejs_performance_event_loop_utilization 0.010140079547955264
5151
```
5252

53-
> Metrics will only be exported after it has collected two ELU readings (at least approximately `RuntimeNodeInstrumentationConfig.eventLoopUtilizationMeasurementInterval` milliseconds after initialization). Otherwise, you may see:
53+
> Metrics will only be exported after it has collected two ELU readings (at least approximately `RuntimeNodeInstrumentationConfig.monitoringPrecision` milliseconds after initialization). Otherwise, you may see:
5454
>
5555
> ```txt
5656
> # no registered metrics
@@ -61,8 +61,8 @@ nodejs_performance_event_loop_utilization 0.010140079547955264
6161
`RuntimeNodeInstrumentation`'s constructor accepts the following options:
6262
6363
| name | type | unit | default | description |
64-
|---|---|---|---|---|
65-
| [`eventLoopUtilizationMeasurementInterval`](./src/types.ts#L25) | `int` | millisecond | `5000` | The approximate number of milliseconds for which to calculate event loop utilization averages. A larger value will result in more accurate averages at the expense of less granular data. Should be set to below the scrape interval of your metrics collector to avoid duplicated data points. |
64+
|---|---|---|---------|---|
65+
| [`monitoringPrecision`](./src/types.ts#L25) | `int` | millisecond | `10` | The approximate number of milliseconds for which to calculate event loop utilization averages. A larger value will result in more accurate averages at the expense of less granular data. Should be set to below the scrape interval of your metrics collector to avoid duplicated data points. |
6666
6767
## Useful links
6868

plugins/node/instrumentation-runtime-node/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"author": "OpenTelemetry Authors",
2020
"license": "Apache-2.0",
2121
"engines": {
22-
"node": ">=14.10.0"
22+
"node": ">=17.4.0"
2323
},
2424
"keywords": [
2525
"perf_hooks",
@@ -44,7 +44,8 @@
4444
"@opentelemetry/api": "^1.3.0",
4545
"@opentelemetry/sdk-metrics": "^1.20.0",
4646
"@types/mocha": "^10.0.6",
47-
"@types/node": "^20.11.2",
47+
"@types/node": "18.18.14",
48+
"mocha": "7.2.0",
4849
"nyc": "^15.1.0",
4950
"rimraf": "5.0.10",
5051
"typescript": "4.4.4"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
export const ATTR_V8JS_HEAP_SPACE_NAME = 'heap.space.name';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { Histogram } from 'perf_hooks';
17+
18+
declare module 'node:perf_hooks' {
19+
interface IntervalHistogram extends Histogram {
20+
/**
21+
* Enables the update interval timer. Returns `true` if the timer was
22+
* started, `false` if it was already started.
23+
* @since v11.10.0
24+
*/
25+
enable(): boolean;
26+
27+
/**
28+
* Disables the update interval timer. Returns `true` if the timer was
29+
* stopped, `false` if it was already stopped.
30+
* @since v11.10.0
31+
*/
32+
disable(): boolean;
33+
34+
count: number;
35+
}
36+
}

plugins/node/instrumentation-runtime-node/src/instrumentation.ts

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,83 +13,76 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { EventLoopUtilization, performance } from 'node:perf_hooks';
17-
const { eventLoopUtilization } = performance;
18-
1916
import { InstrumentationBase } from '@opentelemetry/instrumentation';
2017

21-
/** @knipignore */
22-
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
2318
import { RuntimeNodeInstrumentationConfig } from './types';
19+
import { MetricCollector } from './types/metricCollector';
20+
import { EventLoopUtilizationCollector } from './metrics/eventLoopUtilizationCollector';
21+
import { EventLoopDelayCollector } from './metrics/eventLoopDelayCollector';
22+
import { GCCollector } from './metrics/gcCollector';
23+
import { HeapSpacesSizeAndUsedCollector } from './metrics/heapSpacesSizeAndUsedCollector';
24+
import { ConventionalNamePrefix } from './types/ConventionalNamePrefix';
25+
import { EventLoopTimeCollector } from './metrics/eventLoopTimeCollector';
26+
/** @knipignore */
27+
import { PACKAGE_VERSION, PACKAGE_NAME } from './version';
2428

25-
const ELUS_LENGTH = 2;
2629
const DEFAULT_CONFIG: RuntimeNodeInstrumentationConfig = {
27-
eventLoopUtilizationMeasurementInterval: 5000,
30+
monitoringPrecision: 10,
2831
};
2932

3033
export class RuntimeNodeInstrumentation extends InstrumentationBase<RuntimeNodeInstrumentationConfig> {
31-
private _ELUs: EventLoopUtilization[] = [];
32-
private _interval: NodeJS.Timeout | undefined;
34+
private readonly _collectors: MetricCollector[] = [];
3335

3436
constructor(config: RuntimeNodeInstrumentationConfig = {}) {
3537
super(
3638
PACKAGE_NAME,
3739
PACKAGE_VERSION,
3840
Object.assign({}, DEFAULT_CONFIG, config)
3941
);
40-
}
41-
42-
private _addELU() {
43-
this._ELUs.unshift(eventLoopUtilization());
44-
if (this._ELUs.length > ELUS_LENGTH) {
45-
this._ELUs.pop();
46-
}
47-
}
48-
49-
private _clearELU() {
50-
if (!this._ELUs) {
51-
this._ELUs = [];
42+
this._collectors = [
43+
new EventLoopUtilizationCollector(
44+
this._config,
45+
ConventionalNamePrefix.NodeJs
46+
),
47+
new EventLoopTimeCollector(this._config, ConventionalNamePrefix.NodeJs),
48+
new EventLoopDelayCollector(this._config, ConventionalNamePrefix.NodeJs),
49+
new GCCollector(this._config, ConventionalNamePrefix.V8js),
50+
new HeapSpacesSizeAndUsedCollector(
51+
this._config,
52+
ConventionalNamePrefix.V8js
53+
),
54+
];
55+
if (this._config.enabled) {
56+
for (const collector of this._collectors) {
57+
collector.enable();
58+
}
5259
}
53-
this._ELUs.length = 0;
5460
}
5561

5662
// Called when a new `MeterProvider` is set
5763
// the Meter (result of @opentelemetry/api's getMeter) is available as this.meter within this method
5864
override _updateMetricInstruments() {
59-
this.meter
60-
.createObservableGauge('nodejs.event_loop.utilization', {
61-
description: 'Event loop utilization',
62-
unit: '1',
63-
})
64-
.addCallback(async observableResult => {
65-
if (this._ELUs.length !== ELUS_LENGTH) {
66-
return;
67-
}
68-
const elu = eventLoopUtilization(...this._ELUs);
69-
observableResult.observe(elu.utilization);
70-
});
65+
if (!this._collectors) return;
66+
for (const collector of this._collectors) {
67+
collector.updateMetricInstruments(this.meter);
68+
}
7169
}
7270

7371
init() {
7472
// Not instrumenting or patching a Node.js module
7573
}
7674

7775
override enable() {
78-
this._clearELU();
79-
this._addELU();
80-
clearInterval(this._interval);
81-
this._interval = setInterval(
82-
() => this._addELU(),
83-
this.getConfig().eventLoopUtilizationMeasurementInterval
84-
);
76+
if (!this._collectors) return;
8577

86-
// unref so that it does not keep the process running if disable() is never called
87-
this._interval?.unref();
78+
for (const collector of this._collectors) {
79+
collector.enable();
80+
}
8881
}
8982

9083
override disable() {
91-
this._clearELU();
92-
clearInterval(this._interval);
93-
this._interval = undefined;
84+
for (const collector of this._collectors) {
85+
collector.disable();
86+
}
9487
}
9588
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { MetricCollector } from '../types/metricCollector';
17+
import { Meter } from '@opentelemetry/api';
18+
import { RuntimeNodeInstrumentationConfig } from '../types';
19+
20+
export abstract class BaseCollector implements MetricCollector {
21+
protected _config: RuntimeNodeInstrumentationConfig = {};
22+
23+
protected namePrefix: string;
24+
25+
protected constructor(
26+
config: RuntimeNodeInstrumentationConfig = {},
27+
namePrefix: string
28+
) {
29+
this._config = config;
30+
this.namePrefix = namePrefix;
31+
}
32+
33+
public disable(): void {
34+
this._config.enabled = false;
35+
this.internalDisable();
36+
}
37+
38+
public enable(): void {
39+
this._config.enabled = true;
40+
this.internalEnable();
41+
}
42+
43+
public abstract updateMetricInstruments(meter: Meter): void;
44+
45+
protected abstract internalEnable(): void;
46+
47+
protected abstract internalDisable(): void;
48+
}

0 commit comments

Comments
 (0)