package.dist.src.config.middleware.experimental-metrics-csv-middleware.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk Show documentation
Show all versions of sdk Show documentation
Client SDK for Momento services
The newest version!
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExperimentalMetricsCsvMiddleware = void 0;
const fs = require("fs");
const experimental_metrics_middleware_1 = require("./impl/experimental-metrics-middleware");
class ExperimentalMetricsCsvMiddlewareRequestHandler extends experimental_metrics_middleware_1.ExperimentalMetricsMiddlewareRequestHandler {
constructor(parent, logger, context, csvPath) {
super(parent, logger, context);
this.csvPath = csvPath;
}
async emitMetrics(metrics) {
const csvRow = [
metrics.momento.numActiveRequestsAtStart,
metrics.momento.numActiveRequestsAtFinish,
metrics.momento.requestType,
metrics.momento.status,
metrics.momento.startTime,
metrics.momento.requestBodyTime,
metrics.momento.endTime,
metrics.momento.duration,
metrics.momento.requestSize,
metrics.momento.responseSize,
metrics.momento.connectionID,
].join(',');
try {
await fs.promises.appendFile(this.csvPath, `${csvRow}\n`);
}
catch (err) {
if (err !== null) {
this.logger.error('Error writing to metrics csv file at path: %s : %s', this.csvPath, err);
}
}
}
}
/**
* This middleware enables per-request client-side metrics. Metrics for each
* request will be written to a CSV file; this file can be analyzed or shared
* with Momento to diagnose performance issues.
*
* The metrics format is currently considered experimental; in a future release,
* once the format is considered stable, this class will be renamed to remove
* the Experimental prefix.
*
* WARNING: enabling this middleware may have minor performance implications,
* so enable with caution.
*
* WARNING: depending on your request volume, the CSV file size may grow quickly;
* neither sampling nor file compression / rotation are included at this time
* (though they may be added in the future).
*
* See `advanced.ts` in the examples directory for an example of how to set up
* your {Configuration} to enable this middleware.
*/
class ExperimentalMetricsCsvMiddleware extends experimental_metrics_middleware_1.ExperimentalMetricsMiddleware {
constructor(csvPath, loggerFactory) {
super(loggerFactory, (p, l, c) => new ExperimentalMetricsCsvMiddlewareRequestHandler(p, l, c, csvPath));
this.csvPath = csvPath;
fs.writeFileSync(this.csvPath, `${this.fieldNames().join(',')}\n`);
}
}
exports.ExperimentalMetricsCsvMiddleware = ExperimentalMetricsCsvMiddleware;
ExperimentalMetricsCsvMiddleware.numActiveRequests = 0;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhwZXJpbWVudGFsLW1ldHJpY3MtY3N2LW1pZGRsZXdhcmUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvY29uZmlnL21pZGRsZXdhcmUvZXhwZXJpbWVudGFsLW1ldHJpY3MtY3N2LW1pZGRsZXdhcmUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEseUJBQXlCO0FBRXpCLDRGQUlnRDtBQUdoRCxNQUFNLDhDQUErQyxTQUFRLDZFQUEyQztJQUd0RyxZQUNFLE1BQXFDLEVBQ3JDLE1BQXFCLEVBQ3JCLE9BQXdDLEVBQ3hDLE9BQWU7UUFFZixLQUFLLENBQUMsTUFBTSxFQUFFLE1BQU0sRUFBRSxPQUFPLENBQUMsQ0FBQztRQUMvQixJQUFJLENBQUMsT0FBTyxHQUFHLE9BQU8sQ0FBQztJQUN6QixDQUFDO0lBRUQsS0FBSyxDQUFDLFdBQVcsQ0FBQyxPQUFtQztRQUNuRCxNQUFNLE1BQU0sR0FBRztZQUNiLE9BQU8sQ0FBQyxPQUFPLENBQUMsd0JBQXdCO1lBQ3hDLE9BQU8sQ0FBQyxPQUFPLENBQUMseUJBQXlCO1lBQ3pDLE9BQU8sQ0FBQyxPQUFPLENBQUMsV0FBVztZQUMzQixPQUFPLENBQUMsT0FBTyxDQUFDLE1BQU07WUFDdEIsT0FBTyxDQUFDLE9BQU8sQ0FBQyxTQUFTO1lBQ3pCLE9BQU8sQ0FBQyxPQUFPLENBQUMsZUFBZTtZQUMvQixPQUFPLENBQUMsT0FBTyxDQUFDLE9BQU87WUFDdkIsT0FBTyxDQUFDLE9BQU8sQ0FBQyxRQ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