@promster/express 15.5.1 → 15.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +107 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +74 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -9
- package/dist/declarations/src/index.d.ts +0 -4
- package/dist/declarations/src/middleware/index.d.ts +0 -2
- package/dist/declarations/src/middleware/middleware.d.ts +0 -27
- package/dist/promster-express.cjs.d.ts +0 -2
- package/dist/promster-express.cjs.dev.js +0 -131
- package/dist/promster-express.cjs.js +0 -7
- package/dist/promster-express.cjs.prod.js +0 -131
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let _promster_metrics = require("@promster/metrics");
|
|
25
|
+
let merge_options = require("merge-options");
|
|
26
|
+
merge_options = __toESM(merge_options, 1);
|
|
27
|
+
//#region src/middleware/middleware.ts
|
|
28
|
+
const exposeOnLocals = ({ app, key, value }) => {
|
|
29
|
+
if (app?.locals) app.locals[key] = value;
|
|
30
|
+
};
|
|
31
|
+
const extractPath = (req) => req.originalUrl || req.url;
|
|
32
|
+
let recordRequest;
|
|
33
|
+
let upMetric;
|
|
34
|
+
const getRequestRecorder = () => recordRequest;
|
|
35
|
+
const signalIsUp = () => {
|
|
36
|
+
if (!upMetric) return;
|
|
37
|
+
for (const upMetricType of upMetric) upMetricType.set(1);
|
|
38
|
+
};
|
|
39
|
+
const signalIsNotUp = () => {
|
|
40
|
+
if (!upMetric) return;
|
|
41
|
+
for (const upMetricType of upMetric) upMetricType.set(0);
|
|
42
|
+
};
|
|
43
|
+
const createMiddleware = ({ app, options } = {
|
|
44
|
+
app: void 0,
|
|
45
|
+
options: void 0
|
|
46
|
+
}) => {
|
|
47
|
+
const allDefaultedOptions = (0, merge_options.default)(_promster_metrics.createHttpMetrics.defaultOptions, _promster_metrics.createGcMetrics.defaultOptions, _promster_metrics.createRequestRecorder.defaultOptions, _promster_metrics.createGcObserver.defaultOptions, _promster_metrics.defaultNormalizers, options);
|
|
48
|
+
const shouldSkipMetricsByEnvironment = (0, _promster_metrics.skipMetricsInEnvironment)(allDefaultedOptions);
|
|
49
|
+
const httpMetrics = (0, _promster_metrics.createHttpMetrics)(allDefaultedOptions);
|
|
50
|
+
const gcMetrics = (0, _promster_metrics.createGcMetrics)(allDefaultedOptions);
|
|
51
|
+
const observeGc = (0, _promster_metrics.createGcObserver)(gcMetrics, allDefaultedOptions);
|
|
52
|
+
recordRequest = (0, _promster_metrics.createRequestRecorder)(httpMetrics, allDefaultedOptions);
|
|
53
|
+
upMetric = gcMetrics?.up;
|
|
54
|
+
exposeOnLocals({
|
|
55
|
+
app,
|
|
56
|
+
key: "Prometheus",
|
|
57
|
+
value: _promster_metrics.Prometheus
|
|
58
|
+
});
|
|
59
|
+
exposeOnLocals({
|
|
60
|
+
app,
|
|
61
|
+
key: "recordRequest",
|
|
62
|
+
value: recordRequest
|
|
63
|
+
});
|
|
64
|
+
if (!shouldSkipMetricsByEnvironment) observeGc();
|
|
65
|
+
signalIsUp();
|
|
66
|
+
return (request, response, next) => {
|
|
67
|
+
const requestTiming = _promster_metrics.timing.start();
|
|
68
|
+
response.on("finish", () => {
|
|
69
|
+
const labels = Object.assign({}, {
|
|
70
|
+
method: allDefaultedOptions.normalizeMethod(request.method, {
|
|
71
|
+
req: request,
|
|
72
|
+
res: response
|
|
73
|
+
}),
|
|
74
|
+
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, {
|
|
75
|
+
req: request,
|
|
76
|
+
res: response
|
|
77
|
+
}),
|
|
78
|
+
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
|
79
|
+
req: request,
|
|
80
|
+
res: response
|
|
81
|
+
})
|
|
82
|
+
}, allDefaultedOptions.getLabelValues?.(request, response));
|
|
83
|
+
const shouldSkipByRequest = allDefaultedOptions.skip?.(request, response, labels);
|
|
84
|
+
const requestContentLength = Number(request.headers["content-length"] ?? 0);
|
|
85
|
+
const responseContentLength = Number(response.getHeader("content-length") ?? 0);
|
|
86
|
+
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) recordRequest(requestTiming, {
|
|
87
|
+
labels,
|
|
88
|
+
requestContentLength,
|
|
89
|
+
responseContentLength
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
next();
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
exports.Prometheus = _promster_metrics.Prometheus;
|
|
97
|
+
exports.createMiddleware = createMiddleware;
|
|
98
|
+
exports.defaultNormalizers = _promster_metrics.defaultNormalizers;
|
|
99
|
+
exports.defaultRegister = _promster_metrics.defaultRegister;
|
|
100
|
+
exports.getContentType = _promster_metrics.getContentType;
|
|
101
|
+
exports.getRequestRecorder = getRequestRecorder;
|
|
102
|
+
exports.getSummary = _promster_metrics.getSummary;
|
|
103
|
+
exports.signalIsNotUp = signalIsNotUp;
|
|
104
|
+
exports.signalIsUp = signalIsUp;
|
|
105
|
+
exports.timing = _promster_metrics.timing;
|
|
106
|
+
|
|
107
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["createHttpMetrics","createGcMetrics","createRequestRecorder","createGcObserver","defaultNormalizers","Prometheus","timing"],"sources":["../src/middleware/middleware.ts"],"sourcesContent":["import type { TRequestRecorder } from '@promster/metrics';\nimport {\n createGcMetrics,\n createGcObserver,\n createHttpMetrics,\n createRequestRecorder,\n defaultNormalizers,\n Prometheus,\n skipMetricsInEnvironment,\n timing,\n} from '@promster/metrics';\nimport type {\n TDefaultedPromsterOptions,\n TGcMetrics,\n THttpMetrics,\n TLabelValues,\n TOptionalPromsterOptions,\n} from '@promster/types';\nimport type { Application, NextFunction, Request, Response } from 'express';\nimport merge from 'merge-options';\n\ninterface TApp extends Application {\n locals: Record<string, unknown>;\n}\n\ntype TLocaleTarget = {\n app?: TApp;\n key: string;\n value: typeof Prometheus | TRequestRecorder;\n};\nconst exposeOnLocals = ({ app, key, value }: TLocaleTarget) => {\n if (app?.locals) {\n app.locals[key] = value;\n }\n};\n\nconst extractPath = (req: Request) => req.originalUrl || req.url;\n\nlet recordRequest: TRequestRecorder;\nlet upMetric: TGcMetrics['up'];\n\nconst getRequestRecorder = () => recordRequest;\nconst signalIsUp = () => {\n if (!upMetric) {\n return;\n }\n\n for (const upMetricType of upMetric) {\n upMetricType.set(1);\n }\n};\n\nconst signalIsNotUp = () => {\n if (!upMetric) {\n return;\n }\n\n for (const upMetricType of upMetric) {\n upMetricType.set(0);\n }\n};\n\ntype TSkipFunction = <TRequest = Request, TResponse = Response>(\n _req: TRequest,\n _res: TResponse,\n _labels: TLabelValues,\n) => boolean;\nexport type TPromsterOptions = TOptionalPromsterOptions & {\n skip?: TSkipFunction;\n};\ntype TMiddlewareOptions = {\n app?: TApp;\n options?: TPromsterOptions;\n};\nconst createMiddleware = (\n { app, options }: TMiddlewareOptions = { app: undefined, options: undefined },\n) => {\n const allDefaultedOptions: TDefaultedPromsterOptions & {\n skip?: TSkipFunction;\n } = merge(\n createHttpMetrics.defaultOptions,\n createGcMetrics.defaultOptions,\n createRequestRecorder.defaultOptions,\n // @ts-expect-error\n createGcObserver.defaultOptions,\n defaultNormalizers,\n options,\n );\n\n const shouldSkipMetricsByEnvironment =\n skipMetricsInEnvironment(allDefaultedOptions);\n\n const httpMetrics: THttpMetrics = createHttpMetrics(allDefaultedOptions);\n const gcMetrics: TGcMetrics = createGcMetrics(allDefaultedOptions);\n const observeGc = createGcObserver(gcMetrics, allDefaultedOptions);\n\n recordRequest = createRequestRecorder(httpMetrics, allDefaultedOptions);\n upMetric = gcMetrics?.up;\n\n exposeOnLocals({ app, key: 'Prometheus', value: Prometheus });\n exposeOnLocals({ app, key: 'recordRequest', value: recordRequest });\n\n if (!shouldSkipMetricsByEnvironment) {\n observeGc();\n }\n\n signalIsUp();\n\n return (request: Request, response: Response, next: NextFunction) => {\n const requestTiming = timing.start();\n\n response.on('finish', () => {\n const labels = Object.assign(\n {},\n {\n method: allDefaultedOptions.normalizeMethod(request.method, {\n req: request,\n res: response,\n }),\n status_code: allDefaultedOptions.normalizeStatusCode(\n response.statusCode,\n { req: request, res: response },\n ),\n path: allDefaultedOptions.normalizePath(extractPath(request), {\n req: request,\n res: response,\n }),\n },\n allDefaultedOptions.getLabelValues?.(request, response),\n );\n\n const shouldSkipByRequest = allDefaultedOptions.skip?.(\n request,\n response,\n labels,\n );\n\n const requestContentLength = Number(\n request.headers['content-length'] ?? 0,\n );\n const responseContentLength = Number(\n response.getHeader('content-length') ?? 0,\n );\n\n if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {\n recordRequest(requestTiming, {\n labels,\n requestContentLength,\n responseContentLength,\n });\n }\n });\n\n next();\n };\n};\n\nexport {\n createMiddleware,\n exposeOnLocals,\n extractPath,\n getRequestRecorder,\n signalIsUp,\n signalIsNotUp,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAM,kBAAkB,EAAE,KAAK,KAAK,YAA2B;CAC7D,IAAI,KAAK,QACP,IAAI,OAAO,OAAO;AAEtB;AAEA,MAAM,eAAe,QAAiB,IAAI,eAAe,IAAI;AAE7D,IAAI;AACJ,IAAI;AAEJ,MAAM,2BAA2B;AACjC,MAAM,mBAAmB;CACvB,IAAI,CAAC,UACH;CAGF,KAAK,MAAM,gBAAgB,UACzB,aAAa,IAAI,CAAC;AAEtB;AAEA,MAAM,sBAAsB;CAC1B,IAAI,CAAC,UACH;CAGF,KAAK,MAAM,gBAAgB,UACzB,aAAa,IAAI,CAAC;AAEtB;AAcA,MAAM,oBACJ,EAAE,KAAK,YAAgC;CAAE,KAAK,KAAA;CAAW,SAAS,KAAA;AAAU,MACzE;CACH,MAAM,uBAAA,GAAA,cAAA,QAAA,CAGJA,kBAAAA,kBAAkB,gBAClBC,kBAAAA,gBAAgB,gBAChBC,kBAAAA,sBAAsB,gBAEtBC,kBAAAA,iBAAiB,gBACjBC,kBAAAA,oBACA,OACF;CAEA,MAAM,kCAAA,GAAA,kBAAA,yBAAA,CACqB,mBAAmB;CAE9C,MAAM,eAAA,GAAA,kBAAA,kBAAA,CAA8C,mBAAmB;CACvE,MAAM,aAAA,GAAA,kBAAA,gBAAA,CAAwC,mBAAmB;CACjE,MAAM,aAAA,GAAA,kBAAA,iBAAA,CAA6B,WAAW,mBAAmB;CAEjE,iBAAA,GAAA,kBAAA,sBAAA,CAAsC,aAAa,mBAAmB;CACtE,WAAW,WAAW;CAEtB,eAAe;EAAE;EAAK,KAAK;EAAc,OAAOC,kBAAAA;CAAW,CAAC;CAC5D,eAAe;EAAE;EAAK,KAAK;EAAiB,OAAO;CAAc,CAAC;CAElE,IAAI,CAAC,gCACH,UAAU;CAGZ,WAAW;CAEX,QAAQ,SAAkB,UAAoB,SAAuB;EACnE,MAAM,gBAAgBC,kBAAAA,OAAO,MAAM;EAEnC,SAAS,GAAG,gBAAgB;GAC1B,MAAM,SAAS,OAAO,OACpB,CAAC,GACD;IACE,QAAQ,oBAAoB,gBAAgB,QAAQ,QAAQ;KAC1D,KAAK;KACL,KAAK;IACP,CAAC;IACD,aAAa,oBAAoB,oBAC/B,SAAS,YACT;KAAE,KAAK;KAAS,KAAK;IAAS,CAChC;IACA,MAAM,oBAAoB,cAAc,YAAY,OAAO,GAAG;KAC5D,KAAK;KACL,KAAK;IACP,CAAC;GACH,GACA,oBAAoB,iBAAiB,SAAS,QAAQ,CACxD;GAEA,MAAM,sBAAsB,oBAAoB,OAC9C,SACA,UACA,MACF;GAEA,MAAM,uBAAuB,OAC3B,QAAQ,QAAQ,qBAAqB,CACvC;GACA,MAAM,wBAAwB,OAC5B,SAAS,UAAU,gBAAgB,KAAK,CAC1C;GAEA,IAAI,CAAC,uBAAuB,CAAC,gCAC3B,cAAc,eAAe;IAC3B;IACA;IACA;GACF,CAAC;EAEL,CAAC;EAED,KAAK;CACP;AACF"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Prometheus, TRequestRecorder, defaultNormalizers, defaultRegister, getContentType, getSummary, timing } from "@promster/metrics";
|
|
2
|
+
import { TLabelValues, TOptionalPromsterOptions } from "@promster/types";
|
|
3
|
+
import { Application, NextFunction, Request, Response } from "express";
|
|
4
|
+
export type * from "@promster/types";
|
|
5
|
+
|
|
6
|
+
//#region src/middleware/middleware.d.ts
|
|
7
|
+
interface TApp extends Application {
|
|
8
|
+
locals: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
declare const getRequestRecorder: () => TRequestRecorder;
|
|
11
|
+
declare const signalIsUp: () => void;
|
|
12
|
+
declare const signalIsNotUp: () => void;
|
|
13
|
+
type TSkipFunction = <TRequest = Request, TResponse = Response>(_req: TRequest, _res: TResponse, _labels: TLabelValues) => boolean;
|
|
14
|
+
type TPromsterOptions = TOptionalPromsterOptions & {
|
|
15
|
+
skip?: TSkipFunction;
|
|
16
|
+
};
|
|
17
|
+
type TMiddlewareOptions = {
|
|
18
|
+
app?: TApp;
|
|
19
|
+
options?: TPromsterOptions;
|
|
20
|
+
};
|
|
21
|
+
declare const createMiddleware: ({
|
|
22
|
+
app,
|
|
23
|
+
options
|
|
24
|
+
}?: TMiddlewareOptions) => (request: Request, response: Response, next: NextFunction) => void;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { Prometheus, type TPromsterOptions, createMiddleware, defaultNormalizers, defaultRegister, getContentType, getRequestRecorder, getSummary, signalIsNotUp, signalIsUp, timing };
|
|
27
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Prometheus, Prometheus as Prometheus$1, TRequestRecorder, defaultNormalizers, defaultRegister, getContentType, getSummary, timing } from "@promster/metrics";
|
|
2
|
+
import { TLabelValues, TOptionalPromsterOptions } from "@promster/types";
|
|
3
|
+
import { Application, NextFunction, Request, Response } from "express";
|
|
4
|
+
export type * from "@promster/types";
|
|
5
|
+
|
|
6
|
+
//#region src/middleware/middleware.d.ts
|
|
7
|
+
interface TApp extends Application {
|
|
8
|
+
locals: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
declare const getRequestRecorder: () => TRequestRecorder;
|
|
11
|
+
declare const signalIsUp: () => void;
|
|
12
|
+
declare const signalIsNotUp: () => void;
|
|
13
|
+
type TSkipFunction = <TRequest = Request, TResponse = Response>(_req: TRequest, _res: TResponse, _labels: TLabelValues) => boolean;
|
|
14
|
+
type TPromsterOptions = TOptionalPromsterOptions & {
|
|
15
|
+
skip?: TSkipFunction;
|
|
16
|
+
};
|
|
17
|
+
type TMiddlewareOptions = {
|
|
18
|
+
app?: TApp;
|
|
19
|
+
options?: TPromsterOptions;
|
|
20
|
+
};
|
|
21
|
+
declare const createMiddleware: ({
|
|
22
|
+
app,
|
|
23
|
+
options
|
|
24
|
+
}?: TMiddlewareOptions) => (request: Request, response: Response, next: NextFunction) => void;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { Prometheus, type TPromsterOptions, createMiddleware, defaultNormalizers, defaultRegister, getContentType, getRequestRecorder, getSummary, signalIsNotUp, signalIsUp, timing };
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Prometheus, Prometheus as Prometheus$1, createGcMetrics, createGcObserver, createHttpMetrics, createRequestRecorder, defaultNormalizers, defaultNormalizers as defaultNormalizers$1, defaultRegister, getContentType, getSummary, skipMetricsInEnvironment, timing, timing as timing$1 } from "@promster/metrics";
|
|
2
|
+
import merge from "merge-options";
|
|
3
|
+
//#region src/middleware/middleware.ts
|
|
4
|
+
const exposeOnLocals = ({ app, key, value }) => {
|
|
5
|
+
if (app?.locals) app.locals[key] = value;
|
|
6
|
+
};
|
|
7
|
+
const extractPath = (req) => req.originalUrl || req.url;
|
|
8
|
+
let recordRequest;
|
|
9
|
+
let upMetric;
|
|
10
|
+
const getRequestRecorder = () => recordRequest;
|
|
11
|
+
const signalIsUp = () => {
|
|
12
|
+
if (!upMetric) return;
|
|
13
|
+
for (const upMetricType of upMetric) upMetricType.set(1);
|
|
14
|
+
};
|
|
15
|
+
const signalIsNotUp = () => {
|
|
16
|
+
if (!upMetric) return;
|
|
17
|
+
for (const upMetricType of upMetric) upMetricType.set(0);
|
|
18
|
+
};
|
|
19
|
+
const createMiddleware = ({ app, options } = {
|
|
20
|
+
app: void 0,
|
|
21
|
+
options: void 0
|
|
22
|
+
}) => {
|
|
23
|
+
const allDefaultedOptions = merge(createHttpMetrics.defaultOptions, createGcMetrics.defaultOptions, createRequestRecorder.defaultOptions, createGcObserver.defaultOptions, defaultNormalizers$1, options);
|
|
24
|
+
const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(allDefaultedOptions);
|
|
25
|
+
const httpMetrics = createHttpMetrics(allDefaultedOptions);
|
|
26
|
+
const gcMetrics = createGcMetrics(allDefaultedOptions);
|
|
27
|
+
const observeGc = createGcObserver(gcMetrics, allDefaultedOptions);
|
|
28
|
+
recordRequest = createRequestRecorder(httpMetrics, allDefaultedOptions);
|
|
29
|
+
upMetric = gcMetrics?.up;
|
|
30
|
+
exposeOnLocals({
|
|
31
|
+
app,
|
|
32
|
+
key: "Prometheus",
|
|
33
|
+
value: Prometheus$1
|
|
34
|
+
});
|
|
35
|
+
exposeOnLocals({
|
|
36
|
+
app,
|
|
37
|
+
key: "recordRequest",
|
|
38
|
+
value: recordRequest
|
|
39
|
+
});
|
|
40
|
+
if (!shouldSkipMetricsByEnvironment) observeGc();
|
|
41
|
+
signalIsUp();
|
|
42
|
+
return (request, response, next) => {
|
|
43
|
+
const requestTiming = timing$1.start();
|
|
44
|
+
response.on("finish", () => {
|
|
45
|
+
const labels = Object.assign({}, {
|
|
46
|
+
method: allDefaultedOptions.normalizeMethod(request.method, {
|
|
47
|
+
req: request,
|
|
48
|
+
res: response
|
|
49
|
+
}),
|
|
50
|
+
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, {
|
|
51
|
+
req: request,
|
|
52
|
+
res: response
|
|
53
|
+
}),
|
|
54
|
+
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
|
55
|
+
req: request,
|
|
56
|
+
res: response
|
|
57
|
+
})
|
|
58
|
+
}, allDefaultedOptions.getLabelValues?.(request, response));
|
|
59
|
+
const shouldSkipByRequest = allDefaultedOptions.skip?.(request, response, labels);
|
|
60
|
+
const requestContentLength = Number(request.headers["content-length"] ?? 0);
|
|
61
|
+
const responseContentLength = Number(response.getHeader("content-length") ?? 0);
|
|
62
|
+
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) recordRequest(requestTiming, {
|
|
63
|
+
labels,
|
|
64
|
+
requestContentLength,
|
|
65
|
+
responseContentLength
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
next();
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
//#endregion
|
|
72
|
+
export { Prometheus, createMiddleware, defaultNormalizers, defaultRegister, getContentType, getRequestRecorder, getSummary, signalIsNotUp, signalIsUp, timing };
|
|
73
|
+
|
|
74
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["defaultNormalizers","Prometheus","timing"],"sources":["../src/middleware/middleware.ts"],"sourcesContent":["import type { TRequestRecorder } from '@promster/metrics';\nimport {\n createGcMetrics,\n createGcObserver,\n createHttpMetrics,\n createRequestRecorder,\n defaultNormalizers,\n Prometheus,\n skipMetricsInEnvironment,\n timing,\n} from '@promster/metrics';\nimport type {\n TDefaultedPromsterOptions,\n TGcMetrics,\n THttpMetrics,\n TLabelValues,\n TOptionalPromsterOptions,\n} from '@promster/types';\nimport type { Application, NextFunction, Request, Response } from 'express';\nimport merge from 'merge-options';\n\ninterface TApp extends Application {\n locals: Record<string, unknown>;\n}\n\ntype TLocaleTarget = {\n app?: TApp;\n key: string;\n value: typeof Prometheus | TRequestRecorder;\n};\nconst exposeOnLocals = ({ app, key, value }: TLocaleTarget) => {\n if (app?.locals) {\n app.locals[key] = value;\n }\n};\n\nconst extractPath = (req: Request) => req.originalUrl || req.url;\n\nlet recordRequest: TRequestRecorder;\nlet upMetric: TGcMetrics['up'];\n\nconst getRequestRecorder = () => recordRequest;\nconst signalIsUp = () => {\n if (!upMetric) {\n return;\n }\n\n for (const upMetricType of upMetric) {\n upMetricType.set(1);\n }\n};\n\nconst signalIsNotUp = () => {\n if (!upMetric) {\n return;\n }\n\n for (const upMetricType of upMetric) {\n upMetricType.set(0);\n }\n};\n\ntype TSkipFunction = <TRequest = Request, TResponse = Response>(\n _req: TRequest,\n _res: TResponse,\n _labels: TLabelValues,\n) => boolean;\nexport type TPromsterOptions = TOptionalPromsterOptions & {\n skip?: TSkipFunction;\n};\ntype TMiddlewareOptions = {\n app?: TApp;\n options?: TPromsterOptions;\n};\nconst createMiddleware = (\n { app, options }: TMiddlewareOptions = { app: undefined, options: undefined },\n) => {\n const allDefaultedOptions: TDefaultedPromsterOptions & {\n skip?: TSkipFunction;\n } = merge(\n createHttpMetrics.defaultOptions,\n createGcMetrics.defaultOptions,\n createRequestRecorder.defaultOptions,\n // @ts-expect-error\n createGcObserver.defaultOptions,\n defaultNormalizers,\n options,\n );\n\n const shouldSkipMetricsByEnvironment =\n skipMetricsInEnvironment(allDefaultedOptions);\n\n const httpMetrics: THttpMetrics = createHttpMetrics(allDefaultedOptions);\n const gcMetrics: TGcMetrics = createGcMetrics(allDefaultedOptions);\n const observeGc = createGcObserver(gcMetrics, allDefaultedOptions);\n\n recordRequest = createRequestRecorder(httpMetrics, allDefaultedOptions);\n upMetric = gcMetrics?.up;\n\n exposeOnLocals({ app, key: 'Prometheus', value: Prometheus });\n exposeOnLocals({ app, key: 'recordRequest', value: recordRequest });\n\n if (!shouldSkipMetricsByEnvironment) {\n observeGc();\n }\n\n signalIsUp();\n\n return (request: Request, response: Response, next: NextFunction) => {\n const requestTiming = timing.start();\n\n response.on('finish', () => {\n const labels = Object.assign(\n {},\n {\n method: allDefaultedOptions.normalizeMethod(request.method, {\n req: request,\n res: response,\n }),\n status_code: allDefaultedOptions.normalizeStatusCode(\n response.statusCode,\n { req: request, res: response },\n ),\n path: allDefaultedOptions.normalizePath(extractPath(request), {\n req: request,\n res: response,\n }),\n },\n allDefaultedOptions.getLabelValues?.(request, response),\n );\n\n const shouldSkipByRequest = allDefaultedOptions.skip?.(\n request,\n response,\n labels,\n );\n\n const requestContentLength = Number(\n request.headers['content-length'] ?? 0,\n );\n const responseContentLength = Number(\n response.getHeader('content-length') ?? 0,\n );\n\n if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {\n recordRequest(requestTiming, {\n labels,\n requestContentLength,\n responseContentLength,\n });\n }\n });\n\n next();\n };\n};\n\nexport {\n createMiddleware,\n exposeOnLocals,\n extractPath,\n getRequestRecorder,\n signalIsUp,\n signalIsNotUp,\n};\n"],"mappings":";;;AA8BA,MAAM,kBAAkB,EAAE,KAAK,KAAK,YAA2B;CAC7D,IAAI,KAAK,QACP,IAAI,OAAO,OAAO;AAEtB;AAEA,MAAM,eAAe,QAAiB,IAAI,eAAe,IAAI;AAE7D,IAAI;AACJ,IAAI;AAEJ,MAAM,2BAA2B;AACjC,MAAM,mBAAmB;CACvB,IAAI,CAAC,UACH;CAGF,KAAK,MAAM,gBAAgB,UACzB,aAAa,IAAI,CAAC;AAEtB;AAEA,MAAM,sBAAsB;CAC1B,IAAI,CAAC,UACH;CAGF,KAAK,MAAM,gBAAgB,UACzB,aAAa,IAAI,CAAC;AAEtB;AAcA,MAAM,oBACJ,EAAE,KAAK,YAAgC;CAAE,KAAK,KAAA;CAAW,SAAS,KAAA;AAAU,MACzE;CACH,MAAM,sBAEF,MACF,kBAAkB,gBAClB,gBAAgB,gBAChB,sBAAsB,gBAEtB,iBAAiB,gBACjBA,sBACA,OACF;CAEA,MAAM,iCACJ,yBAAyB,mBAAmB;CAE9C,MAAM,cAA4B,kBAAkB,mBAAmB;CACvE,MAAM,YAAwB,gBAAgB,mBAAmB;CACjE,MAAM,YAAY,iBAAiB,WAAW,mBAAmB;CAEjE,gBAAgB,sBAAsB,aAAa,mBAAmB;CACtE,WAAW,WAAW;CAEtB,eAAe;EAAE;EAAK,KAAK;EAAc,OAAOC;CAAW,CAAC;CAC5D,eAAe;EAAE;EAAK,KAAK;EAAiB,OAAO;CAAc,CAAC;CAElE,IAAI,CAAC,gCACH,UAAU;CAGZ,WAAW;CAEX,QAAQ,SAAkB,UAAoB,SAAuB;EACnE,MAAM,gBAAgBC,SAAO,MAAM;EAEnC,SAAS,GAAG,gBAAgB;GAC1B,MAAM,SAAS,OAAO,OACpB,CAAC,GACD;IACE,QAAQ,oBAAoB,gBAAgB,QAAQ,QAAQ;KAC1D,KAAK;KACL,KAAK;IACP,CAAC;IACD,aAAa,oBAAoB,oBAC/B,SAAS,YACT;KAAE,KAAK;KAAS,KAAK;IAAS,CAChC;IACA,MAAM,oBAAoB,cAAc,YAAY,OAAO,GAAG;KAC5D,KAAK;KACL,KAAK;IACP,CAAC;GACH,GACA,oBAAoB,iBAAiB,SAAS,QAAQ,CACxD;GAEA,MAAM,sBAAsB,oBAAoB,OAC9C,SACA,UACA,MACF;GAEA,MAAM,uBAAuB,OAC3B,QAAQ,QAAQ,qBAAqB,CACvC;GACA,MAAM,wBAAwB,OAC5B,SAAS,UAAU,gBAAgB,KAAK,CAC1C;GAEA,IAAI,CAAC,uBAAuB,CAAC,gCAC3B,cAAc,eAAe;IAC3B;IACA;IACA;GACF,CAAC;EAEL,CAAC;EAED,KAAK;CACP;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@promster/express",
|
|
3
|
-
"version": "15.5.
|
|
3
|
+
"version": "15.5.2",
|
|
4
4
|
"description": "Express server integrations of promster",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"continousdelivery",
|
|
@@ -22,29 +22,51 @@
|
|
|
22
22
|
"readme.md",
|
|
23
23
|
"package.json",
|
|
24
24
|
"LICENSE",
|
|
25
|
-
"dist/**"
|
|
26
|
-
"modules/**"
|
|
25
|
+
"dist/**"
|
|
27
26
|
],
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/index.d.cts",
|
|
39
|
+
"default": "./dist/index.cjs"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
31
43
|
"publishConfig": {
|
|
32
44
|
"access": "public"
|
|
33
45
|
},
|
|
34
46
|
"dependencies": {
|
|
35
47
|
"merge-options": "3.0.4",
|
|
36
48
|
"tslib": "2.8.1",
|
|
37
|
-
"@promster/metrics": "15.5.
|
|
49
|
+
"@promster/metrics": "15.5.2"
|
|
38
50
|
},
|
|
39
51
|
"devDependencies": {
|
|
40
52
|
"@types/express": "5.0.6",
|
|
41
53
|
"express": "5.2.1",
|
|
42
54
|
"parse-prometheus-text-format": "1.1.1",
|
|
43
|
-
"
|
|
44
|
-
"@promster/
|
|
55
|
+
"tsdown": "0.22.2",
|
|
56
|
+
"@promster/server": "15.5.2",
|
|
57
|
+
"@promster/tsconfig": "15.5.2",
|
|
58
|
+
"@promster/tsdown-config": "15.5.2",
|
|
59
|
+
"@promster/types": "15.5.2"
|
|
45
60
|
},
|
|
46
61
|
"engines": {
|
|
47
62
|
"node": ">=20",
|
|
48
63
|
"npm": ">=8"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsdown",
|
|
67
|
+
"check-types": "tsc --noEmit",
|
|
68
|
+
"test": "vitest --run",
|
|
69
|
+
"test:watch": "vitest",
|
|
70
|
+
"dev": "tsdown --watch"
|
|
49
71
|
}
|
|
50
72
|
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { defaultNormalizers, defaultRegister, getContentType, getSummary, Prometheus, timing } from '@promster/metrics';
|
|
2
|
-
import { createMiddleware, getRequestRecorder, signalIsNotUp, signalIsUp, type TPromsterOptions } from "./middleware/index.js";
|
|
3
|
-
export { type TPromsterOptions, createMiddleware, getRequestRecorder, signalIsUp, signalIsNotUp, getSummary, getContentType, Prometheus, defaultRegister, defaultNormalizers, timing, };
|
|
4
|
-
export type * from '@promster/types';
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { TRequestRecorder } from '@promster/metrics';
|
|
2
|
-
import { Prometheus } from '@promster/metrics';
|
|
3
|
-
import type { TLabelValues, TOptionalPromsterOptions } from '@promster/types';
|
|
4
|
-
import type { Application, NextFunction, Request, Response } from 'express';
|
|
5
|
-
interface TApp extends Application {
|
|
6
|
-
locals: Record<string, unknown>;
|
|
7
|
-
}
|
|
8
|
-
type TLocaleTarget = {
|
|
9
|
-
app?: TApp;
|
|
10
|
-
key: string;
|
|
11
|
-
value: typeof Prometheus | TRequestRecorder;
|
|
12
|
-
};
|
|
13
|
-
declare const exposeOnLocals: ({ app, key, value }: TLocaleTarget) => void;
|
|
14
|
-
declare const extractPath: (req: Request) => string;
|
|
15
|
-
declare const getRequestRecorder: () => TRequestRecorder;
|
|
16
|
-
declare const signalIsUp: () => void;
|
|
17
|
-
declare const signalIsNotUp: () => void;
|
|
18
|
-
type TSkipFunction = <TRequest = Request, TResponse = Response>(_req: TRequest, _res: TResponse, _labels: TLabelValues) => boolean;
|
|
19
|
-
export type TPromsterOptions = TOptionalPromsterOptions & {
|
|
20
|
-
skip?: TSkipFunction;
|
|
21
|
-
};
|
|
22
|
-
type TMiddlewareOptions = {
|
|
23
|
-
app?: TApp;
|
|
24
|
-
options?: TPromsterOptions;
|
|
25
|
-
};
|
|
26
|
-
declare const createMiddleware: ({ app, options }?: TMiddlewareOptions) => (request: Request, response: Response, next: NextFunction) => void;
|
|
27
|
-
export { createMiddleware, exposeOnLocals, extractPath, getRequestRecorder, signalIsUp, signalIsNotUp, };
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export * from "./declarations/src/index.js";
|
|
2
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvbXN0ZXItZXhwcmVzcy5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var metrics = require('@promster/metrics');
|
|
6
|
-
var merge = require('merge-options');
|
|
7
|
-
|
|
8
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
9
|
-
|
|
10
|
-
var merge__default = /*#__PURE__*/_interopDefault(merge);
|
|
11
|
-
|
|
12
|
-
const exposeOnLocals = ({
|
|
13
|
-
app,
|
|
14
|
-
key,
|
|
15
|
-
value
|
|
16
|
-
}) => {
|
|
17
|
-
if (app !== null && app !== void 0 && app.locals) {
|
|
18
|
-
app.locals[key] = value;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
const extractPath = req => req.originalUrl || req.url;
|
|
22
|
-
let recordRequest;
|
|
23
|
-
let upMetric;
|
|
24
|
-
const getRequestRecorder = () => recordRequest;
|
|
25
|
-
const signalIsUp = () => {
|
|
26
|
-
if (!upMetric) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
for (const upMetricType of upMetric) {
|
|
30
|
-
upMetricType.set(1);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
const signalIsNotUp = () => {
|
|
34
|
-
if (!upMetric) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
for (const upMetricType of upMetric) {
|
|
38
|
-
upMetricType.set(0);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
const createMiddleware = ({
|
|
42
|
-
app,
|
|
43
|
-
options
|
|
44
|
-
} = {
|
|
45
|
-
app: undefined,
|
|
46
|
-
options: undefined
|
|
47
|
-
}) => {
|
|
48
|
-
const allDefaultedOptions = merge__default["default"](metrics.createHttpMetrics.defaultOptions, metrics.createGcMetrics.defaultOptions, metrics.createRequestRecorder.defaultOptions,
|
|
49
|
-
// @ts-expect-error
|
|
50
|
-
metrics.createGcObserver.defaultOptions, metrics.defaultNormalizers, options);
|
|
51
|
-
const shouldSkipMetricsByEnvironment = metrics.skipMetricsInEnvironment(allDefaultedOptions);
|
|
52
|
-
const httpMetrics = metrics.createHttpMetrics(allDefaultedOptions);
|
|
53
|
-
const gcMetrics = metrics.createGcMetrics(allDefaultedOptions);
|
|
54
|
-
const observeGc = metrics.createGcObserver(gcMetrics, allDefaultedOptions);
|
|
55
|
-
recordRequest = metrics.createRequestRecorder(httpMetrics, allDefaultedOptions);
|
|
56
|
-
upMetric = gcMetrics === null || gcMetrics === void 0 ? void 0 : gcMetrics.up;
|
|
57
|
-
exposeOnLocals({
|
|
58
|
-
app,
|
|
59
|
-
key: 'Prometheus',
|
|
60
|
-
value: metrics.Prometheus
|
|
61
|
-
});
|
|
62
|
-
exposeOnLocals({
|
|
63
|
-
app,
|
|
64
|
-
key: 'recordRequest',
|
|
65
|
-
value: recordRequest
|
|
66
|
-
});
|
|
67
|
-
if (!shouldSkipMetricsByEnvironment) {
|
|
68
|
-
observeGc();
|
|
69
|
-
}
|
|
70
|
-
signalIsUp();
|
|
71
|
-
return (request, response, next) => {
|
|
72
|
-
const requestTiming = metrics.timing.start();
|
|
73
|
-
response.on('finish', () => {
|
|
74
|
-
var _allDefaultedOptions$, _allDefaultedOptions$2, _request$headers$cont, _response$getHeader;
|
|
75
|
-
const labels = Object.assign({}, {
|
|
76
|
-
method: allDefaultedOptions.normalizeMethod(request.method, {
|
|
77
|
-
req: request,
|
|
78
|
-
res: response
|
|
79
|
-
}),
|
|
80
|
-
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, {
|
|
81
|
-
req: request,
|
|
82
|
-
res: response
|
|
83
|
-
}),
|
|
84
|
-
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
|
85
|
-
req: request,
|
|
86
|
-
res: response
|
|
87
|
-
})
|
|
88
|
-
}, (_allDefaultedOptions$ = allDefaultedOptions.getLabelValues) === null || _allDefaultedOptions$ === void 0 ? void 0 : _allDefaultedOptions$.call(allDefaultedOptions, request, response));
|
|
89
|
-
const shouldSkipByRequest = (_allDefaultedOptions$2 = allDefaultedOptions.skip) === null || _allDefaultedOptions$2 === void 0 ? void 0 : _allDefaultedOptions$2.call(allDefaultedOptions, request, response, labels);
|
|
90
|
-
const requestContentLength = Number((_request$headers$cont = request.headers['content-length']) !== null && _request$headers$cont !== void 0 ? _request$headers$cont : 0);
|
|
91
|
-
const responseContentLength = Number((_response$getHeader = response.getHeader('content-length')) !== null && _response$getHeader !== void 0 ? _response$getHeader : 0);
|
|
92
|
-
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {
|
|
93
|
-
recordRequest(requestTiming, {
|
|
94
|
-
labels,
|
|
95
|
-
requestContentLength,
|
|
96
|
-
responseContentLength
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
next();
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
Object.defineProperty(exports, 'Prometheus', {
|
|
105
|
-
enumerable: true,
|
|
106
|
-
get: function () { return metrics.Prometheus; }
|
|
107
|
-
});
|
|
108
|
-
Object.defineProperty(exports, 'defaultNormalizers', {
|
|
109
|
-
enumerable: true,
|
|
110
|
-
get: function () { return metrics.defaultNormalizers; }
|
|
111
|
-
});
|
|
112
|
-
Object.defineProperty(exports, 'defaultRegister', {
|
|
113
|
-
enumerable: true,
|
|
114
|
-
get: function () { return metrics.defaultRegister; }
|
|
115
|
-
});
|
|
116
|
-
Object.defineProperty(exports, 'getContentType', {
|
|
117
|
-
enumerable: true,
|
|
118
|
-
get: function () { return metrics.getContentType; }
|
|
119
|
-
});
|
|
120
|
-
Object.defineProperty(exports, 'getSummary', {
|
|
121
|
-
enumerable: true,
|
|
122
|
-
get: function () { return metrics.getSummary; }
|
|
123
|
-
});
|
|
124
|
-
Object.defineProperty(exports, 'timing', {
|
|
125
|
-
enumerable: true,
|
|
126
|
-
get: function () { return metrics.timing; }
|
|
127
|
-
});
|
|
128
|
-
exports.createMiddleware = createMiddleware;
|
|
129
|
-
exports.getRequestRecorder = getRequestRecorder;
|
|
130
|
-
exports.signalIsNotUp = signalIsNotUp;
|
|
131
|
-
exports.signalIsUp = signalIsUp;
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var metrics = require('@promster/metrics');
|
|
6
|
-
var merge = require('merge-options');
|
|
7
|
-
|
|
8
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
9
|
-
|
|
10
|
-
var merge__default = /*#__PURE__*/_interopDefault(merge);
|
|
11
|
-
|
|
12
|
-
const exposeOnLocals = ({
|
|
13
|
-
app,
|
|
14
|
-
key,
|
|
15
|
-
value
|
|
16
|
-
}) => {
|
|
17
|
-
if (app !== null && app !== void 0 && app.locals) {
|
|
18
|
-
app.locals[key] = value;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
const extractPath = req => req.originalUrl || req.url;
|
|
22
|
-
let recordRequest;
|
|
23
|
-
let upMetric;
|
|
24
|
-
const getRequestRecorder = () => recordRequest;
|
|
25
|
-
const signalIsUp = () => {
|
|
26
|
-
if (!upMetric) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
for (const upMetricType of upMetric) {
|
|
30
|
-
upMetricType.set(1);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
const signalIsNotUp = () => {
|
|
34
|
-
if (!upMetric) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
for (const upMetricType of upMetric) {
|
|
38
|
-
upMetricType.set(0);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
const createMiddleware = ({
|
|
42
|
-
app,
|
|
43
|
-
options
|
|
44
|
-
} = {
|
|
45
|
-
app: undefined,
|
|
46
|
-
options: undefined
|
|
47
|
-
}) => {
|
|
48
|
-
const allDefaultedOptions = merge__default["default"](metrics.createHttpMetrics.defaultOptions, metrics.createGcMetrics.defaultOptions, metrics.createRequestRecorder.defaultOptions,
|
|
49
|
-
// @ts-expect-error
|
|
50
|
-
metrics.createGcObserver.defaultOptions, metrics.defaultNormalizers, options);
|
|
51
|
-
const shouldSkipMetricsByEnvironment = metrics.skipMetricsInEnvironment(allDefaultedOptions);
|
|
52
|
-
const httpMetrics = metrics.createHttpMetrics(allDefaultedOptions);
|
|
53
|
-
const gcMetrics = metrics.createGcMetrics(allDefaultedOptions);
|
|
54
|
-
const observeGc = metrics.createGcObserver(gcMetrics, allDefaultedOptions);
|
|
55
|
-
recordRequest = metrics.createRequestRecorder(httpMetrics, allDefaultedOptions);
|
|
56
|
-
upMetric = gcMetrics === null || gcMetrics === void 0 ? void 0 : gcMetrics.up;
|
|
57
|
-
exposeOnLocals({
|
|
58
|
-
app,
|
|
59
|
-
key: 'Prometheus',
|
|
60
|
-
value: metrics.Prometheus
|
|
61
|
-
});
|
|
62
|
-
exposeOnLocals({
|
|
63
|
-
app,
|
|
64
|
-
key: 'recordRequest',
|
|
65
|
-
value: recordRequest
|
|
66
|
-
});
|
|
67
|
-
if (!shouldSkipMetricsByEnvironment) {
|
|
68
|
-
observeGc();
|
|
69
|
-
}
|
|
70
|
-
signalIsUp();
|
|
71
|
-
return (request, response, next) => {
|
|
72
|
-
const requestTiming = metrics.timing.start();
|
|
73
|
-
response.on('finish', () => {
|
|
74
|
-
var _allDefaultedOptions$, _allDefaultedOptions$2, _request$headers$cont, _response$getHeader;
|
|
75
|
-
const labels = Object.assign({}, {
|
|
76
|
-
method: allDefaultedOptions.normalizeMethod(request.method, {
|
|
77
|
-
req: request,
|
|
78
|
-
res: response
|
|
79
|
-
}),
|
|
80
|
-
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, {
|
|
81
|
-
req: request,
|
|
82
|
-
res: response
|
|
83
|
-
}),
|
|
84
|
-
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
|
85
|
-
req: request,
|
|
86
|
-
res: response
|
|
87
|
-
})
|
|
88
|
-
}, (_allDefaultedOptions$ = allDefaultedOptions.getLabelValues) === null || _allDefaultedOptions$ === void 0 ? void 0 : _allDefaultedOptions$.call(allDefaultedOptions, request, response));
|
|
89
|
-
const shouldSkipByRequest = (_allDefaultedOptions$2 = allDefaultedOptions.skip) === null || _allDefaultedOptions$2 === void 0 ? void 0 : _allDefaultedOptions$2.call(allDefaultedOptions, request, response, labels);
|
|
90
|
-
const requestContentLength = Number((_request$headers$cont = request.headers['content-length']) !== null && _request$headers$cont !== void 0 ? _request$headers$cont : 0);
|
|
91
|
-
const responseContentLength = Number((_response$getHeader = response.getHeader('content-length')) !== null && _response$getHeader !== void 0 ? _response$getHeader : 0);
|
|
92
|
-
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {
|
|
93
|
-
recordRequest(requestTiming, {
|
|
94
|
-
labels,
|
|
95
|
-
requestContentLength,
|
|
96
|
-
responseContentLength
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
next();
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
Object.defineProperty(exports, 'Prometheus', {
|
|
105
|
-
enumerable: true,
|
|
106
|
-
get: function () { return metrics.Prometheus; }
|
|
107
|
-
});
|
|
108
|
-
Object.defineProperty(exports, 'defaultNormalizers', {
|
|
109
|
-
enumerable: true,
|
|
110
|
-
get: function () { return metrics.defaultNormalizers; }
|
|
111
|
-
});
|
|
112
|
-
Object.defineProperty(exports, 'defaultRegister', {
|
|
113
|
-
enumerable: true,
|
|
114
|
-
get: function () { return metrics.defaultRegister; }
|
|
115
|
-
});
|
|
116
|
-
Object.defineProperty(exports, 'getContentType', {
|
|
117
|
-
enumerable: true,
|
|
118
|
-
get: function () { return metrics.getContentType; }
|
|
119
|
-
});
|
|
120
|
-
Object.defineProperty(exports, 'getSummary', {
|
|
121
|
-
enumerable: true,
|
|
122
|
-
get: function () { return metrics.getSummary; }
|
|
123
|
-
});
|
|
124
|
-
Object.defineProperty(exports, 'timing', {
|
|
125
|
-
enumerable: true,
|
|
126
|
-
get: function () { return metrics.timing; }
|
|
127
|
-
});
|
|
128
|
-
exports.createMiddleware = createMiddleware;
|
|
129
|
-
exports.getRequestRecorder = getRequestRecorder;
|
|
130
|
-
exports.signalIsNotUp = signalIsNotUp;
|
|
131
|
-
exports.signalIsUp = signalIsUp;
|