@tramvai/module-metrics 1.63.1 → 1.64.0
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/lib/server.es.js +60 -48
- package/lib/server.js +60 -48
- package/package.json +8 -8
package/lib/server.es.js
CHANGED
|
@@ -72,56 +72,68 @@ const getUrlAndOptions = (args) => {
|
|
|
72
72
|
const getDuration = (current, prev) =>
|
|
73
73
|
// max to avoid negative values and turn that into zero
|
|
74
74
|
prev === 0 ? 0 : Math.max((current - prev) / 1000, 0);
|
|
75
|
-
const createRequestWithMetrics = ({ metricsInstances: { requestsTotal, requestsErrors, requestsDuration, dnsResolveDuration, tcpConnectDuration, tlsHandshakeDuration, }, getServiceName, config, }) =>
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
75
|
+
const createRequestWithMetrics = ({ metricsInstances: { requestsTotal, requestsErrors, requestsDuration, dnsResolveDuration, tcpConnectDuration, tlsHandshakeDuration, }, getServiceName, config, }) => {
|
|
76
|
+
const socketSet = new WeakSet();
|
|
77
|
+
return function requestWithMetrics(originalRequest, ...args) {
|
|
78
|
+
const [url, options] = getUrlAndOptions(args);
|
|
79
|
+
const serviceName = getServiceName(url);
|
|
80
|
+
const req = originalRequest.apply(this, args);
|
|
81
|
+
const timerDone = requestsDuration.startTimer();
|
|
82
|
+
const labelsValues = {
|
|
83
|
+
method: options.method || 'unknown',
|
|
84
|
+
service: serviceName || new URL(url).origin || 'unknown',
|
|
85
|
+
status: 'unknown',
|
|
86
|
+
};
|
|
87
|
+
req.on('response', (res) => {
|
|
88
|
+
labelsValues.status = res.statusCode.toString();
|
|
89
|
+
if (res.statusCode >= 400) {
|
|
90
|
+
requestsErrors.inc(labelsValues);
|
|
91
|
+
}
|
|
92
|
+
requestsTotal.inc(labelsValues);
|
|
93
|
+
timerDone(labelsValues);
|
|
94
|
+
});
|
|
95
|
+
req.on('error', (e) => {
|
|
96
|
+
if (POSSIBLE_ERRORS.includes(e === null || e === void 0 ? void 0 : e.code)) {
|
|
97
|
+
labelsValues.status = e.code;
|
|
98
|
+
}
|
|
99
|
+
requestsTotal.inc(labelsValues);
|
|
88
100
|
requestsErrors.inc(labelsValues);
|
|
89
|
-
|
|
90
|
-
requestsTotal.inc(labelsValues);
|
|
91
|
-
timerDone(labelsValues);
|
|
92
|
-
});
|
|
93
|
-
req.on('error', (e) => {
|
|
94
|
-
if (POSSIBLE_ERRORS.includes(e === null || e === void 0 ? void 0 : e.code)) {
|
|
95
|
-
labelsValues.status = e.code;
|
|
96
|
-
}
|
|
97
|
-
requestsTotal.inc(labelsValues);
|
|
98
|
-
requestsErrors.inc(labelsValues);
|
|
99
|
-
timerDone(labelsValues);
|
|
100
|
-
});
|
|
101
|
-
if (config.enableConnectionResolveMetrics) {
|
|
102
|
-
req.on('socket', (socket) => {
|
|
103
|
-
const timings = {
|
|
104
|
-
start: Date.now(),
|
|
105
|
-
lookupEnd: 0,
|
|
106
|
-
connectEnd: 0,
|
|
107
|
-
secureConnectEnd: 0,
|
|
108
|
-
};
|
|
109
|
-
const { service } = labelsValues;
|
|
110
|
-
socket.on('lookup', () => {
|
|
111
|
-
timings.lookupEnd = Date.now();
|
|
112
|
-
dnsResolveDuration.observe({ service }, getDuration(timings.lookupEnd, timings.start));
|
|
113
|
-
});
|
|
114
|
-
socket.on('connect', () => {
|
|
115
|
-
timings.connectEnd = Date.now();
|
|
116
|
-
tcpConnectDuration.observe({ service }, getDuration(timings.connectEnd, timings.lookupEnd));
|
|
117
|
-
});
|
|
118
|
-
socket.on('secureConnect', () => {
|
|
119
|
-
timings.secureConnectEnd = Date.now();
|
|
120
|
-
tlsHandshakeDuration.observe({ service }, getDuration(timings.secureConnectEnd, timings.connectEnd));
|
|
121
|
-
});
|
|
101
|
+
timerDone(labelsValues);
|
|
122
102
|
});
|
|
123
|
-
|
|
124
|
-
|
|
103
|
+
if (config.enableConnectionResolveMetrics) {
|
|
104
|
+
req.on('socket', (socket) => {
|
|
105
|
+
// due to keep-alive tcp option sockets might be reused
|
|
106
|
+
// ignore them because they have already emitted events we are interested in
|
|
107
|
+
if (socketSet.has(socket)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
socketSet.add(socket);
|
|
111
|
+
const timings = {
|
|
112
|
+
start: Date.now(),
|
|
113
|
+
lookupEnd: 0,
|
|
114
|
+
connectEnd: 0,
|
|
115
|
+
secureConnectEnd: 0,
|
|
116
|
+
};
|
|
117
|
+
const { service } = labelsValues;
|
|
118
|
+
socket.on('lookup', () => {
|
|
119
|
+
timings.lookupEnd = Date.now();
|
|
120
|
+
dnsResolveDuration.observe({ service }, getDuration(timings.lookupEnd, timings.start));
|
|
121
|
+
});
|
|
122
|
+
socket.on('connect', () => {
|
|
123
|
+
timings.connectEnd = Date.now();
|
|
124
|
+
tcpConnectDuration.observe({ service }, getDuration(timings.connectEnd, timings.lookupEnd));
|
|
125
|
+
});
|
|
126
|
+
socket.on('secureConnect', () => {
|
|
127
|
+
timings.secureConnectEnd = Date.now();
|
|
128
|
+
tlsHandshakeDuration.observe({ service }, getDuration(timings.secureConnectEnd, timings.connectEnd));
|
|
129
|
+
});
|
|
130
|
+
socket.on('close', () => {
|
|
131
|
+
socketSet.delete(socket);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return req;
|
|
136
|
+
};
|
|
125
137
|
};
|
|
126
138
|
|
|
127
139
|
const DEFAULT_BUCKETS = [0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 20, 40, 60];
|
package/lib/server.js
CHANGED
|
@@ -86,56 +86,68 @@ const getUrlAndOptions = (args) => {
|
|
|
86
86
|
const getDuration = (current, prev) =>
|
|
87
87
|
// max to avoid negative values and turn that into zero
|
|
88
88
|
prev === 0 ? 0 : Math.max((current - prev) / 1000, 0);
|
|
89
|
-
const createRequestWithMetrics = ({ metricsInstances: { requestsTotal, requestsErrors, requestsDuration, dnsResolveDuration, tcpConnectDuration, tlsHandshakeDuration, }, getServiceName, config, }) =>
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
89
|
+
const createRequestWithMetrics = ({ metricsInstances: { requestsTotal, requestsErrors, requestsDuration, dnsResolveDuration, tcpConnectDuration, tlsHandshakeDuration, }, getServiceName, config, }) => {
|
|
90
|
+
const socketSet = new WeakSet();
|
|
91
|
+
return function requestWithMetrics(originalRequest, ...args) {
|
|
92
|
+
const [url, options] = getUrlAndOptions(args);
|
|
93
|
+
const serviceName = getServiceName(url);
|
|
94
|
+
const req = originalRequest.apply(this, args);
|
|
95
|
+
const timerDone = requestsDuration.startTimer();
|
|
96
|
+
const labelsValues = {
|
|
97
|
+
method: options.method || 'unknown',
|
|
98
|
+
service: serviceName || new URL(url).origin || 'unknown',
|
|
99
|
+
status: 'unknown',
|
|
100
|
+
};
|
|
101
|
+
req.on('response', (res) => {
|
|
102
|
+
labelsValues.status = res.statusCode.toString();
|
|
103
|
+
if (res.statusCode >= 400) {
|
|
104
|
+
requestsErrors.inc(labelsValues);
|
|
105
|
+
}
|
|
106
|
+
requestsTotal.inc(labelsValues);
|
|
107
|
+
timerDone(labelsValues);
|
|
108
|
+
});
|
|
109
|
+
req.on('error', (e) => {
|
|
110
|
+
if (POSSIBLE_ERRORS.includes(e === null || e === void 0 ? void 0 : e.code)) {
|
|
111
|
+
labelsValues.status = e.code;
|
|
112
|
+
}
|
|
113
|
+
requestsTotal.inc(labelsValues);
|
|
102
114
|
requestsErrors.inc(labelsValues);
|
|
103
|
-
|
|
104
|
-
requestsTotal.inc(labelsValues);
|
|
105
|
-
timerDone(labelsValues);
|
|
106
|
-
});
|
|
107
|
-
req.on('error', (e) => {
|
|
108
|
-
if (POSSIBLE_ERRORS.includes(e === null || e === void 0 ? void 0 : e.code)) {
|
|
109
|
-
labelsValues.status = e.code;
|
|
110
|
-
}
|
|
111
|
-
requestsTotal.inc(labelsValues);
|
|
112
|
-
requestsErrors.inc(labelsValues);
|
|
113
|
-
timerDone(labelsValues);
|
|
114
|
-
});
|
|
115
|
-
if (config.enableConnectionResolveMetrics) {
|
|
116
|
-
req.on('socket', (socket) => {
|
|
117
|
-
const timings = {
|
|
118
|
-
start: Date.now(),
|
|
119
|
-
lookupEnd: 0,
|
|
120
|
-
connectEnd: 0,
|
|
121
|
-
secureConnectEnd: 0,
|
|
122
|
-
};
|
|
123
|
-
const { service } = labelsValues;
|
|
124
|
-
socket.on('lookup', () => {
|
|
125
|
-
timings.lookupEnd = Date.now();
|
|
126
|
-
dnsResolveDuration.observe({ service }, getDuration(timings.lookupEnd, timings.start));
|
|
127
|
-
});
|
|
128
|
-
socket.on('connect', () => {
|
|
129
|
-
timings.connectEnd = Date.now();
|
|
130
|
-
tcpConnectDuration.observe({ service }, getDuration(timings.connectEnd, timings.lookupEnd));
|
|
131
|
-
});
|
|
132
|
-
socket.on('secureConnect', () => {
|
|
133
|
-
timings.secureConnectEnd = Date.now();
|
|
134
|
-
tlsHandshakeDuration.observe({ service }, getDuration(timings.secureConnectEnd, timings.connectEnd));
|
|
135
|
-
});
|
|
115
|
+
timerDone(labelsValues);
|
|
136
116
|
});
|
|
137
|
-
|
|
138
|
-
|
|
117
|
+
if (config.enableConnectionResolveMetrics) {
|
|
118
|
+
req.on('socket', (socket) => {
|
|
119
|
+
// due to keep-alive tcp option sockets might be reused
|
|
120
|
+
// ignore them because they have already emitted events we are interested in
|
|
121
|
+
if (socketSet.has(socket)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
socketSet.add(socket);
|
|
125
|
+
const timings = {
|
|
126
|
+
start: Date.now(),
|
|
127
|
+
lookupEnd: 0,
|
|
128
|
+
connectEnd: 0,
|
|
129
|
+
secureConnectEnd: 0,
|
|
130
|
+
};
|
|
131
|
+
const { service } = labelsValues;
|
|
132
|
+
socket.on('lookup', () => {
|
|
133
|
+
timings.lookupEnd = Date.now();
|
|
134
|
+
dnsResolveDuration.observe({ service }, getDuration(timings.lookupEnd, timings.start));
|
|
135
|
+
});
|
|
136
|
+
socket.on('connect', () => {
|
|
137
|
+
timings.connectEnd = Date.now();
|
|
138
|
+
tcpConnectDuration.observe({ service }, getDuration(timings.connectEnd, timings.lookupEnd));
|
|
139
|
+
});
|
|
140
|
+
socket.on('secureConnect', () => {
|
|
141
|
+
timings.secureConnectEnd = Date.now();
|
|
142
|
+
tlsHandshakeDuration.observe({ service }, getDuration(timings.secureConnectEnd, timings.connectEnd));
|
|
143
|
+
});
|
|
144
|
+
socket.on('close', () => {
|
|
145
|
+
socketSet.delete(socket);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return req;
|
|
150
|
+
};
|
|
139
151
|
};
|
|
140
152
|
|
|
141
153
|
const DEFAULT_BUCKETS = [0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 20, 40, 60];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-metrics",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.64.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"build-for-publish": "true"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@tramvai/core": "1.
|
|
23
|
-
"@tramvai/tokens-server": "1.
|
|
24
|
-
"@tramvai/tokens-metrics": "1.
|
|
25
|
-
"@tramvai/module-common": "1.
|
|
26
|
-
"@tramvai/tokens-http-client": "1.
|
|
27
|
-
"@tramvai/state": "1.
|
|
28
|
-
"@tramvai/papi": "1.
|
|
22
|
+
"@tramvai/core": "1.64.0",
|
|
23
|
+
"@tramvai/tokens-server": "1.64.0",
|
|
24
|
+
"@tramvai/tokens-metrics": "1.64.0",
|
|
25
|
+
"@tramvai/module-common": "1.64.0",
|
|
26
|
+
"@tramvai/tokens-http-client": "1.64.0",
|
|
27
|
+
"@tramvai/state": "1.64.0",
|
|
28
|
+
"@tramvai/papi": "1.64.0",
|
|
29
29
|
"@tinkoff/measure-express-requests": "1.4.3",
|
|
30
30
|
"@tinkoff/monkeypatch": "1.3.3",
|
|
31
31
|
"@tinkoff/url": "0.7.37",
|