-
Notifications
You must be signed in to change notification settings - Fork 410
feat: implement advanced event loop monitoring #278
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
00f0fde
feat: implement advanced event loop monitoring
648ccc7
Update lib/metrics/eventLoopLag.js
yvasiyarov 474aa24
Update lib/metrics/eventLoopLag.js
yvasiyarov f81831e
Update lib/metrics/eventLoopLag.js
yvasiyarov 73083f5
fix: remove timestamp support, fix tests
yvasiyarov fa467ca
Update lib/metrics/eventLoopLag.js
yvasiyarov e4bcfce
Update lib/metrics/eventLoopLag.js
yvasiyarov ac3e673
Update lib/metrics/eventLoopLag.js
yvasiyarov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,128 @@ | ||
| 'use strict'; | ||
|
|
||
| const Gauge = require('../gauge'); | ||
|
|
||
| // Check if perf_hooks module is available | ||
| let perf_hooks; | ||
| try { | ||
| // eslint-disable-next-line | ||
| perf_hooks = require('perf_hooks'); | ||
| } catch (e) { | ||
| // node version is too old | ||
| } | ||
|
|
||
| const NODEJS_EVENTLOOP_LAG = 'nodejs_eventloop_lag_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_MIN = 'nodejs_eventloop_lag_min_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_MAX = 'nodejs_eventloop_lag_max_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_MEAN = 'nodejs_eventloop_lag_mean_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_STDDEV = 'nodejs_eventloop_lag_stddev_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_P50 = 'nodejs_eventloop_lag_p50_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_P90 = 'nodejs_eventloop_lag_p90_seconds'; | ||
| const NODEJS_EVENTLOOP_LAG_P99 = 'nodejs_eventloop_lag_p99_seconds'; | ||
|
|
||
| function reportEventloopLag(start, gauge) { | ||
| function reportEventloopLag(start, gauge, timestamps) { | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| const delta = process.hrtime(start); | ||
| const nanosec = delta[0] * 1e9 + delta[1]; | ||
| const seconds = nanosec / 1e9; | ||
|
|
||
| gauge.set(seconds, Date.now()); | ||
| if (timestamps) { | ||
| gauge.set(seconds, Date.now()); | ||
| } else { | ||
| gauge.set(seconds); | ||
| } | ||
| } | ||
|
|
||
| module.exports = (registry, config = {}) => { | ||
| const namePrefix = config.prefix ? config.prefix : ''; | ||
|
|
||
| const gauge = new Gauge({ | ||
| const lag = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG, | ||
| help: 'Lag of event loop in seconds.', | ||
| registers: registry ? [registry] : undefined, | ||
| aggregator: 'average' | ||
| }); | ||
| const lagMin = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_MIN, | ||
| help: 'The minimum recorded event loop delay.', | ||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const lagMax = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_MAX, | ||
| help: 'The maximum recorded event loop delay.', | ||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const lagMean = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_MEAN, | ||
| help: 'The mean of the recorded event loop delays.', | ||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const lagStddev = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_STDDEV, | ||
| help: 'The standard deviation of the recorded event loop delays.', | ||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const lagP50 = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_P50, | ||
| help: 'The 50 percentile of the recorded event loop delays.', | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const lagP90 = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_P90, | ||
| help: 'The 90 percentile of the recorded event loop delays.', | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const lagP99 = new Gauge({ | ||
| name: namePrefix + NODEJS_EVENTLOOP_LAG_P99, | ||
| help: 'The 99 percentile of the recorded event loop delays.', | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| registers: registry ? [registry] : undefined | ||
| }); | ||
|
|
||
| // eslint-disable-next-line | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| if (!perf_hooks || !perf_hooks.monitorEventLoopDelay) { | ||
| return () => { | ||
| const start = process.hrtime(); | ||
| setImmediate(reportEventloopLag, start, lag, config.timestamps); | ||
| }; | ||
| } | ||
|
|
||
| // eslint-disable-next-line | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| const histogram = perf_hooks.monitorEventLoopDelay({ | ||
| resolution: config.eventLoopMonitoringPrecision | ||
| }); | ||
| histogram.enable(); | ||
|
|
||
| return () => { | ||
| const start = process.hrtime(); | ||
| setImmediate(reportEventloopLag, start, gauge); | ||
| setImmediate(reportEventloopLag, start, lag, config.timestamps); | ||
|
|
||
| if (config.timestamps) { | ||
| const now = Date.now(); | ||
|
|
||
| lagMin.set(histogram.min / 1e9, now); | ||
| lagMax.set(histogram.max / 1e9, now); | ||
| lagMean.set(histogram.mean / 1e9, now); | ||
| lagStddev.set(histogram.stddev / 1e9, now); | ||
| lagP50.set(histogram.percentile(50) / 1e9, now); | ||
| lagP90.set(histogram.percentile(90) / 1e9, now); | ||
| lagP99.set(histogram.percentile(99) / 1e9, now); | ||
| } else { | ||
| lagMin.set(histogram.min / 1e9); | ||
| lagMax.set(histogram.max / 1e9); | ||
| lagMean.set(histogram.mean / 1e9); | ||
| lagStddev.set(histogram.stddev / 1e9); | ||
| lagP50.set(histogram.percentile(50) / 1e9); | ||
| lagP90.set(histogram.percentile(90) / 1e9); | ||
| lagP99.set(histogram.percentile(99) / 1e9); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| module.exports.metricNames = [NODEJS_EVENTLOOP_LAG]; | ||
| module.exports.metricNames = [ | ||
| NODEJS_EVENTLOOP_LAG, | ||
| NODEJS_EVENTLOOP_LAG_MIN, | ||
| NODEJS_EVENTLOOP_LAG_MAX, | ||
| NODEJS_EVENTLOOP_LAG_MEAN, | ||
| NODEJS_EVENTLOOP_LAG_STDDEV, | ||
| NODEJS_EVENTLOOP_LAG_P50, | ||
| NODEJS_EVENTLOOP_LAG_P90, | ||
| NODEJS_EVENTLOOP_LAG_P99 | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.