diff --git a/.github/component_owners.yml b/.github/component_owners.yml
index a3ad46b401..a8e0dc3e31 100644
--- a/.github/component_owners.yml
+++ b/.github/component_owners.yml
@@ -48,6 +48,8 @@ components:
- blumamir
plugins/node/instrumentation-mongoose:
- blumamir
+ plugins/node/instrumentation-runtime:
+ - klippx
plugins/node/instrumentation-socket.io:
- mottibec
- rauno56
diff --git a/plugins/node/instrumentation-runtime/.eslintrc.js b/plugins/node/instrumentation-runtime/.eslintrc.js
new file mode 100644
index 0000000000..0c986a856c
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/.eslintrc.js
@@ -0,0 +1,7 @@
+module.exports = {
+ env: {
+ mocha: true,
+ node: true,
+ },
+ ...require('../../../eslint.config.js'),
+};
diff --git a/plugins/node/instrumentation-runtime/CHANGELOG.md b/plugins/node/instrumentation-runtime/CHANGELOG.md
new file mode 100644
index 0000000000..825c32f0d0
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/CHANGELOG.md
@@ -0,0 +1 @@
+# Changelog
diff --git a/plugins/node/instrumentation-runtime/LICENSE b/plugins/node/instrumentation-runtime/LICENSE
new file mode 100644
index 0000000000..e50e8c80f9
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [2022] 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
+
+ http://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.
diff --git a/plugins/node/instrumentation-runtime/README.md b/plugins/node/instrumentation-runtime/README.md
new file mode 100644
index 0000000000..bcd9955fd4
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/README.md
@@ -0,0 +1,71 @@
+# OpenTelemetry runtime Instrumentation for Node.js
+
+[![NPM Published Version][npm-img]][npm-url]
+[![Apache License][license-image]][license-image]
+
+This module provides automatic instrumentation for some of the [`perf_hooks`](https://nodejs.org/api/perf_hooks.html) performance measurement.
+
+Compatible with OpenTelemetry JS API and SDK `1.0+`.
+
+## Installation
+
+```bash
+npm install --save @opentelemetry/instrumentation-runtime
+```
+
+## Usage
+
+```js
+const { MeterProvider } = require('@opentelemetry/sdk-metrics');
+const { HostMetrics } = require('@opentelemetry/host-metrics');
+const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
+const { registerInstrumentations } = require('@opentelemetry/instrumentation');
+const {
+ RuntimeInstrumentation,
+} = require('@opentelemetry/instrumentation-runtime');
+
+const exporter = new PrometheusExporter(
+ {
+ startServer: true,
+ },
+ () => {
+ console.log('prometheus scrape endpoint: http://localhost:9464/metrics');
+ }
+);
+
+const meterProvider = new MeterProvider();
+meterProvider.addMetricReader(exporter);
+
+registerInstrumentations({
+ instrumentations: [
+ new RuntimeInstrumentation({
+ // see under for available configuration
+ }),
+ ],
+});
+```
+
+### Runtime Instrumentation Options
+
+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) |
+| `customMetricAttributes` | `() => Attributes` | Function for adding custom metric attributes on all recorded metrics |
+
+## Useful links
+
+- For more information on OpenTelemetry, visit:
+- For more about OpenTelemetry JavaScript:
+- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]
+
+## License
+
+Apache 2.0 - See [LICENSE][license-url] for more information.
+
+[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
+[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE
+[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
+[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-runtime
+[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-runtime.svg
diff --git a/plugins/node/instrumentation-runtime/package.json b/plugins/node/instrumentation-runtime/package.json
new file mode 100644
index 0000000000..f900528222
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/package.json
@@ -0,0 +1,66 @@
+{
+ "name": "@opentelemetry/instrumentation-runtime",
+ "version": "0.1.0",
+ "description": "OpenTelemetry node runtime instrumentation package.",
+ "main": "build/src/index.js",
+ "types": "build/src/index.d.ts",
+ "repository": "open-telemetry/opentelemetry-js-contrib",
+ "scripts": {
+ "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
+ "tdd": "npm run test -- --watch-extensions ts --watch",
+ "clean": "rimraf build/*",
+ "lint": "eslint . --ext .ts",
+ "lint:fix": "eslint . --ext .ts --fix",
+ "precompile": "tsc --version && lerna run version:update --scope @opentelemetry/instrumentation-runtime --include-dependencies",
+ "prewatch": "npm run precompile",
+ "prepare": "npm run compile",
+ "version:update": "node ../../../scripts/version-update.js",
+ "compile": "tsc -p ."
+ },
+ "keywords": [
+ "runtime",
+ "instrumentation",
+ "nodejs",
+ "opentelemetry",
+ "profiling",
+ "tracing"
+ ],
+ "author": "OpenTelemetry Authors",
+ "license": "Apache-2.0",
+ "engines": {
+ "node": ">=14"
+ },
+ "files": [
+ "build/src/**/*.js",
+ "build/src/**/*.js.map",
+ "build/src/**/*.d.ts",
+ "LICENSE",
+ "README.md"
+ ],
+ "publishConfig": {
+ "access": "public"
+ },
+ "peerDependencies": {
+ "@opentelemetry/api": "^1.3.0"
+ },
+ "devDependencies": {
+ "@opentelemetry/api": "^1.3.0",
+ "@opentelemetry/core": "^1.17.0",
+ "@opentelemetry/sdk-metrics": "^1.8.0",
+ "@types/mocha": "7.0.2",
+ "@types/node": "18.6.5",
+ "@types/sinon": "10.0.16",
+ "mocha": "7.2.0",
+ "nyc": "15.1.0",
+ "rimraf": "5.0.1",
+ "sinon": "15.2.0",
+ "ts-mocha": "10.0.0",
+ "typescript": "4.4.4"
+ },
+ "dependencies": {
+ "@opentelemetry/core": "^1.8.0",
+ "@opentelemetry/instrumentation": "^0.43.0",
+ "@opentelemetry/semantic-conventions": "^1.0.0"
+ },
+ "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-runtime#readme"
+}
diff --git a/plugins/node/instrumentation-runtime/src/enums/GaugeNames.ts b/plugins/node/instrumentation-runtime/src/enums/GaugeNames.ts
new file mode 100644
index 0000000000..e0fd9ffedc
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/src/enums/GaugeNames.ts
@@ -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',
+}
diff --git a/plugins/node/instrumentation-runtime/src/index.ts b/plugins/node/instrumentation-runtime/src/index.ts
new file mode 100644
index 0000000000..2ae955c356
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/src/index.ts
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+
+export * from './enums/GaugeNames';
+export * from './instrumentation';
+export * from './types';
diff --git a/plugins/node/instrumentation-runtime/src/instrumentation.ts b/plugins/node/instrumentation-runtime/src/instrumentation.ts
new file mode 100644
index 0000000000..7ab924602d
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/src/instrumentation.ts
@@ -0,0 +1,266 @@
+/*
+ * 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 { InstrumentationBase } from '@opentelemetry/instrumentation';
+import { RuntimeInstrumentationConfig } from './types';
+import { VERSION } from './version';
+import {
+ EventLoopUtilization,
+ IntervalHistogram,
+ monitorEventLoopDelay,
+ performance,
+} from 'perf_hooks';
+import {
+ hrTime,
+ hrTimeDuration,
+ hrTimeToMilliseconds,
+} from '@opentelemetry/core';
+import { GaugeNames } from './enums/GaugeNames';
+import { Attributes } from '@opentelemetry/api';
+
+/**
+ * Runtime instrumentation for Opentelemetry
+ */
+export class RuntimeInstrumentation extends InstrumentationBase {
+ private eventLoopDelayHistogram: IntervalHistogram;
+ private _lastIntervalELU: EventLoopUtilization;
+ private config: RuntimeInstrumentationConfig;
+
+ constructor(
+ config: RuntimeInstrumentationConfig = {
+ monitorEventLoopDelayResolution: 10,
+ }
+ ) {
+ super('@opentelemetry/instrumentation-runtime', VERSION, config);
+ this.config = config;
+
+ // https://nodejs.org/api/perf_hooks.html#perf_hooksmonitoreventloopdelayoptions
+ this.eventLoopDelayHistogram = monitorEventLoopDelay({
+ resolution: config.monitorEventLoopDelayResolution,
+ });
+ this.eventLoopDelayHistogram.enable();
+
+ // https://nodejs.org/api/perf_hooks.html#performanceeventlooputilizationutilization1-utilization2
+ this._lastIntervalELU = performance.eventLoopUtilization();
+ }
+
+ private _getCustomMetricAttributes(): Attributes {
+ 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(
+ GaugeNames.NODE_EVENT_LOOP_DELAY,
+ {
+ description: 'Delay of event loop.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayMinGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_MIN,
+ {
+ description: 'The minimum recorded event loop delay.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayMaxGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_MAX,
+ {
+ description: 'The maximum recorded event loop delay.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayMeanGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_MEAN,
+ {
+ description: 'The mean of the recorded event loop delays.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayStddevGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_STDDEV,
+ {
+ description:
+ 'The standard deviation of the recorded event loop delays.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayP50Gauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_P50,
+ {
+ description: 'The 50th percentile of the recorded event loop delays.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayP95Gauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_P95,
+ {
+ description: 'The 95th percentile of the recorded event loop delays.',
+ unit: 'ms',
+ }
+ );
+
+ const eventLoopDelayP99Gauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_DELAY_P99,
+ {
+ description: 'The 99th percentile of the recorded event loop delays.',
+ unit: 'ms',
+ }
+ );
+
+ // Event Loop Delay metrics
+ this.meter.addBatchObservableCallback(
+ async observable => {
+ const attributes = this._getCustomMetricAttributes();
+
+ const startTime = hrTime();
+ // Waits for all existing and chained promises to be resolved:
+ await new Promise(setImmediate);
+ const endTime = hrTime();
+
+ // Calculate the delta between scheduling a promise and the event loop handling it.
+ // This is the delay of the event loop.
+ const duration = hrTimeToMilliseconds(
+ hrTimeDuration(startTime, endTime)
+ );
+ observable.observe(eventLoopDelayGauge, duration, attributes);
+
+ function nanoSecondToMilliSecond(nanoSecond: number) {
+ return nanoSecond / 1e6;
+ }
+
+ observable.observe(
+ eventLoopDelayMinGauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.min),
+ attributes
+ );
+ observable.observe(
+ eventLoopDelayMaxGauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.max),
+ attributes
+ );
+
+ observable.observe(
+ eventLoopDelayMeanGauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.mean),
+ attributes
+ );
+
+ observable.observe(
+ eventLoopDelayStddevGauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.stddev),
+ attributes
+ );
+
+ observable.observe(
+ eventLoopDelayP50Gauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.percentile(50)),
+ attributes
+ );
+
+ observable.observe(
+ eventLoopDelayP95Gauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.percentile(95)),
+ attributes
+ );
+
+ observable.observe(
+ eventLoopDelayP99Gauge,
+ nanoSecondToMilliSecond(this.eventLoopDelayHistogram.percentile(99)),
+ attributes
+ );
+ },
+ [
+ eventLoopDelayGauge,
+ eventLoopDelayMinGauge,
+ eventLoopDelayMaxGauge,
+ eventLoopDelayMeanGauge,
+ eventLoopDelayStddevGauge,
+ eventLoopDelayP50Gauge,
+ eventLoopDelayP95Gauge,
+ eventLoopDelayP99Gauge,
+ ]
+ );
+
+ const loopUtilizationGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_UTILIZATION,
+ {
+ description: 'The percentage utilization of the event loop.',
+ unit: 'percent',
+ }
+ );
+
+ const loopIdleGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_UTILIZATION_IDLE,
+ {
+ description: 'The idle time utilization of event loop.',
+ unit: 'ms',
+ }
+ );
+
+ const loopActiveGauge = this.meter.createObservableGauge(
+ GaugeNames.NODE_EVENT_LOOP_UTILIZATION_ACTIVE,
+ {
+ description: 'The active time utilization of event loop.',
+ unit: 'ms',
+ }
+ );
+
+ // Event Loop Utilization metrics
+ this.meter.addBatchObservableCallback(
+ async observable => {
+ const attributes = this._getCustomMetricAttributes();
+
+ // Store the current ELU so it can be assigned later.
+ const currentIntervalELU = performance.eventLoopUtilization();
+ // Calculate the diff between the current and last before sending.
+ const ELU = performance.eventLoopUtilization(
+ currentIntervalELU,
+ this._lastIntervalELU
+ );
+ observable.observe(
+ loopUtilizationGauge,
+ ELU.utilization * 100,
+ attributes
+ );
+ observable.observe(loopIdleGauge, ELU.idle, attributes);
+ observable.observe(loopActiveGauge, ELU.active, attributes);
+ // Assign over the last value to report the next interval.
+ this._lastIntervalELU = currentIntervalELU;
+ },
+ [loopUtilizationGauge, loopIdleGauge, loopActiveGauge]
+ );
+ }
+
+ init() {
+ // No instrumentation, only metrics
+ }
+}
diff --git a/plugins/node/instrumentation-runtime/src/types.ts b/plugins/node/instrumentation-runtime/src/types.ts
new file mode 100644
index 0000000000..de9aafbc52
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/src/types.ts
@@ -0,0 +1,29 @@
+/*
+ * 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 { InstrumentationConfig } from '@opentelemetry/instrumentation';
+import { Attributes } from '@opentelemetry/api';
+
+export interface RuntimeInstrumentationConfig extends InstrumentationConfig {
+ /**
+ * The sampling rate in milliseconds of event loop delay.
+ * Setting this too high will result in inaccurate data, as the baseline
+ * will be skewed by this value.
+ * @default 10
+ */
+ monitorEventLoopDelayResolution?: number;
+ /** Function for adding custom metric attributes on all recorded metrics */
+ customMetricAttributes?: () => Attributes;
+}
diff --git a/plugins/node/instrumentation-runtime/test/metrics/event-loop-lag-metrics.test.ts b/plugins/node/instrumentation-runtime/test/metrics/event-loop-lag-metrics.test.ts
new file mode 100644
index 0000000000..3d5a1a9428
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/test/metrics/event-loop-lag-metrics.test.ts
@@ -0,0 +1,252 @@
+/*
+ * 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 {
+ AggregationTemporality,
+ InMemoryMetricExporter,
+ MeterProvider,
+ DataPointType,
+} from '@opentelemetry/sdk-metrics';
+import { RuntimeInstrumentation } from '../../src/instrumentation';
+import * as assert from 'assert';
+import { TestMetricReader } from '../utils/TestMetricReader';
+
+const instrumentation = new RuntimeInstrumentation({
+ monitorEventLoopDelayResolution: 100,
+ customMetricAttributes: () => ({
+ foo: 'bar',
+ }),
+});
+
+const meterProvider = new MeterProvider();
+const metricsMemoryExporter = new InMemoryMetricExporter(
+ AggregationTemporality.DELTA
+);
+const metricReader = new TestMetricReader(metricsMemoryExporter);
+
+meterProvider.addMetricReader(metricReader);
+instrumentation.setMeterProvider(meterProvider);
+
+describe('metrics', () => {
+ beforeEach(() => {
+ metricsMemoryExporter.reset();
+ });
+
+ before(() => {
+ instrumentation.enable();
+ });
+
+ after(() => {
+ instrumentation.disable();
+ });
+
+ it('should add required metrics', async () => {
+ const requestCount = 3;
+ for (let i = 0; i < requestCount; i++) {
+ await new Promise(resolve =>
+ setTimeout(() => resolve(), (i + 1) * 3)
+ );
+ }
+
+ await metricReader.collectAndExport();
+
+ const resourceMetrics = metricsMemoryExporter.getMetrics();
+ const scopeMetrics = resourceMetrics[0].scopeMetrics;
+ assert.strictEqual(scopeMetrics.length, 1, 'scopeMetrics count');
+ const metrics = scopeMetrics[0].metrics;
+ assert.strictEqual(metrics.length, 11, 'metrics count');
+ const eventLoopDelay = metrics[0];
+ assert.strictEqual(eventLoopDelay.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelay.descriptor.description,
+ 'Delay of event loop.'
+ );
+ 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, {
+ foo: 'bar',
+ });
+
+ const eventLoopDelayMin = metrics[1];
+ assert.strictEqual(eventLoopDelayMin.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayMin.descriptor.description,
+ 'The minimum recorded event loop delay.'
+ );
+ assert.strictEqual(
+ eventLoopDelayMin.descriptor.name,
+ 'nodejs.event_loop_delay.min'
+ );
+ assert.strictEqual(eventLoopDelayMin.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayMin.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopDelayMin.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopDelayMax = metrics[2];
+ assert.strictEqual(eventLoopDelayMax.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayMax.descriptor.description,
+ 'The maximum recorded event loop delay.'
+ );
+ assert.strictEqual(
+ eventLoopDelayMax.descriptor.name,
+ 'nodejs.event_loop_delay.max'
+ );
+ assert.strictEqual(eventLoopDelayMax.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayMax.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopDelayMax.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopDelayMean = metrics[3];
+ assert.strictEqual(eventLoopDelayMean.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayMean.descriptor.description,
+ 'The mean of the recorded event loop delays.'
+ );
+ assert.strictEqual(
+ eventLoopDelayMean.descriptor.name,
+ 'nodejs.event_loop_delay.mean'
+ );
+ assert.strictEqual(eventLoopDelayMean.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayMean.dataPoints.length, 1);
+
+ const eventLoopDelayStddev = metrics[4];
+ assert.strictEqual(eventLoopDelayStddev.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayStddev.descriptor.description,
+ 'The standard deviation of the recorded event loop delays.'
+ );
+ assert.strictEqual(
+ eventLoopDelayStddev.descriptor.name,
+ 'nodejs.event_loop_delay.stddev'
+ );
+ assert.strictEqual(eventLoopDelayStddev.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayStddev.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopDelayStddev.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopDelayP50 = metrics[5];
+ assert.strictEqual(eventLoopDelayP50.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayP50.descriptor.description,
+ 'The 50th percentile of the recorded event loop delays.'
+ );
+ assert.strictEqual(
+ eventLoopDelayP50.descriptor.name,
+ 'nodejs.event_loop_delay.p50'
+ );
+ assert.strictEqual(eventLoopDelayP50.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayP50.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopDelayP50.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopDelayP95 = metrics[6];
+ assert.strictEqual(eventLoopDelayP95.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayP95.descriptor.description,
+ 'The 95th percentile of the recorded event loop delays.'
+ );
+ assert.strictEqual(
+ eventLoopDelayP95.descriptor.name,
+ 'nodejs.event_loop_delay.p95'
+ );
+ assert.strictEqual(eventLoopDelayP95.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayP95.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopDelayP95.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopDelayP99 = metrics[7];
+ assert.strictEqual(eventLoopDelayP99.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopDelayP99.descriptor.description,
+ 'The 99th percentile of the recorded event loop delays.'
+ );
+ assert.strictEqual(
+ eventLoopDelayP99.descriptor.name,
+ 'nodejs.event_loop_delay.p99'
+ );
+ assert.strictEqual(eventLoopDelayP99.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopDelayP99.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopDelayP99.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopUtilization = metrics[8];
+ assert.strictEqual(eventLoopUtilization.dataPointType, DataPointType.GAUGE);
+ assert.strictEqual(
+ eventLoopUtilization.descriptor.description,
+ 'The percentage utilization of the event loop.'
+ );
+ assert.strictEqual(
+ eventLoopUtilization.descriptor.name,
+ 'nodejs.event_loop_utilization'
+ );
+ assert.strictEqual(eventLoopUtilization.descriptor.unit, 'percent');
+ assert.strictEqual(eventLoopUtilization.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopUtilization.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopUtilizationIdle = metrics[9];
+ assert.strictEqual(
+ eventLoopUtilizationIdle.dataPointType,
+ DataPointType.GAUGE
+ );
+ assert.strictEqual(
+ eventLoopUtilizationIdle.descriptor.description,
+ 'The idle time utilization of event loop.'
+ );
+ assert.strictEqual(
+ eventLoopUtilizationIdle.descriptor.name,
+ 'nodejs.event_loop_utilization.idle'
+ );
+ assert.strictEqual(eventLoopUtilizationIdle.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopUtilizationIdle.dataPoints.length, 1);
+ assert.deepStrictEqual(eventLoopUtilizationIdle.dataPoints[0].attributes, {
+ foo: 'bar',
+ });
+
+ const eventLoopUtilizationActive = metrics[10];
+ assert.strictEqual(
+ eventLoopUtilizationActive.dataPointType,
+ DataPointType.GAUGE
+ );
+ assert.strictEqual(
+ eventLoopUtilizationActive.descriptor.description,
+ 'The active time utilization of event loop.'
+ );
+ assert.strictEqual(
+ eventLoopUtilizationActive.descriptor.name,
+ 'nodejs.event_loop_utilization.active'
+ );
+ assert.strictEqual(eventLoopUtilizationActive.descriptor.unit, 'ms');
+ assert.strictEqual(eventLoopUtilizationActive.dataPoints.length, 1);
+ assert.deepStrictEqual(
+ eventLoopUtilizationActive.dataPoints[0].attributes,
+ {
+ foo: 'bar',
+ }
+ );
+ });
+});
diff --git a/plugins/node/instrumentation-runtime/test/utils/TestMetricReader.ts b/plugins/node/instrumentation-runtime/test/utils/TestMetricReader.ts
new file mode 100644
index 0000000000..12a59b1175
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/test/utils/TestMetricReader.ts
@@ -0,0 +1,47 @@
+/*
+ * 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 { MetricReader, PushMetricExporter } from '@opentelemetry/sdk-metrics';
+
+export class TestMetricReader extends MetricReader {
+ constructor(private _exporter: PushMetricExporter) {
+ super({
+ aggregationTemporalitySelector:
+ _exporter.selectAggregationTemporality?.bind(_exporter),
+ });
+ }
+
+ protected onForceFlush(): Promise {
+ return Promise.resolve(undefined);
+ }
+
+ protected onShutdown(): Promise {
+ return Promise.resolve(undefined);
+ }
+
+ public async collectAndExport(): Promise {
+ const result = await this.collect();
+ await new Promise((resolve, reject) => {
+ this._exporter.export(result.resourceMetrics, result => {
+ if (result.error != null) {
+ reject(result.error);
+ } else {
+ resolve();
+ }
+ });
+ });
+ }
+}
diff --git a/plugins/node/instrumentation-runtime/tsconfig.json b/plugins/node/instrumentation-runtime/tsconfig.json
new file mode 100644
index 0000000000..28be80d266
--- /dev/null
+++ b/plugins/node/instrumentation-runtime/tsconfig.json
@@ -0,0 +1,11 @@
+{
+ "extends": "../../../tsconfig.base",
+ "compilerOptions": {
+ "rootDir": ".",
+ "outDir": "build"
+ },
+ "include": [
+ "src/**/*.ts",
+ "test/**/*.ts"
+ ]
+}
diff --git a/release-please-config.json b/release-please-config.json
index be9ea170ab..2733f0689a 100644
--- a/release-please-config.json
+++ b/release-please-config.json
@@ -54,6 +54,7 @@
"plugins/node/opentelemetry-instrumentation-redis-4": {},
"plugins/node/opentelemetry-instrumentation-restify": {},
"plugins/node/opentelemetry-instrumentation-router": {},
+ "plugins/node/opentelemetry-instrumentation-runtime": {},
"plugins/node/opentelemetry-instrumentation-winston": {},
"plugins/web/opentelemetry-instrumentation-document-load": {},
"plugins/web/opentelemetry-instrumentation-long-task": {},