Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
> {
Expand All @@ -775,6 +790,7 @@ export interface DefaultMetricsCollectorConfiguration<
gcDurationBuckets?: number[];
eventLoopMonitoringPrecision?: number;
labels?: object;
exclude?: AvailableDefaultMetrics[];
}

export const collectDefaultMetrics: {
Expand All @@ -786,7 +802,7 @@ export const collectDefaultMetrics: {
config?: DefaultMetricsCollectorConfiguration<T>,
): void;
/** All available default metrics */
metricsList: string[];
metricsList: AvailableDefaultMetrics[];
};

/**
Expand Down
9 changes: 6 additions & 3 deletions lib/defaultMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
16 changes: 16 additions & 0 deletions test/defaultMetricsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down