@promster/express 4.1.15 → 5.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +55 -0
- package/dist/{modules → declarations/src}/index.d.ts +3 -4
- package/dist/{modules → declarations/src}/middleware/index.d.ts +2 -2
- package/dist/{modules → declarations/src}/middleware/middleware.d.ts +23 -23
- package/dist/promster-express.cjs.d.ts +1 -0
- package/dist/promster-express.cjs.dev.js +139 -0
- package/dist/promster-express.cjs.js +7 -0
- package/dist/promster-express.cjs.prod.js +139 -0
- package/package.json +9 -11
- package/dist/modules/index.js +0 -17
- package/dist/modules/index.js.map +0 -1
- package/dist/modules/middleware/index.js +0 -9
- package/dist/modules/middleware/index.js.map +0 -1
- package/dist/modules/middleware/middleware.js +0 -60
- package/dist/modules/middleware/middleware.js.map +0 -1
- package/dist/package.json +0 -48
- package/modules/index.ts +0 -27
- package/modules/middleware/index.ts +0 -8
- package/modules/middleware/middleware.spec.js +0 -145
- package/modules/middleware/middleware.ts +0 -112
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,60 @@
|
|
1
1
|
# @promster/express
|
2
2
|
|
3
|
+
## 5.0.3
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- [#620](https://github.com/tdeekens/promster/pull/620) [`ec2a8f8`](https://github.com/tdeekens/promster/commit/ec2a8f83a94bbe63360cf7027eeba92895315a19) Thanks [@tdeekens](https://github.com/tdeekens)! - refactor: improve typings (not exposed part)
|
8
|
+
|
9
|
+
* [#618](https://github.com/tdeekens/promster/pull/618) [`bbab9ca`](https://github.com/tdeekens/promster/commit/bbab9cad6a3484e4894d159267d62e54e202812a) Thanks [@tdeekens](https://github.com/tdeekens)! - chore: update deps
|
10
|
+
|
11
|
+
* Updated dependencies [[`ec2a8f8`](https://github.com/tdeekens/promster/commit/ec2a8f83a94bbe63360cf7027eeba92895315a19), [`bbab9ca`](https://github.com/tdeekens/promster/commit/bbab9cad6a3484e4894d159267d62e54e202812a)]:
|
12
|
+
- @promster/metrics@6.0.2
|
13
|
+
|
14
|
+
## 5.0.2
|
15
|
+
|
16
|
+
### Patch Changes
|
17
|
+
|
18
|
+
- [#601](https://github.com/tdeekens/promster/pull/601) [`17a24dc`](https://github.com/tdeekens/promster/commit/17a24dc0d735478001524c853b9f54f862153852) Thanks [@tdeekens](https://github.com/tdeekens)! - Update dependencies and apply eslint rule suggestions.
|
19
|
+
|
20
|
+
- Updated dependencies [[`17a24dc`](https://github.com/tdeekens/promster/commit/17a24dc0d735478001524c853b9f54f862153852)]:
|
21
|
+
- @promster/metrics@6.0.1
|
22
|
+
|
23
|
+
## 5.0.1
|
24
|
+
|
25
|
+
### Patch Changes
|
26
|
+
|
27
|
+
- Updated dependencies [[`daf8605`](https://github.com/tdeekens/promster/commit/daf86055e64cb420c83dbc7abbcd5024d449c53f)]:
|
28
|
+
- @promster/metrics@6.0.0
|
29
|
+
|
30
|
+
## 5.0.0
|
31
|
+
|
32
|
+
### Major Changes
|
33
|
+
|
34
|
+
- [`0eb64ca`](https://github.com/tdeekens/promster/commit/0eb64cac9a4a51dab1a556f46c97a2a5542bcc88) [#529](https://github.com/tdeekens/promster/pull/529) Thanks [@tdeekens](https://github.com/tdeekens)! - # Introduction
|
35
|
+
|
36
|
+
refactor: to use preconstruct for building
|
37
|
+
|
38
|
+
Prior TypeScript was used to build bundles. In all this should not affect consumers of this library. Under the hood preconstruct uses rollup and babel which is now instructed to build for Node.js v12 using the preset-env preset.
|
39
|
+
|
40
|
+
# Breaking Change
|
41
|
+
|
42
|
+
This release can _potentially_ be breaking for you. We want to respect semantic versioning and follow it strictly.
|
43
|
+
|
44
|
+
While migrating to preconstruct the `version` exports had to be removed as preconstruct's rollup will not resolve them. If you relied on this value you should either load the `package.json` of each module yourself or drop the usage.
|
45
|
+
|
46
|
+
```diff
|
47
|
+
- const { version } = require('@promster/server');
|
48
|
+
+ const { version } = require('@promster/server/package.json');
|
49
|
+
```
|
50
|
+
|
51
|
+
### Patch Changes
|
52
|
+
|
53
|
+
- [`bdf75de`](https://github.com/tdeekens/promster/commit/bdf75dec8d0ce6be65ecccf5963f348e1a0a96b3) [#531](https://github.com/tdeekens/promster/pull/531) Thanks [@tdeekens](https://github.com/tdeekens)! - fix: update dependencies
|
54
|
+
|
55
|
+
- Updated dependencies [[`bdf75de`](https://github.com/tdeekens/promster/commit/bdf75dec8d0ce6be65ecccf5963f348e1a0a96b3), [`0eb64ca`](https://github.com/tdeekens/promster/commit/0eb64cac9a4a51dab1a556f46c97a2a5542bcc88)]:
|
56
|
+
- @promster/metrics@5.0.0
|
57
|
+
|
3
58
|
## 4.1.15
|
4
59
|
|
5
60
|
### Patch Changes
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
|
4
|
-
export { version, createMiddleware, getRequestRecorder, signalIsUp, signalIsNotUp, getSummary, getContentType, Prometheus, defaultRegister, defaultNormalizers, };
|
1
|
+
import { createMiddleware, getRequestRecorder, signalIsUp, signalIsNotUp } from './middleware';
|
2
|
+
import { getSummary, getContentType, Prometheus, defaultRegister, defaultNormalizers } from '@promster/metrics';
|
3
|
+
export { createMiddleware, getRequestRecorder, signalIsUp, signalIsNotUp, getSummary, getContentType, Prometheus, defaultRegister, defaultNormalizers, };
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import { createMiddleware, getRequestRecorder, signalIsUp, signalIsNotUp } from './middleware';
|
2
|
-
export { createMiddleware, signalIsUp, signalIsNotUp, getRequestRecorder };
|
1
|
+
import { createMiddleware, getRequestRecorder, signalIsUp, signalIsNotUp } from './middleware';
|
2
|
+
export { createMiddleware, signalIsUp, signalIsNotUp, getRequestRecorder };
|
@@ -1,23 +1,23 @@
|
|
1
|
-
import type { TPromsterOptions } from '@promster/types';
|
2
|
-
import type { TRequestRecorder } from '@promster/metrics';
|
3
|
-
import { Application, Request, Response, NextFunction } from 'express';
|
4
|
-
import { Prometheus } from '@promster/metrics';
|
5
|
-
interface TApp extends Application {
|
6
|
-
locals: Record<string, unknown>;
|
7
|
-
}
|
8
|
-
declare 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
|
-
declare type TMiddlewareOptions = {
|
19
|
-
app?: TApp;
|
20
|
-
options?: TPromsterOptions;
|
21
|
-
};
|
22
|
-
declare const createMiddleware: ({ app, options }?: TMiddlewareOptions) => (request: Request, response: Response, next: NextFunction) => void;
|
23
|
-
export { createMiddleware, exposeOnLocals, extractPath, getRequestRecorder, signalIsUp, signalIsNotUp, };
|
1
|
+
import type { TPromsterOptions } from '@promster/types';
|
2
|
+
import type { TRequestRecorder } from '@promster/metrics';
|
3
|
+
import { Application, Request, Response, NextFunction } from 'express';
|
4
|
+
import { Prometheus } from '@promster/metrics';
|
5
|
+
interface TApp extends Application {
|
6
|
+
locals: Record<string, unknown>;
|
7
|
+
}
|
8
|
+
declare 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
|
+
declare type TMiddlewareOptions = {
|
19
|
+
app?: TApp;
|
20
|
+
options?: TPromsterOptions;
|
21
|
+
};
|
22
|
+
declare const createMiddleware: ({ app, options }?: TMiddlewareOptions) => (request: Request, response: Response, next: NextFunction) => void;
|
23
|
+
export { createMiddleware, exposeOnLocals, extractPath, getRequestRecorder, signalIsUp, signalIsNotUp, };
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./declarations/src/index";
|
@@ -0,0 +1,139 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
+
|
5
|
+
var merge = require('merge-options');
|
6
|
+
var metrics = require('@promster/metrics');
|
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) app.locals[key] = value;
|
18
|
+
};
|
19
|
+
|
20
|
+
const extractPath = req => req.originalUrl || req.url;
|
21
|
+
|
22
|
+
let recordRequest;
|
23
|
+
let upMetric;
|
24
|
+
|
25
|
+
const getRequestRecorder = () => recordRequest;
|
26
|
+
|
27
|
+
const signalIsUp = () => {
|
28
|
+
if (!upMetric) {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
|
32
|
+
upMetric.forEach(upMetricType => {
|
33
|
+
upMetricType.set(1);
|
34
|
+
});
|
35
|
+
};
|
36
|
+
|
37
|
+
const signalIsNotUp = () => {
|
38
|
+
if (!upMetric) {
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
|
42
|
+
upMetric.forEach(upMetricType => {
|
43
|
+
upMetricType.set(0);
|
44
|
+
});
|
45
|
+
};
|
46
|
+
|
47
|
+
const createMiddleware = ({
|
48
|
+
app,
|
49
|
+
options
|
50
|
+
} = {
|
51
|
+
app: undefined,
|
52
|
+
options: undefined
|
53
|
+
}) => {
|
54
|
+
const allDefaultedOptions = merge__default['default'](metrics.createMetricTypes.defaultOptions, metrics.createRequestRecorder.defaultOptions, metrics.defaultNormalizers, options);
|
55
|
+
const shouldSkipMetricsByEnvironment = allDefaultedOptions.detectKubernetes === true && !metrics.isRunningInKubernetes();
|
56
|
+
const metricTypes = metrics.createMetricTypes(allDefaultedOptions);
|
57
|
+
const observeGc = metrics.createGcObserver(metricTypes);
|
58
|
+
recordRequest = metrics.createRequestRecorder(metricTypes, allDefaultedOptions);
|
59
|
+
upMetric = metricTypes === null || metricTypes === void 0 ? void 0 : metricTypes.up;
|
60
|
+
exposeOnLocals({
|
61
|
+
app,
|
62
|
+
key: 'Prometheus',
|
63
|
+
value: metrics.Prometheus
|
64
|
+
});
|
65
|
+
exposeOnLocals({
|
66
|
+
app,
|
67
|
+
key: 'recordRequest',
|
68
|
+
value: recordRequest
|
69
|
+
});
|
70
|
+
|
71
|
+
if (!shouldSkipMetricsByEnvironment) {
|
72
|
+
observeGc();
|
73
|
+
}
|
74
|
+
|
75
|
+
return (request, response, next) => {
|
76
|
+
const start = process.hrtime();
|
77
|
+
response.on('finish', () => {
|
78
|
+
var _allDefaultedOptions$, _allDefaultedOptions$2;
|
79
|
+
|
80
|
+
const labels = Object.assign({}, {
|
81
|
+
method: allDefaultedOptions.normalizeMethod(request.method, {
|
82
|
+
req: request,
|
83
|
+
res: response
|
84
|
+
}),
|
85
|
+
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, {
|
86
|
+
req: request,
|
87
|
+
res: response
|
88
|
+
}),
|
89
|
+
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
90
|
+
req: request,
|
91
|
+
res: response
|
92
|
+
})
|
93
|
+
}, (_allDefaultedOptions$ = allDefaultedOptions.getLabelValues) === null || _allDefaultedOptions$ === void 0 ? void 0 : _allDefaultedOptions$.call(allDefaultedOptions, request, response));
|
94
|
+
const shouldSkipByRequest = (_allDefaultedOptions$2 = allDefaultedOptions.skip) === null || _allDefaultedOptions$2 === void 0 ? void 0 : _allDefaultedOptions$2.call(allDefaultedOptions, request, response, labels);
|
95
|
+
|
96
|
+
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {
|
97
|
+
recordRequest(start, {
|
98
|
+
labels
|
99
|
+
});
|
100
|
+
}
|
101
|
+
});
|
102
|
+
next();
|
103
|
+
};
|
104
|
+
};
|
105
|
+
|
106
|
+
Object.defineProperty(exports, 'Prometheus', {
|
107
|
+
enumerable: true,
|
108
|
+
get: function () {
|
109
|
+
return metrics.Prometheus;
|
110
|
+
}
|
111
|
+
});
|
112
|
+
Object.defineProperty(exports, 'defaultNormalizers', {
|
113
|
+
enumerable: true,
|
114
|
+
get: function () {
|
115
|
+
return metrics.defaultNormalizers;
|
116
|
+
}
|
117
|
+
});
|
118
|
+
Object.defineProperty(exports, 'defaultRegister', {
|
119
|
+
enumerable: true,
|
120
|
+
get: function () {
|
121
|
+
return metrics.defaultRegister;
|
122
|
+
}
|
123
|
+
});
|
124
|
+
Object.defineProperty(exports, 'getContentType', {
|
125
|
+
enumerable: true,
|
126
|
+
get: function () {
|
127
|
+
return metrics.getContentType;
|
128
|
+
}
|
129
|
+
});
|
130
|
+
Object.defineProperty(exports, 'getSummary', {
|
131
|
+
enumerable: true,
|
132
|
+
get: function () {
|
133
|
+
return metrics.getSummary;
|
134
|
+
}
|
135
|
+
});
|
136
|
+
exports.createMiddleware = createMiddleware;
|
137
|
+
exports.getRequestRecorder = getRequestRecorder;
|
138
|
+
exports.signalIsNotUp = signalIsNotUp;
|
139
|
+
exports.signalIsUp = signalIsUp;
|
@@ -0,0 +1,139 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
+
|
5
|
+
var merge = require('merge-options');
|
6
|
+
var metrics = require('@promster/metrics');
|
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) app.locals[key] = value;
|
18
|
+
};
|
19
|
+
|
20
|
+
const extractPath = req => req.originalUrl || req.url;
|
21
|
+
|
22
|
+
let recordRequest;
|
23
|
+
let upMetric;
|
24
|
+
|
25
|
+
const getRequestRecorder = () => recordRequest;
|
26
|
+
|
27
|
+
const signalIsUp = () => {
|
28
|
+
if (!upMetric) {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
|
32
|
+
upMetric.forEach(upMetricType => {
|
33
|
+
upMetricType.set(1);
|
34
|
+
});
|
35
|
+
};
|
36
|
+
|
37
|
+
const signalIsNotUp = () => {
|
38
|
+
if (!upMetric) {
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
|
42
|
+
upMetric.forEach(upMetricType => {
|
43
|
+
upMetricType.set(0);
|
44
|
+
});
|
45
|
+
};
|
46
|
+
|
47
|
+
const createMiddleware = ({
|
48
|
+
app,
|
49
|
+
options
|
50
|
+
} = {
|
51
|
+
app: undefined,
|
52
|
+
options: undefined
|
53
|
+
}) => {
|
54
|
+
const allDefaultedOptions = merge__default['default'](metrics.createMetricTypes.defaultOptions, metrics.createRequestRecorder.defaultOptions, metrics.defaultNormalizers, options);
|
55
|
+
const shouldSkipMetricsByEnvironment = allDefaultedOptions.detectKubernetes === true && !metrics.isRunningInKubernetes();
|
56
|
+
const metricTypes = metrics.createMetricTypes(allDefaultedOptions);
|
57
|
+
const observeGc = metrics.createGcObserver(metricTypes);
|
58
|
+
recordRequest = metrics.createRequestRecorder(metricTypes, allDefaultedOptions);
|
59
|
+
upMetric = metricTypes === null || metricTypes === void 0 ? void 0 : metricTypes.up;
|
60
|
+
exposeOnLocals({
|
61
|
+
app,
|
62
|
+
key: 'Prometheus',
|
63
|
+
value: metrics.Prometheus
|
64
|
+
});
|
65
|
+
exposeOnLocals({
|
66
|
+
app,
|
67
|
+
key: 'recordRequest',
|
68
|
+
value: recordRequest
|
69
|
+
});
|
70
|
+
|
71
|
+
if (!shouldSkipMetricsByEnvironment) {
|
72
|
+
observeGc();
|
73
|
+
}
|
74
|
+
|
75
|
+
return (request, response, next) => {
|
76
|
+
const start = process.hrtime();
|
77
|
+
response.on('finish', () => {
|
78
|
+
var _allDefaultedOptions$, _allDefaultedOptions$2;
|
79
|
+
|
80
|
+
const labels = Object.assign({}, {
|
81
|
+
method: allDefaultedOptions.normalizeMethod(request.method, {
|
82
|
+
req: request,
|
83
|
+
res: response
|
84
|
+
}),
|
85
|
+
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, {
|
86
|
+
req: request,
|
87
|
+
res: response
|
88
|
+
}),
|
89
|
+
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
90
|
+
req: request,
|
91
|
+
res: response
|
92
|
+
})
|
93
|
+
}, (_allDefaultedOptions$ = allDefaultedOptions.getLabelValues) === null || _allDefaultedOptions$ === void 0 ? void 0 : _allDefaultedOptions$.call(allDefaultedOptions, request, response));
|
94
|
+
const shouldSkipByRequest = (_allDefaultedOptions$2 = allDefaultedOptions.skip) === null || _allDefaultedOptions$2 === void 0 ? void 0 : _allDefaultedOptions$2.call(allDefaultedOptions, request, response, labels);
|
95
|
+
|
96
|
+
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {
|
97
|
+
recordRequest(start, {
|
98
|
+
labels
|
99
|
+
});
|
100
|
+
}
|
101
|
+
});
|
102
|
+
next();
|
103
|
+
};
|
104
|
+
};
|
105
|
+
|
106
|
+
Object.defineProperty(exports, 'Prometheus', {
|
107
|
+
enumerable: true,
|
108
|
+
get: function () {
|
109
|
+
return metrics.Prometheus;
|
110
|
+
}
|
111
|
+
});
|
112
|
+
Object.defineProperty(exports, 'defaultNormalizers', {
|
113
|
+
enumerable: true,
|
114
|
+
get: function () {
|
115
|
+
return metrics.defaultNormalizers;
|
116
|
+
}
|
117
|
+
});
|
118
|
+
Object.defineProperty(exports, 'defaultRegister', {
|
119
|
+
enumerable: true,
|
120
|
+
get: function () {
|
121
|
+
return metrics.defaultRegister;
|
122
|
+
}
|
123
|
+
});
|
124
|
+
Object.defineProperty(exports, 'getContentType', {
|
125
|
+
enumerable: true,
|
126
|
+
get: function () {
|
127
|
+
return metrics.getContentType;
|
128
|
+
}
|
129
|
+
});
|
130
|
+
Object.defineProperty(exports, 'getSummary', {
|
131
|
+
enumerable: true,
|
132
|
+
get: function () {
|
133
|
+
return metrics.getSummary;
|
134
|
+
}
|
135
|
+
});
|
136
|
+
exports.createMiddleware = createMiddleware;
|
137
|
+
exports.getRequestRecorder = getRequestRecorder;
|
138
|
+
exports.signalIsNotUp = signalIsNotUp;
|
139
|
+
exports.signalIsUp = signalIsUp;
|
package/package.json
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "@promster/express",
|
3
|
-
"version": "
|
3
|
+
"version": "5.0.3",
|
4
4
|
"description": "Express server integrations of promster",
|
5
|
-
"main": "dist/
|
6
|
-
"typings": "dist/
|
7
|
-
"
|
8
|
-
|
9
|
-
"build": "cross-env tsc -p tsconfig.json --outDir dist --declarationDir dist"
|
10
|
-
},
|
5
|
+
"main": "dist/promster-express.cjs.js",
|
6
|
+
"typings": "dist/promster-express.cjs.d.ts",
|
7
|
+
"types": "dist/promster-express.cjs.d.ts",
|
8
|
+
"scripts": {},
|
11
9
|
"files": [
|
12
10
|
"readme.md",
|
13
11
|
"package.json",
|
@@ -38,11 +36,11 @@
|
|
38
36
|
"prometheus"
|
39
37
|
],
|
40
38
|
"dependencies": {
|
41
|
-
"@promster/metrics": "^
|
42
|
-
"merge-options": "3.0.
|
43
|
-
"tslib": "2.0
|
39
|
+
"@promster/metrics": "^6.0.2",
|
40
|
+
"merge-options": "3.0.4",
|
41
|
+
"tslib": "2.2.0"
|
44
42
|
},
|
45
43
|
"devDependencies": {
|
46
|
-
"@types/express": "4.17.
|
44
|
+
"@types/express": "4.17.11"
|
47
45
|
}
|
48
46
|
}
|
package/dist/modules/index.js
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.defaultNormalizers = exports.defaultRegister = exports.Prometheus = exports.getContentType = exports.getSummary = exports.signalIsNotUp = exports.signalIsUp = exports.getRequestRecorder = exports.createMiddleware = exports.version = void 0;
|
4
|
-
const package_json_1 = require("../package.json");
|
5
|
-
Object.defineProperty(exports, "version", { enumerable: true, get: function () { return package_json_1.version; } });
|
6
|
-
const middleware_1 = require("./middleware");
|
7
|
-
Object.defineProperty(exports, "createMiddleware", { enumerable: true, get: function () { return middleware_1.createMiddleware; } });
|
8
|
-
Object.defineProperty(exports, "getRequestRecorder", { enumerable: true, get: function () { return middleware_1.getRequestRecorder; } });
|
9
|
-
Object.defineProperty(exports, "signalIsUp", { enumerable: true, get: function () { return middleware_1.signalIsUp; } });
|
10
|
-
Object.defineProperty(exports, "signalIsNotUp", { enumerable: true, get: function () { return middleware_1.signalIsNotUp; } });
|
11
|
-
const metrics_1 = require("@promster/metrics");
|
12
|
-
Object.defineProperty(exports, "getSummary", { enumerable: true, get: function () { return metrics_1.getSummary; } });
|
13
|
-
Object.defineProperty(exports, "getContentType", { enumerable: true, get: function () { return metrics_1.getContentType; } });
|
14
|
-
Object.defineProperty(exports, "Prometheus", { enumerable: true, get: function () { return metrics_1.Prometheus; } });
|
15
|
-
Object.defineProperty(exports, "defaultRegister", { enumerable: true, get: function () { return metrics_1.defaultRegister; } });
|
16
|
-
Object.defineProperty(exports, "defaultNormalizers", { enumerable: true, get: function () { return metrics_1.defaultNormalizers; } });
|
17
|
-
//# sourceMappingURL=index.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../modules/index.ts"],"names":[],"mappings":";;;AAAA,kDAA0C;AAgBxC,wFAhBO,sBAAO,OAgBP;AAfT,6CAKsB;AAWpB,iGAfA,6BAAgB,OAeA;AAChB,mGAfA,+BAAkB,OAeA;AAClB,2FAfA,uBAAU,OAeA;AACV,8FAfA,0BAAa,OAeA;AAbf,+CAM2B;AAQzB,2FAbA,oBAAU,OAaA;AACV,+FAbA,wBAAc,OAaA;AACd,2FAbA,oBAAU,OAaA;AACV,gGAbA,yBAAe,OAaA;AACf,mGAbA,4BAAkB,OAaA"}
|
@@ -1,9 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getRequestRecorder = exports.signalIsNotUp = exports.signalIsUp = exports.createMiddleware = void 0;
|
4
|
-
const middleware_1 = require("./middleware");
|
5
|
-
Object.defineProperty(exports, "createMiddleware", { enumerable: true, get: function () { return middleware_1.createMiddleware; } });
|
6
|
-
Object.defineProperty(exports, "getRequestRecorder", { enumerable: true, get: function () { return middleware_1.getRequestRecorder; } });
|
7
|
-
Object.defineProperty(exports, "signalIsUp", { enumerable: true, get: function () { return middleware_1.signalIsUp; } });
|
8
|
-
Object.defineProperty(exports, "signalIsNotUp", { enumerable: true, get: function () { return middleware_1.signalIsNotUp; } });
|
9
|
-
//# sourceMappingURL=index.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../modules/middleware/index.ts"],"names":[],"mappings":";;;AAAA,6CAKsB;AAEb,iGANP,6BAAgB,OAMO;AAA6B,mGALpD,+BAAkB,OAKoD;AAA7C,2FAJzB,uBAAU,OAIyB;AAAE,8FAHrC,0BAAa,OAGqC"}
|
@@ -1,60 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.signalIsNotUp = exports.signalIsUp = exports.getRequestRecorder = exports.extractPath = exports.exposeOnLocals = exports.createMiddleware = void 0;
|
4
|
-
const tslib_1 = require("tslib");
|
5
|
-
const merge_options_1 = tslib_1.__importDefault(require("merge-options"));
|
6
|
-
const metrics_1 = require("@promster/metrics");
|
7
|
-
const exposeOnLocals = ({ app, key, value }) => {
|
8
|
-
if (app === null || app === void 0 ? void 0 : app.locals)
|
9
|
-
app.locals[key] = value;
|
10
|
-
};
|
11
|
-
exports.exposeOnLocals = exposeOnLocals;
|
12
|
-
const extractPath = (req) => req.originalUrl || req.url;
|
13
|
-
exports.extractPath = extractPath;
|
14
|
-
let recordRequest;
|
15
|
-
let upMetric;
|
16
|
-
const getRequestRecorder = () => recordRequest;
|
17
|
-
exports.getRequestRecorder = getRequestRecorder;
|
18
|
-
const signalIsUp = () => upMetric === null || upMetric === void 0 ? void 0 : upMetric.forEach((upMetricType) => upMetricType.set(1));
|
19
|
-
exports.signalIsUp = signalIsUp;
|
20
|
-
const signalIsNotUp = () => upMetric === null || upMetric === void 0 ? void 0 : upMetric.forEach((upMetricType) => upMetricType.set(0));
|
21
|
-
exports.signalIsNotUp = signalIsNotUp;
|
22
|
-
const createMiddleware = ({ app, options } = { app: undefined, options: undefined }) => {
|
23
|
-
const allDefaultedOptions = merge_options_1.default(metrics_1.createMetricTypes.defaultOptions, metrics_1.createRequestRecorder.defaultOptions, metrics_1.defaultNormalizers, options);
|
24
|
-
const shouldSkipMetricsByEnvironment = allDefaultedOptions.detectKubernetes === true && !metrics_1.isRunningInKubernetes();
|
25
|
-
const metricTypes = metrics_1.createMetricTypes(allDefaultedOptions);
|
26
|
-
const observeGc = metrics_1.createGcObserver(metricTypes);
|
27
|
-
recordRequest = metrics_1.createRequestRecorder(metricTypes, allDefaultedOptions);
|
28
|
-
upMetric = metricTypes === null || metricTypes === void 0 ? void 0 : metricTypes.up;
|
29
|
-
exposeOnLocals({ app, key: 'Prometheus', value: metrics_1.Prometheus });
|
30
|
-
exposeOnLocals({ app, key: 'recordRequest', value: recordRequest });
|
31
|
-
if (!shouldSkipMetricsByEnvironment) {
|
32
|
-
observeGc();
|
33
|
-
}
|
34
|
-
return (request, response, next) => {
|
35
|
-
const start = process.hrtime();
|
36
|
-
response.on('finish', () => {
|
37
|
-
var _a, _b;
|
38
|
-
const labels = Object.assign({}, {
|
39
|
-
method: allDefaultedOptions.normalizeMethod(request.method, {
|
40
|
-
req: request,
|
41
|
-
res: response,
|
42
|
-
}),
|
43
|
-
status_code: allDefaultedOptions.normalizeStatusCode(response.statusCode, { req: request, res: response }),
|
44
|
-
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
45
|
-
req: request,
|
46
|
-
res: response,
|
47
|
-
}),
|
48
|
-
}, (_a = allDefaultedOptions.getLabelValues) === null || _a === void 0 ? void 0 : _a.call(allDefaultedOptions, request, response));
|
49
|
-
const shouldSkipByRequest = (_b = allDefaultedOptions.skip) === null || _b === void 0 ? void 0 : _b.call(allDefaultedOptions, request, response, labels);
|
50
|
-
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {
|
51
|
-
recordRequest(start, {
|
52
|
-
labels,
|
53
|
-
});
|
54
|
-
}
|
55
|
-
});
|
56
|
-
return next();
|
57
|
-
};
|
58
|
-
};
|
59
|
-
exports.createMiddleware = createMiddleware;
|
60
|
-
//# sourceMappingURL=middleware.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../modules/middleware/middleware.ts"],"names":[],"mappings":"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"}
|
package/dist/package.json
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"name": "@promster/express",
|
3
|
-
"version": "4.1.15",
|
4
|
-
"description": "Express server integrations of promster",
|
5
|
-
"main": "dist/modules/index.js",
|
6
|
-
"typings": "dist/modules/index.d.ts",
|
7
|
-
"scripts": {
|
8
|
-
"prebuild": "rimraf dist/**",
|
9
|
-
"build": "cross-env tsc -p tsconfig.json --outDir dist --declarationDir dist"
|
10
|
-
},
|
11
|
-
"files": [
|
12
|
-
"readme.md",
|
13
|
-
"package.json",
|
14
|
-
"LICENSE",
|
15
|
-
"dist/**",
|
16
|
-
"modules/**"
|
17
|
-
],
|
18
|
-
"engines": {
|
19
|
-
"node": ">=9",
|
20
|
-
"npm": ">=3"
|
21
|
-
},
|
22
|
-
"publishConfig": {
|
23
|
-
"access": "public"
|
24
|
-
},
|
25
|
-
"repository": {
|
26
|
-
"type": "git",
|
27
|
-
"url": "git+https://github.com/tdeekens/promster.git"
|
28
|
-
},
|
29
|
-
"author": "Tobias Deekens <nerd@tdeekens.name>",
|
30
|
-
"license": "MIT",
|
31
|
-
"bugs": {
|
32
|
-
"url": "https://github.com/tdeekens/promster/issues"
|
33
|
-
},
|
34
|
-
"homepage": "https://github.com/tdeekens/promster#readme",
|
35
|
-
"keywords": [
|
36
|
-
"metrics",
|
37
|
-
"continousdelivery",
|
38
|
-
"prometheus"
|
39
|
-
],
|
40
|
-
"dependencies": {
|
41
|
-
"@promster/metrics": "^4.1.13",
|
42
|
-
"merge-options": "3.0.3",
|
43
|
-
"tslib": "2.0.3"
|
44
|
-
},
|
45
|
-
"devDependencies": {
|
46
|
-
"@types/express": "4.17.7"
|
47
|
-
}
|
48
|
-
}
|
package/modules/index.ts
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
import { version } from '../package.json';
|
2
|
-
import {
|
3
|
-
createMiddleware,
|
4
|
-
getRequestRecorder,
|
5
|
-
signalIsUp,
|
6
|
-
signalIsNotUp,
|
7
|
-
} from './middleware';
|
8
|
-
import {
|
9
|
-
getSummary,
|
10
|
-
getContentType,
|
11
|
-
Prometheus,
|
12
|
-
defaultRegister,
|
13
|
-
defaultNormalizers,
|
14
|
-
} from '@promster/metrics';
|
15
|
-
|
16
|
-
export {
|
17
|
-
version,
|
18
|
-
createMiddleware,
|
19
|
-
getRequestRecorder,
|
20
|
-
signalIsUp,
|
21
|
-
signalIsNotUp,
|
22
|
-
getSummary,
|
23
|
-
getContentType,
|
24
|
-
Prometheus,
|
25
|
-
defaultRegister,
|
26
|
-
defaultNormalizers,
|
27
|
-
};
|
@@ -1,145 +0,0 @@
|
|
1
|
-
jest.mock('@promster/metrics', () => ({
|
2
|
-
Prometheus: 'MockPrometheus',
|
3
|
-
createMetricTypes: jest.fn(),
|
4
|
-
createRequestRecorder: jest.fn(() => jest.fn()),
|
5
|
-
createGcObserver: jest.fn(() => jest.fn()),
|
6
|
-
defaultNormalizers: {
|
7
|
-
normalizePath: jest.fn((_) => _),
|
8
|
-
normalizeStatusCode: jest.fn((_) => _),
|
9
|
-
normalizeMethod: jest.fn((_) => _),
|
10
|
-
},
|
11
|
-
}));
|
12
|
-
|
13
|
-
const {
|
14
|
-
Prometheus,
|
15
|
-
createRequestRecorder,
|
16
|
-
createGcObserver,
|
17
|
-
} = require('@promster/metrics');
|
18
|
-
const {
|
19
|
-
exposeOnLocals,
|
20
|
-
extractPath,
|
21
|
-
createMiddleware,
|
22
|
-
} = require('./middleware');
|
23
|
-
|
24
|
-
describe('exposing Prometheus', () => {
|
25
|
-
describe('with app and locals', () => {
|
26
|
-
let app = { locals: {} };
|
27
|
-
beforeEach(() => {
|
28
|
-
exposeOnLocals({ app, key: 'Prometheus', value: Prometheus });
|
29
|
-
});
|
30
|
-
|
31
|
-
it('should expose Prometheus on app locals', () => {
|
32
|
-
expect(app.locals).toHaveProperty('Prometheus', 'MockPrometheus');
|
33
|
-
});
|
34
|
-
});
|
35
|
-
|
36
|
-
describe('without app and locals', () => {
|
37
|
-
let app = {};
|
38
|
-
beforeEach(() => {
|
39
|
-
exposeOnLocals({ app, key: 'Prometheus', value: Prometheus });
|
40
|
-
});
|
41
|
-
|
42
|
-
it('should not expose Prometheus on app locals', () => {
|
43
|
-
expect(app).not.toHaveProperty('locals');
|
44
|
-
});
|
45
|
-
});
|
46
|
-
});
|
47
|
-
|
48
|
-
describe('extracting path', () => {
|
49
|
-
let extractedPath;
|
50
|
-
describe('with original url', () => {
|
51
|
-
let req = { originalUrl: 'originalUrl', url: 'nextUrl' };
|
52
|
-
|
53
|
-
beforeEach(() => {
|
54
|
-
extractedPath = extractPath(req);
|
55
|
-
});
|
56
|
-
|
57
|
-
it('should extract original url', () => {
|
58
|
-
expect(extractedPath).toEqual(req.originalUrl);
|
59
|
-
});
|
60
|
-
});
|
61
|
-
|
62
|
-
describe('with out original url', () => {
|
63
|
-
let req = { url: 'nextUrl' };
|
64
|
-
|
65
|
-
beforeEach(() => {
|
66
|
-
extractedPath = extractPath(req);
|
67
|
-
});
|
68
|
-
|
69
|
-
it('should extract url', () => {
|
70
|
-
expect(extractedPath).toEqual(req.url);
|
71
|
-
});
|
72
|
-
});
|
73
|
-
});
|
74
|
-
|
75
|
-
describe('middleware', () => {
|
76
|
-
let middleware;
|
77
|
-
describe('when creating middleware', () => {
|
78
|
-
let observeGc = jest.fn();
|
79
|
-
let recordRequest = jest.fn();
|
80
|
-
|
81
|
-
beforeEach(() => {
|
82
|
-
createGcObserver.mockReturnValue(jest.fn(observeGc));
|
83
|
-
createRequestRecorder.mockReturnValue(jest.fn(recordRequest));
|
84
|
-
|
85
|
-
middleware = createMiddleware();
|
86
|
-
});
|
87
|
-
|
88
|
-
it('should start observing garbage collection', () => {
|
89
|
-
expect(observeGc).toHaveBeenCalled();
|
90
|
-
});
|
91
|
-
|
92
|
-
describe('when request starts', () => {
|
93
|
-
let req;
|
94
|
-
let res;
|
95
|
-
let next;
|
96
|
-
let onRequest = jest.fn();
|
97
|
-
|
98
|
-
beforeEach(() => {
|
99
|
-
req = {
|
100
|
-
method: 'GET',
|
101
|
-
};
|
102
|
-
next = jest.fn();
|
103
|
-
res = {
|
104
|
-
statusCode: 200,
|
105
|
-
url: 'foo/bar',
|
106
|
-
on: onRequest,
|
107
|
-
};
|
108
|
-
|
109
|
-
middleware(req, res, next);
|
110
|
-
});
|
111
|
-
|
112
|
-
it('should invoke `next` to pass the middleware', () => {
|
113
|
-
expect(next).toHaveBeenCalled();
|
114
|
-
});
|
115
|
-
|
116
|
-
it('should listen to the response to finish', () => {
|
117
|
-
expect(res.on).toHaveBeenCalledWith('finish', expect.any(Function));
|
118
|
-
});
|
119
|
-
|
120
|
-
describe('when the response finishes', () => {
|
121
|
-
beforeEach(() => {
|
122
|
-
res.on.mock.calls[res.on.mock.calls.length - 1][1]();
|
123
|
-
});
|
124
|
-
|
125
|
-
it('should have observed the request', () => {
|
126
|
-
expect(recordRequest).toHaveBeenCalled();
|
127
|
-
});
|
128
|
-
|
129
|
-
it('should pass labels to the observer', () => {
|
130
|
-
expect(recordRequest).toHaveBeenCalledWith(
|
131
|
-
expect.anything(),
|
132
|
-
expect.objectContaining({
|
133
|
-
labels: expect.objectContaining({
|
134
|
-
// eslint-disable-next-line camelcase
|
135
|
-
status_code: res.statusCode,
|
136
|
-
method: req.method,
|
137
|
-
path: req.url,
|
138
|
-
}),
|
139
|
-
})
|
140
|
-
);
|
141
|
-
});
|
142
|
-
});
|
143
|
-
});
|
144
|
-
});
|
145
|
-
});
|
@@ -1,112 +0,0 @@
|
|
1
|
-
import type { TPromsterOptions, TMetricTypes } from '@promster/types';
|
2
|
-
import type { TRequestRecorder } from '@promster/metrics';
|
3
|
-
import { Application, Request, Response, NextFunction } from 'express';
|
4
|
-
|
5
|
-
import merge from 'merge-options';
|
6
|
-
import {
|
7
|
-
Prometheus,
|
8
|
-
createMetricTypes,
|
9
|
-
createRequestRecorder,
|
10
|
-
createGcObserver,
|
11
|
-
defaultNormalizers,
|
12
|
-
isRunningInKubernetes,
|
13
|
-
} from '@promster/metrics';
|
14
|
-
|
15
|
-
interface TApp extends Application {
|
16
|
-
locals: Record<string, unknown>;
|
17
|
-
}
|
18
|
-
|
19
|
-
type TLocaleTarget = {
|
20
|
-
app?: TApp;
|
21
|
-
key: string;
|
22
|
-
value: typeof Prometheus | TRequestRecorder;
|
23
|
-
};
|
24
|
-
const exposeOnLocals = ({ app, key, value }: TLocaleTarget) => {
|
25
|
-
if (app?.locals) app.locals[key] = value;
|
26
|
-
};
|
27
|
-
|
28
|
-
const extractPath = (req: Request) => req.originalUrl || req.url;
|
29
|
-
|
30
|
-
let recordRequest: TRequestRecorder;
|
31
|
-
let upMetric: TMetricTypes['up'];
|
32
|
-
|
33
|
-
const getRequestRecorder = () => recordRequest;
|
34
|
-
const signalIsUp = () =>
|
35
|
-
upMetric?.forEach((upMetricType) => upMetricType.set(1));
|
36
|
-
const signalIsNotUp = () =>
|
37
|
-
upMetric?.forEach((upMetricType) => upMetricType.set(0));
|
38
|
-
|
39
|
-
type TMiddlewareOptions = {
|
40
|
-
app?: TApp;
|
41
|
-
options?: TPromsterOptions;
|
42
|
-
};
|
43
|
-
const createMiddleware = (
|
44
|
-
{ app, options }: TMiddlewareOptions = { app: undefined, options: undefined }
|
45
|
-
) => {
|
46
|
-
const allDefaultedOptions = merge(
|
47
|
-
createMetricTypes.defaultOptions,
|
48
|
-
createRequestRecorder.defaultOptions,
|
49
|
-
defaultNormalizers,
|
50
|
-
options
|
51
|
-
);
|
52
|
-
|
53
|
-
const shouldSkipMetricsByEnvironment =
|
54
|
-
allDefaultedOptions.detectKubernetes === true && !isRunningInKubernetes();
|
55
|
-
|
56
|
-
const metricTypes = createMetricTypes(allDefaultedOptions);
|
57
|
-
const observeGc = createGcObserver(metricTypes);
|
58
|
-
|
59
|
-
recordRequest = createRequestRecorder(metricTypes, allDefaultedOptions);
|
60
|
-
upMetric = metricTypes?.up;
|
61
|
-
|
62
|
-
exposeOnLocals({ app, key: 'Prometheus', value: Prometheus });
|
63
|
-
exposeOnLocals({ app, key: 'recordRequest', value: recordRequest });
|
64
|
-
|
65
|
-
if (!shouldSkipMetricsByEnvironment) {
|
66
|
-
observeGc();
|
67
|
-
}
|
68
|
-
|
69
|
-
return (request: Request, response: Response, next: NextFunction) => {
|
70
|
-
const start = process.hrtime();
|
71
|
-
response.on('finish', () => {
|
72
|
-
const labels = Object.assign(
|
73
|
-
{},
|
74
|
-
{
|
75
|
-
method: allDefaultedOptions.normalizeMethod(request.method, {
|
76
|
-
req: request,
|
77
|
-
res: response,
|
78
|
-
}),
|
79
|
-
status_code: allDefaultedOptions.normalizeStatusCode(
|
80
|
-
response.statusCode,
|
81
|
-
{ req: request, res: response }
|
82
|
-
),
|
83
|
-
path: allDefaultedOptions.normalizePath(extractPath(request), {
|
84
|
-
req: request,
|
85
|
-
res: response,
|
86
|
-
}),
|
87
|
-
},
|
88
|
-
allDefaultedOptions.getLabelValues?.(request, response)
|
89
|
-
);
|
90
|
-
|
91
|
-
const shouldSkipByRequest =
|
92
|
-
allDefaultedOptions.skip?.(request, response, labels);
|
93
|
-
|
94
|
-
if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {
|
95
|
-
recordRequest(start, {
|
96
|
-
labels,
|
97
|
-
});
|
98
|
-
}
|
99
|
-
});
|
100
|
-
|
101
|
-
return next();
|
102
|
-
};
|
103
|
-
};
|
104
|
-
|
105
|
-
export {
|
106
|
-
createMiddleware,
|
107
|
-
exposeOnLocals,
|
108
|
-
extractPath,
|
109
|
-
getRequestRecorder,
|
110
|
-
signalIsUp,
|
111
|
-
signalIsNotUp,
|
112
|
-
};
|