1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-06 01:15:28 +02:00

feat: add capability to write heap snapshot. (#2611)

Is protected behind a config option called `enableHeapSnapshotEnpoint`.
When API endpoint is triggered it will write a heapSnapshot to disk.

Steps to create heap snapshot:

1. Add env variable `ENABLE_HEAP_SNAPSHOT_ENPOINT=true`. 
2. Call this endpoint: `/internal-backstage/heapSnapshot`
3. Download the created heapdump from the server. 

The snapshot can be loaded in the chrome dev-tool to analysis.
This commit is contained in:
Ivar Conradi Østhus 2022-12-08 11:25:39 +01:00 committed by GitHub
parent 353f50237d
commit d9d9f01b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 0 deletions

View File

@ -126,6 +126,7 @@ exports[`should create default config 1`] = `
"server": { "server": {
"baseUriPath": "", "baseUriPath": "",
"cdnPrefix": undefined, "cdnPrefix": undefined,
"enableHeapSnapshotEnpoint": false,
"enableRequestLogger": false, "enableRequestLogger": false,
"gracefulShutdownEnable": true, "gracefulShutdownEnable": true,
"gracefulShutdownTimeout": 1000, "gracefulShutdownTimeout": 1000,

View File

@ -165,6 +165,10 @@ const defaultServerOption: IServerOption = {
cdnPrefix: process.env.CDN_PREFIX, cdnPrefix: process.env.CDN_PREFIX,
unleashUrl: process.env.UNLEASH_URL || 'http://localhost:4242', unleashUrl: process.env.UNLEASH_URL || 'http://localhost:4242',
serverMetrics: true, serverMetrics: true,
enableHeapSnapshotEnpoint: parseEnvVarBoolean(
process.env.ENABLE_HEAP_SNAPSHOT_ENPOINT,
false,
),
keepAliveTimeout: minutesToMilliseconds(1), keepAliveTimeout: minutesToMilliseconds(1),
headersTimeout: secondsToMilliseconds(61), headersTimeout: secondsToMilliseconds(61),
enableRequestLogger: false, enableRequestLogger: false,

View File

@ -1,3 +1,4 @@
import { writeHeapSnapshot } from 'v8';
import { register as prometheusRegister } from 'prom-client'; import { register as prometheusRegister } from 'prom-client';
import Controller from './controller'; import Controller from './controller';
import { IUnleashConfig } from '../types/option'; import { IUnleashConfig } from '../types/option';
@ -16,6 +17,14 @@ class BackstageController extends Controller {
res.end(await prometheusRegister.metrics()); res.end(await prometheusRegister.metrics());
}); });
} }
if (config.server.enableHeapSnapshotEnpoint) {
this.get('/heap-snapshot', async (req, res) => {
writeHeapSnapshot();
res.status(200);
res.end('Snapshot written');
});
}
} }
} }

View File

@ -79,6 +79,7 @@ export interface IServerOption {
cdnPrefix?: string; cdnPrefix?: string;
unleashUrl: string; unleashUrl: string;
serverMetrics: boolean; serverMetrics: boolean;
enableHeapSnapshotEnpoint: boolean;
enableRequestLogger: boolean; enableRequestLogger: boolean;
gracefulShutdownEnable: boolean; gracefulShutdownEnable: boolean;
gracefulShutdownTimeout: number; gracefulShutdownTimeout: number;

View File

@ -23,6 +23,7 @@ process.nextTick(async () => {
// keepAliveTimeout: 1, // keepAliveTimeout: 1,
gracefulShutdownEnable: true, gracefulShutdownEnable: true,
// cdnPrefix: 'https://cdn.getunleash.io/unleash/v4.4.1', // cdnPrefix: 'https://cdn.getunleash.io/unleash/v4.4.1',
enableHeapSnapshotEnpoint: true,
}, },
logLevel: LogLevel.debug, logLevel: LogLevel.debug,
enableOAS: true, enableOAS: true,