diff --git a/index.d.ts b/index.d.ts index 8cf983a7..6dbb96a6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -767,6 +767,21 @@ export function exponentialBuckets( count: number, ): number[]; +type AvailableDefaultMetrics = + | 'processCpuTotal' + | 'processStartTime' + | 'osMemoryHeap' + | 'processOpenFileDescriptors' + | 'processMaxFileDescriptors' + | 'eventLoopLag' + | 'processResources' + | 'processHandles' + | 'processRequests' + | 'heapSizeAndUsed' + | 'heapSpacesSizeAndUsed' + | 'version' + | 'gc'; + export interface DefaultMetricsCollectorConfiguration< T extends RegistryContentType, > { @@ -775,6 +790,7 @@ export interface DefaultMetricsCollectorConfiguration< gcDurationBuckets?: number[]; eventLoopMonitoringPrecision?: number; labels?: object; + exclude?: AvailableDefaultMetrics[]; } export const collectDefaultMetrics: { @@ -786,7 +802,7 @@ export const collectDefaultMetrics: { config?: DefaultMetricsCollectorConfiguration, ): void; /** All available default metrics */ - metricsList: string[]; + metricsList: AvailableDefaultMetrics[]; }; /** diff --git a/lib/defaultMetrics.js b/lib/defaultMetrics.js index 651d640e..d0ab18d6 100644 --- a/lib/defaultMetrics.js +++ b/lib/defaultMetrics.js @@ -42,10 +42,13 @@ module.exports = function collectDefaultMetrics(config) { throw new TypeError('config must be null, undefined, or an object'); } - config = { eventLoopMonitoringPrecision: 10, ...config }; + config = { eventLoopMonitoringPrecision: 10, exclude: [], ...config }; - for (const metric of Object.values(metrics)) { - metric(config.register, config); + for (const metricGroup in metrics) { + if (config.exclude.includes(metricGroup)) { + continue; + } + metrics[metricGroup](config.register, config); } }; diff --git a/test/defaultMetricsTest.js b/test/defaultMetricsTest.js index 2021d799..a6f02759 100644 --- a/test/defaultMetricsTest.js +++ b/test/defaultMetricsTest.js @@ -91,6 +91,22 @@ describe.each([ }); }); + it('should allow to exclude specific metrics', async () => { + expect(await register.getMetricsAsJSON()).toHaveLength(0); + + collectDefaultMetrics(); + expect( + await register.getSingleMetric('nodejs_eventloop_lag_seconds'), + ).toBeDefined(); + + register.clear(); + + collectDefaultMetrics({ exclude: ['eventLoopLag'] }); + expect( + await register.getSingleMetric('nodejs_eventloop_lag_seconds'), + ).toBeUndefined(); + }); + describe('disabling', () => { it('should not throw error', () => { const fn = function () {