-
Notifications
You must be signed in to change notification settings - Fork 409
feat: implement GC metrics collection without native(C++) modules #274
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e9ba1e
feat: implement GC metrics collection without native(C++) modules
57b69c1
fix: replace summury with histogram.
a54e523
fix: remove duplicate metrics, update CHANGELOG.md
be5b51f
Update example/server.js
yvasiyarov 3e904af
Update CHANGELOG.md
yvasiyarov 55fcbdc
fix: update README
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| 'use strict'; | ||
| const Counter = require('../counter'); | ||
| const Histogram = require('../histogram'); | ||
|
|
||
| let perf_hooks; | ||
|
|
||
| try { | ||
| // eslint-disable-next-line | ||
| perf_hooks = require('perf_hooks'); | ||
| } catch (e) { | ||
| // node version is too old | ||
| } | ||
|
|
||
| const NODEJS_GC_RUNS_TOTAL = 'nodejs_gc_runs_total'; | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| const NODEJS_GC_DURATION = 'nodejs_gc_duration'; | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| const DEFAULT_GC_DURATION_BUCKETS = [ | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| 0.001, | ||
| 0.005, | ||
| 0.01, | ||
| 0.025, | ||
| 0.05, | ||
| 0.1, | ||
| 0.25, | ||
| 0.5, | ||
| 1, | ||
| 2.5, | ||
| 5 | ||
| ]; | ||
|
|
||
| const kinds = []; | ||
| kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR] = 'major'; | ||
| kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR] = 'minor'; | ||
| kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL] = 'incremental'; | ||
| kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB] = 'weakcb'; | ||
|
|
||
| module.exports = (registry, config = {}) => { | ||
| if (!perf_hooks) { | ||
| return () => {}; | ||
| } | ||
|
yvasiyarov marked this conversation as resolved.
|
||
|
|
||
| const namePrefix = config.prefix ? config.prefix : ''; | ||
| const buckets = config.gcDurationBuckets | ||
| ? config.gcDurationBuckets | ||
| : DEFAULT_GC_DURATION_BUCKETS; | ||
| const gcCount = new Counter({ | ||
| name: namePrefix + NODEJS_GC_RUNS_TOTAL, | ||
| help: | ||
| 'Count of garbage collections. kind label is one of major, minor, incremental or weakcb.', | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| labelNames: ['kind'], | ||
| registers: registry ? [registry] : undefined | ||
| }); | ||
| const gcHistogram = new Histogram({ | ||
| name: namePrefix + NODEJS_GC_DURATION, | ||
| help: | ||
| 'Histogram of garbage collections. kind label is one of major, minor, incremental or weakcb.', | ||
|
yvasiyarov marked this conversation as resolved.
Outdated
|
||
| labelNames: ['kind'], | ||
| buckets, | ||
| registers: registry ? [registry] : undefined | ||
| }); | ||
|
|
||
| const obs = new perf_hooks.PerformanceObserver(list => { | ||
| const entry = list.getEntries()[0]; | ||
| const labels = { kind: kinds[entry.kind] }; | ||
|
|
||
| gcCount.inc(labels, 1); | ||
| // Convert duration from milliseconds to seconds | ||
| gcHistogram.observe(labels, entry.duration / 1000); | ||
| }); | ||
|
|
||
| // We do not expect too many gc events per second, so we do not use buffering | ||
| obs.observe({ entryTypes: ['gc'], buffered: false }); | ||
|
|
||
| return () => {}; | ||
| }; | ||
|
|
||
| module.exports.metricNames = [NODEJS_GC_RUNS_TOTAL, NODEJS_GC_DURATION]; | ||
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| 'use strict'; | ||
|
|
||
| describe('gc', () => { | ||
| const register = require('../../index').register; | ||
| const processHandles = require('../../lib/metrics/gc'); | ||
|
|
||
| beforeAll(() => { | ||
| register.clear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| register.clear(); | ||
| }); | ||
|
|
||
| it('should add metric to the registry', () => { | ||
| expect(register.getMetricsAsJSON()).toHaveLength(0); | ||
|
|
||
| processHandles()(); | ||
|
|
||
| const metrics = register.getMetricsAsJSON(); | ||
|
|
||
| // 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 | ||
| } | ||
|
|
||
| if (perf_hooks) { | ||
| expect(metrics).toHaveLength(2); | ||
|
|
||
| expect(metrics[0].help).toEqual( | ||
| 'Count of garbage collections. kind label is one of major, minor, incremental or weakcb.' | ||
| ); | ||
| expect(metrics[0].type).toEqual('counter'); | ||
| expect(metrics[0].name).toEqual('nodejs_gc_runs_total'); | ||
|
|
||
| expect(metrics[1].help).toEqual( | ||
| 'Histogram of garbage collections. kind label is one of major, minor, incremental or weakcb.' | ||
| ); | ||
| expect(metrics[1].type).toEqual('histogram'); | ||
| expect(metrics[1].name).toEqual('nodejs_gc_duration'); | ||
| } else { | ||
| expect(metrics).toHaveLength(0); | ||
| } | ||
| }); | ||
| }); |
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.