Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eventloop idle time metric #474

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Added: `nodejs_eventloop_idle_seconds_total` metric to 'collectDefaultMetrics()`.

## [14.0.0] - 2021-09-18

### Breaking
Expand Down
2 changes: 2 additions & 0 deletions lib/defaultMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const osMemoryHeap = require('./metrics/osMemoryHeap');
const processOpenFileDescriptors = require('./metrics/processOpenFileDescriptors');
const processMaxFileDescriptors = require('./metrics/processMaxFileDescriptors');
const eventLoopLag = require('./metrics/eventLoopLag');
const eventLoopIdle = require('./metrics/eventLoopIdle');
const processHandles = require('./metrics/processHandles');
const processRequests = require('./metrics/processRequests');
const heapSizeAndUsed = require('./metrics/heapSizeAndUsed');
Expand All @@ -23,6 +24,7 @@ const metrics = {
processOpenFileDescriptors,
processMaxFileDescriptors,
eventLoopLag,
eventLoopIdle,
processHandles,
processRequests,
heapSizeAndUsed,
Expand Down
44 changes: 44 additions & 0 deletions lib/metrics/eventLoopIdle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const Counter = require('../counter');

// Check if metric is available
let nodeTiming;
try {
/* eslint-disable node/no-unsupported-features/node-builtins */
nodeTiming = require('perf_hooks').performance.nodeTiming;
} catch (ex) {
// node version is too old
}

// Reported only when perf_hooks.performance.nodeTiming.idleTime is available.
const NODEJS_EVENTLOOP_IDLE = 'nodejs_eventloop_idle_seconds_total';

module.exports = (registry, config = {}) => {
if (!nodeTiming || nodeTiming.idleTime === undefined) {
return;
}

const namePrefix = config.prefix ? config.prefix : '';
const labels = config.labels ? config.labels : {};
const labelNames = Object.keys(labels);
const registers = registry ? [registry] : undefined;

let lastIdle = nodeTiming.idleTime;

const idle = new Counter({
name: namePrefix + NODEJS_EVENTLOOP_IDLE,
help:
"Total amount of time the event loop has been idle within the event loop's event provider",
registers,
labelNames,
aggregator: 'average',
collect() {
const val = nodeTiming.idleTime / 1e3;
idle.inc(labels, val - lastIdle);
lastIdle = val;
},
});
};

module.exports.metricNames = [NODEJS_EVENTLOOP_IDLE];