-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement GC metrics collection without native(C++) modules (#274)
* feat: implement GC metrics collection without native(C++) modules * fix: replace summury with histogram. Make metric names compatible with promtool check * fix: remove duplicate metrics, update CHANGELOG.md * Update example/server.js Co-Authored-By: Sam Roberts <vieuxtech@gmail.com> * Update CHANGELOG.md Co-Authored-By: Sam Roberts <vieuxtech@gmail.com> * fix: update README Co-authored-by: Sam Roberts <vieuxtech@gmail.com>
- Loading branch information
1 parent
c9af549
commit 9b4ef10
Showing
7 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,54 @@ | ||
'use strict'; | ||
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_DURATION_SECONDS = 'nodejs_gc_duration_seconds'; | ||
const DEFAULT_GC_DURATION_BUCKETS = [0.001, 0.01, 0.1, 1, 2, 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 () => {}; | ||
} | ||
|
||
const namePrefix = config.prefix ? config.prefix : ''; | ||
const buckets = config.gcDurationBuckets | ||
? config.gcDurationBuckets | ||
: DEFAULT_GC_DURATION_BUCKETS; | ||
const gcHistogram = new Histogram({ | ||
name: namePrefix + NODEJS_GC_DURATION_SECONDS, | ||
help: | ||
'Garbage collection duration by kind, one of major, minor, incremental or weakcb.', | ||
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] }; | ||
|
||
// 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_DURATION_SECONDS]; |
This file contains 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,43 @@ | ||
'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(1); | ||
|
||
expect(metrics[0].help).toEqual( | ||
'Garbage collection duration by kind, one of major, minor, incremental or weakcb.' | ||
); | ||
expect(metrics[0].type).toEqual('histogram'); | ||
expect(metrics[0].name).toEqual('nodejs_gc_duration_seconds'); | ||
} else { | ||
expect(metrics).toHaveLength(0); | ||
} | ||
}); | ||
}); |