-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use process.getActiveResourcesInfo() in process metrics
We should use process.getActiveResourcesInfo() here because it is a public alternative of the private APIs process._getActiveHandles() and process._getActiveRequests(). Refs: https://nodejs.org/api/process.html#processgetactiveresourcesinfo Signed-off-by: Darshan Sen <darshan.sen@postman.com>
- Loading branch information
Showing
5 changed files
with
106 additions
and
2 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
Binary file not shown.
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,58 @@ | ||
'use strict'; | ||
const Gauge = require('../gauge'); | ||
const { updateMetrics } = require('./helpers/processMetricsHelpers'); | ||
|
||
const NODEJS_ACTIVE_RESOURCES = 'nodejs_active_resources'; | ||
const NODEJS_ACTIVE_RESOURCES_TOTAL = 'nodejs_active_resources_total'; | ||
|
||
module.exports = (registry, config = {}) => { | ||
// Don't do anything if the function does not exist in previous nodes (exists in node@17.3.0) | ||
if (typeof process.getActiveResourcesInfo !== 'function') { | ||
return; | ||
} | ||
|
||
const namePrefix = config.prefix ? config.prefix : ''; | ||
const labels = config.labels ? config.labels : {}; | ||
const labelNames = Object.keys(labels); | ||
|
||
new Gauge({ | ||
name: namePrefix + NODEJS_ACTIVE_RESOURCES, | ||
help: | ||
'Number of active resources that are currently keeping the event loop alive, grouped by async resource type.', | ||
labelNames: ['type', ...labelNames], | ||
registers: registry ? [registry] : undefined, | ||
collect() { | ||
const resources = process.getActiveResourcesInfo(); | ||
|
||
const data = {}; | ||
|
||
for (let i = 0; i < resources.length; i++) { | ||
const resource = resources[i]; | ||
|
||
if (Object.hasOwn(data, resource)) { | ||
data[resource] += 1; | ||
} else { | ||
data[resource] = 1; | ||
} | ||
} | ||
|
||
updateMetrics(this, data, labels); | ||
}, | ||
}); | ||
|
||
new Gauge({ | ||
name: namePrefix + NODEJS_ACTIVE_RESOURCES_TOTAL, | ||
help: 'Total number of active resources.', | ||
registers: registry ? [registry] : undefined, | ||
labelNames, | ||
collect() { | ||
const resources = process.getActiveResourcesInfo(); | ||
this.set(labels, resources.length); | ||
}, | ||
}); | ||
}; | ||
|
||
module.exports.metricNames = [ | ||
NODEJS_ACTIVE_RESOURCES, | ||
NODEJS_ACTIVE_RESOURCES_TOTAL, | ||
]; |
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,37 @@ | ||
'use strict'; | ||
|
||
describe('processRequests', () => { | ||
const register = require('../../index').register; | ||
const processResources = require('../../lib/metrics/processResources'); | ||
|
||
beforeAll(() => { | ||
register.clear(); | ||
}); | ||
|
||
afterEach(() => { | ||
register.clear(); | ||
}); | ||
|
||
it('should add metric to the registry', async () => { | ||
if (typeof process.getActiveResourcesInfo !== 'function') { | ||
return; | ||
} | ||
|
||
expect(await register.getMetricsAsJSON()).toHaveLength(0); | ||
|
||
processResources(); | ||
|
||
const metrics = await register.getMetricsAsJSON(); | ||
|
||
expect(metrics).toHaveLength(2); | ||
expect(metrics[0].help).toEqual( | ||
'Number of active resources that are currently keeping the event loop alive, grouped by async resource type.', | ||
); | ||
expect(metrics[0].type).toEqual('gauge'); | ||
expect(metrics[0].name).toEqual('nodejs_active_resources'); | ||
|
||
expect(metrics[1].help).toEqual('Total number of active resources.'); | ||
expect(metrics[1].type).toEqual('gauge'); | ||
expect(metrics[1].name).toEqual('nodejs_active_resources_total'); | ||
}); | ||
}); |