All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.dist.src.config.middleware.experimental-garbage-collection-middleware.js Maven / Gradle / Ivy

The newest version!
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExperimentalGarbageCollectionPerformanceMetricsMiddleware = void 0;
const perf_hooks_1 = require("perf_hooks");
class ExperimentalGarbageCollectionPerformanceMetricsMiddlewareRequestHandler {
    onRequestBody(request) {
        return Promise.resolve(request);
    }
    onRequestMetadata(metadata) {
        return Promise.resolve(metadata);
    }
    onResponseMetadata(metadata) {
        return Promise.resolve(metadata);
    }
    onResponseBody(response) {
        return Promise.resolve(response);
    }
    onResponseStatus(status) {
        return Promise.resolve(status);
    }
}
/**
 * This middleware enables garbage collection metrics. It subscribers to a GC performance observer provided by
 * node's built-in performance hooks, and logs key GC events. A sample log looks like:
 *
 * {
 *     "entryType": "gc",
 *     "startTime": 8221.879917,
 *     "duration": 2.8905000016093254,  <-- most important field to analyze for stop the world events, measured in milliseconds.
 *     "detail": {
 *         "kind": 4,  <-- constant for NODE_PERFORMANCE_GC_MAJOR. `MAJOR` events might point to GC events causing long delays.
 *         "flags": 32
 *     },
 *     "timestamp": 1710300309368
 * }
 */
class ExperimentalGarbageCollectionPerformanceMetricsMiddleware {
    constructor(loggerFactory) {
        this.logger = loggerFactory.getLogger(this);
        this.gcObserver = new perf_hooks_1.PerformanceObserver(items => {
            items
                .getEntries()
                .filter(
            // NODE_PERFORMANCE_GC_MAJOR indicates a major GC event such as STW (stop-the-world) pauses
            // and other long delays. This filter is to control the volume of GC logs if we were to enable
            // this on a customer's client.
            // NODE_PERFORMANCE_GC_INCREMENTAL prints incremental GC stream of logs when the process is approaching
            // max memory.
            item => {
                if (item.entryType !== 'gc') {
                    return false;
                }
                const gcPerfDetail = item.detail;
                return (gcPerfDetail.kind === perf_hooks_1.constants.NODE_PERFORMANCE_GC_MAJOR ||
                    gcPerfDetail.kind === perf_hooks_1.constants.NODE_PERFORMANCE_GC_INCREMENTAL);
            })
                .forEach(item => {
                const gcPerfDetail = item.detail;
                const gcEventObject = {
                    entryType: item.entryType,
                    startTime: item.startTime,
                    duration: item.duration,
                    kind: gcPerfDetail.kind,
                    flags: gcPerfDetail.flags,
                    timestamp: Date.now(),
                };
                this.logger.info(JSON.stringify(gcEventObject));
            });
        });
        this.gcObserver.observe({ entryTypes: ['gc'] });
    }
    onNewRequest() {
        return new ExperimentalGarbageCollectionPerformanceMetricsMiddlewareRequestHandler();
    }
    close() {
        this.gcObserver.disconnect();
    }
}
exports.ExperimentalGarbageCollectionPerformanceMetricsMiddleware = ExperimentalGarbageCollectionPerformanceMetricsMiddleware;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhwZXJpbWVudGFsLWdhcmJhZ2UtY29sbGVjdGlvbi1taWRkbGV3YXJlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2NvbmZpZy9taWRkbGV3YXJlL2V4cGVyaW1lbnRhbC1nYXJiYWdlLWNvbGxlY3Rpb24tbWlkZGxld2FyZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFPQSwyQ0FBMEQ7QUFJMUQsTUFBTSx1RUFBdUU7SUFHM0UsYUFBYSxDQUFDLE9BQTBCO1FBQ3RDLE9BQU8sT0FBTyxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQztJQUNsQyxDQUFDO0lBRUQsaUJBQWlCLENBQUMsUUFBNEI7UUFDNUMsT0FBTyxPQUFPLENBQUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQ25DLENBQUM7SUFFRCxrQkFBa0IsQ0FDaEIsUUFBNEI7UUFFNUIsT0FBTyxPQUFPLENBQUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQ25DLENBQUM7SUFFRCxjQUFjLENBQ1osUUFBa0M7UUFFbEMsT0FBTyxPQUFPLENBQUM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ZXN0KCk6IE1pZGRsZXdhcmVSZXF1ZXN0SGFuZGxlciB7XG4gICAgcmV0dXJuIG5ldyBFeHBlcmltZW50YWxHYXJiYWdlQ29sbGVjdGlvblBlcmZvcm1hbmNlTWV0cmljc01pZGRsZXdhcmVSZXF1ZXN0SGFuZGxlcigpO1xuICB9XG5cbiAgY2xvc2UoKSB7XG4gICAgdGhpcy5nY09ic2VydmVyLmRpc2Nvbm5lY3QoKTtcbiAgfVxufVxuIl19




© 2015 - 2025 Weber Informatics LLC | Privacy Policy