@oxog/spark 1.1.0 → 1.2.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/CHANGELOG.md +632 -293
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1070 -956
- package/src/core/context.js +1100 -830
- package/src/core/middleware.js +305 -217
- package/src/index.js +183 -278
- package/src/middleware/body-parser.js +484 -526
- package/src/middleware/cache.js +179 -170
- package/src/middleware/compression.js +179 -182
- package/src/middleware/cors.js +178 -95
- package/src/middleware/health.js +221 -218
- package/src/middleware/logger.js +197 -194
- package/src/middleware/metrics.js +341 -327
- package/src/middleware/rate-limit.js +369 -319
- package/src/middleware/security.js +444 -332
- package/src/middleware/session.js +649 -529
- package/src/middleware/static.js +406 -299
- package/src/router/layer.js +199 -117
- package/src/router/route.js +151 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +147 -144
- package/src/utils/context-pool.js +85 -82
- package/src/utils/regex-validator.js +189 -176
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -957
- package/dist/core/context.js +0 -831
- package/dist/core/middleware.js +0 -218
- package/dist/core/request.js +0 -333
- package/dist/core/response.js +0 -375
- package/dist/index.js +0 -278
- package/dist/middleware/body-parser.js +0 -526
- package/dist/middleware/cache.js +0 -171
- package/dist/middleware/compression.js +0 -183
- package/dist/middleware/cors.js +0 -95
- package/dist/middleware/health.js +0 -219
- package/dist/middleware/index.js +0 -21
- package/dist/middleware/logger.js +0 -195
- package/dist/middleware/metrics.js +0 -328
- package/dist/middleware/rate-limit.js +0 -320
- package/dist/middleware/security.js +0 -333
- package/dist/middleware/session.js +0 -530
- package/dist/middleware/static.js +0 -299
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -117
- package/dist/router/route.js +0 -123
- package/dist/router/router.js +0 -756
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -145
- package/dist/utils/cache.js +0 -441
- package/dist/utils/context-pool.js +0 -83
- package/dist/utils/error-types.js +0 -132
- package/dist/utils/http.js +0 -413
- package/dist/utils/regex-validator.js +0 -177
- package/src/core/request.js +0 -333
- package/src/core/response.js +0 -375
- package/src/middleware/index.js +0 -21
- package/src/utils/cache.js +0 -441
- package/src/utils/error-types.js +0 -132
- package/src/utils/http.js +0 -413
|
@@ -1,328 +1,342 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.memoryInterval =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
//
|
|
64
|
-
this.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.metrics.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
responseTime
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
//
|
|
294
|
-
collector.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/** @constant {number} Maximum distinct paths tracked in requests.byPath */
|
|
4
|
+
const MAX_TRACKED_PATHS = 1000;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Metrics collection middleware for performance monitoring
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
class MetricsCollector {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.metrics = {
|
|
13
|
+
requests: {
|
|
14
|
+
total: 0,
|
|
15
|
+
byMethod: {},
|
|
16
|
+
byStatus: {},
|
|
17
|
+
byPath: {}
|
|
18
|
+
},
|
|
19
|
+
responseTime: {
|
|
20
|
+
total: 0,
|
|
21
|
+
count: 0,
|
|
22
|
+
min: Infinity,
|
|
23
|
+
max: 0,
|
|
24
|
+
buckets: {
|
|
25
|
+
'<100ms': 0,
|
|
26
|
+
'100-500ms': 0,
|
|
27
|
+
'500ms-1s': 0,
|
|
28
|
+
'1s-5s': 0,
|
|
29
|
+
'>5s': 0
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
errors: {
|
|
33
|
+
total: 0,
|
|
34
|
+
byType: {},
|
|
35
|
+
byStatus: {}
|
|
36
|
+
},
|
|
37
|
+
activeConnections: 0,
|
|
38
|
+
memory: {
|
|
39
|
+
samples: [],
|
|
40
|
+
maxSamples: 100
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
this.startTime = Date.now();
|
|
45
|
+
this.memoryInterval = null;
|
|
46
|
+
this.setupMemoryTracking();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setupMemoryTracking() {
|
|
50
|
+
this.memoryInterval = setInterval(() => {
|
|
51
|
+
const memUsage = process.memoryUsage();
|
|
52
|
+
this.metrics.memory.samples.push({
|
|
53
|
+
timestamp: Date.now(),
|
|
54
|
+
...memUsage
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Keep only last 100 samples
|
|
58
|
+
if (this.metrics.memory.samples.length > this.metrics.memory.maxSamples) {
|
|
59
|
+
this.metrics.memory.samples.shift();
|
|
60
|
+
}
|
|
61
|
+
}, 30000); // Every 30 seconds
|
|
62
|
+
|
|
63
|
+
// Don't keep process alive
|
|
64
|
+
this.memoryInterval.unref();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
recordRequest(method, path, status, duration, error = null) {
|
|
68
|
+
// Total requests
|
|
69
|
+
this.metrics.requests.total++;
|
|
70
|
+
|
|
71
|
+
// By method
|
|
72
|
+
this.metrics.requests.byMethod[method] = (this.metrics.requests.byMethod[method] || 0) + 1;
|
|
73
|
+
|
|
74
|
+
// By status
|
|
75
|
+
this.metrics.requests.byStatus[status] = (this.metrics.requests.byStatus[status] || 0) + 1;
|
|
76
|
+
|
|
77
|
+
// By path (sanitized)
|
|
78
|
+
const sanitizedPath = this.sanitizePath(path);
|
|
79
|
+
const byPath = this.metrics.requests.byPath;
|
|
80
|
+
// Cap distinct paths so random URLs (scanners, 404 probes) cannot grow memory without bound
|
|
81
|
+
const pathKey = sanitizedPath in byPath || Object.keys(byPath).length < MAX_TRACKED_PATHS
|
|
82
|
+
? sanitizedPath
|
|
83
|
+
: '(other)';
|
|
84
|
+
byPath[pathKey] = (byPath[pathKey] || 0) + 1;
|
|
85
|
+
|
|
86
|
+
// Response time
|
|
87
|
+
this.metrics.responseTime.total += duration;
|
|
88
|
+
this.metrics.responseTime.count++;
|
|
89
|
+
this.metrics.responseTime.min = Math.min(this.metrics.responseTime.min, duration);
|
|
90
|
+
this.metrics.responseTime.max = Math.max(this.metrics.responseTime.max, duration);
|
|
91
|
+
|
|
92
|
+
// Response time buckets
|
|
93
|
+
if (duration < 100) {
|
|
94
|
+
this.metrics.responseTime.buckets['<100ms']++;
|
|
95
|
+
} else if (duration < 500) {
|
|
96
|
+
this.metrics.responseTime.buckets['100-500ms']++;
|
|
97
|
+
} else if (duration < 1000) {
|
|
98
|
+
this.metrics.responseTime.buckets['500ms-1s']++;
|
|
99
|
+
} else if (duration < 5000) {
|
|
100
|
+
this.metrics.responseTime.buckets['1s-5s']++;
|
|
101
|
+
} else {
|
|
102
|
+
this.metrics.responseTime.buckets['>5s']++;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Errors
|
|
106
|
+
if (error || status >= 400) {
|
|
107
|
+
this.metrics.errors.total++;
|
|
108
|
+
this.metrics.errors.byStatus[status] = (this.metrics.errors.byStatus[status] || 0) + 1;
|
|
109
|
+
|
|
110
|
+
if (error) {
|
|
111
|
+
const errorType = error.constructor.name;
|
|
112
|
+
this.metrics.errors.byType[errorType] = (this.metrics.errors.byType[errorType] || 0) + 1;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
sanitizePath(path) {
|
|
118
|
+
// Replace IDs and numbers with placeholders
|
|
119
|
+
return path
|
|
120
|
+
.replace(/\/\d+/g, '/:id')
|
|
121
|
+
.replace(/\/[a-f0-9]{24}/g, '/:id') // MongoDB ObjectIds
|
|
122
|
+
.replace(/\/[a-f0-9-]{36}/g, '/:uuid'); // UUIDs
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
incrementActiveConnections() {
|
|
126
|
+
this.metrics.activeConnections++;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
decrementActiveConnections() {
|
|
130
|
+
this.metrics.activeConnections = Math.max(0, this.metrics.activeConnections - 1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getMetrics() {
|
|
134
|
+
const now = Date.now();
|
|
135
|
+
const uptime = now - this.startTime;
|
|
136
|
+
|
|
137
|
+
const avgResponseTime = this.metrics.responseTime.count > 0
|
|
138
|
+
? this.metrics.responseTime.total / this.metrics.responseTime.count
|
|
139
|
+
: 0;
|
|
140
|
+
|
|
141
|
+
// SECURITY: Prevent division by zero when uptime is 0 (immediately after startup)
|
|
142
|
+
const uptimeSeconds = uptime / 1000;
|
|
143
|
+
const rps = uptimeSeconds > 0 ? this.metrics.requests.total / uptimeSeconds : 0;
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
timestamp: now,
|
|
147
|
+
uptime: uptime,
|
|
148
|
+
requests: {
|
|
149
|
+
...this.metrics.requests,
|
|
150
|
+
rps: rps // requests per second (safe from division by zero)
|
|
151
|
+
},
|
|
152
|
+
responseTime: {
|
|
153
|
+
...this.metrics.responseTime,
|
|
154
|
+
average: avgResponseTime,
|
|
155
|
+
min: this.metrics.responseTime.min === Infinity ? 0 : this.metrics.responseTime.min
|
|
156
|
+
},
|
|
157
|
+
errors: {
|
|
158
|
+
...this.metrics.errors,
|
|
159
|
+
errorRate: this.metrics.requests.total > 0
|
|
160
|
+
? (this.metrics.errors.total / this.metrics.requests.total * 100).toFixed(2) + '%'
|
|
161
|
+
: '0%'
|
|
162
|
+
},
|
|
163
|
+
activeConnections: this.metrics.activeConnections,
|
|
164
|
+
memory: this.getMemoryStats()
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
getMemoryStats() {
|
|
169
|
+
if (this.metrics.memory.samples.length === 0) {
|
|
170
|
+
return { current: process.memoryUsage() };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const latest = this.metrics.memory.samples[this.metrics.memory.samples.length - 1];
|
|
174
|
+
const samples = this.metrics.memory.samples;
|
|
175
|
+
|
|
176
|
+
const heapUsedSamples = samples.map(s => s.heapUsed);
|
|
177
|
+
const avg = heapUsedSamples.reduce((a, b) => a + b, 0) / heapUsedSamples.length;
|
|
178
|
+
const max = Math.max(...heapUsedSamples);
|
|
179
|
+
const min = Math.min(...heapUsedSamples);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
current: {
|
|
183
|
+
heapUsed: latest.heapUsed,
|
|
184
|
+
heapTotal: latest.heapTotal,
|
|
185
|
+
external: latest.external,
|
|
186
|
+
rss: latest.rss
|
|
187
|
+
},
|
|
188
|
+
stats: {
|
|
189
|
+
average: avg,
|
|
190
|
+
max: max,
|
|
191
|
+
min: min,
|
|
192
|
+
samples: samples.length
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
reset() {
|
|
198
|
+
this.metrics = {
|
|
199
|
+
requests: {
|
|
200
|
+
total: 0,
|
|
201
|
+
byMethod: {},
|
|
202
|
+
byStatus: {},
|
|
203
|
+
byPath: {}
|
|
204
|
+
},
|
|
205
|
+
responseTime: {
|
|
206
|
+
total: 0,
|
|
207
|
+
count: 0,
|
|
208
|
+
min: Infinity,
|
|
209
|
+
max: 0,
|
|
210
|
+
buckets: {
|
|
211
|
+
'<100ms': 0,
|
|
212
|
+
'100-500ms': 0,
|
|
213
|
+
'500ms-1s': 0,
|
|
214
|
+
'1s-5s': 0,
|
|
215
|
+
'>5s': 0
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
errors: {
|
|
219
|
+
total: 0,
|
|
220
|
+
byType: {},
|
|
221
|
+
byStatus: {}
|
|
222
|
+
},
|
|
223
|
+
activeConnections: 0,
|
|
224
|
+
memory: {
|
|
225
|
+
samples: [],
|
|
226
|
+
maxSamples: 100
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
this.startTime = Date.now();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
stop() {
|
|
234
|
+
if (this.memoryInterval) {
|
|
235
|
+
clearInterval(this.memoryInterval);
|
|
236
|
+
this.memoryInterval = null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Global metrics collector instance
|
|
242
|
+
const globalCollector = new MetricsCollector();
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Metrics middleware
|
|
246
|
+
* @param {Object} options - Metrics options
|
|
247
|
+
* @returns {Function} Middleware function
|
|
248
|
+
*/
|
|
249
|
+
function metrics(options = {}) {
|
|
250
|
+
const opts = {
|
|
251
|
+
path: '/_metrics',
|
|
252
|
+
collectResponseTime: true,
|
|
253
|
+
collectMemory: true,
|
|
254
|
+
excludePaths: ['/favicon.ico'],
|
|
255
|
+
customCollector: null,
|
|
256
|
+
...options
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const collector = opts.customCollector || globalCollector;
|
|
260
|
+
|
|
261
|
+
const middleware = async (ctx, next) => {
|
|
262
|
+
// Serve metrics endpoint
|
|
263
|
+
if (ctx.path === opts.path) {
|
|
264
|
+
if (ctx.method !== 'GET') {
|
|
265
|
+
ctx.status(405).json({ error: 'Method Not Allowed' });
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const metricsData = collector.getMetrics();
|
|
270
|
+
ctx.json(metricsData);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Skip excluded paths
|
|
275
|
+
if (opts.excludePaths.includes(ctx.path)) {
|
|
276
|
+
return next();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Track active connections
|
|
280
|
+
collector.incrementActiveConnections();
|
|
281
|
+
|
|
282
|
+
const startTime = Date.now();
|
|
283
|
+
let error = null;
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
await next();
|
|
287
|
+
} catch (err) {
|
|
288
|
+
error = err;
|
|
289
|
+
throw err;
|
|
290
|
+
} finally {
|
|
291
|
+
const duration = Date.now() - startTime;
|
|
292
|
+
|
|
293
|
+
// Record metrics
|
|
294
|
+
collector.recordRequest(
|
|
295
|
+
ctx.method,
|
|
296
|
+
ctx.path,
|
|
297
|
+
ctx.statusCode,
|
|
298
|
+
duration,
|
|
299
|
+
error
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
// Add response time header
|
|
303
|
+
if (opts.collectResponseTime) {
|
|
304
|
+
ctx.set('X-Response-Time', `${duration}ms`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Decrement active connections
|
|
308
|
+
collector.decrementActiveConnections();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// Add cleanup method to middleware
|
|
313
|
+
middleware.cleanup = () => {
|
|
314
|
+
collector.stop();
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// Add collector access
|
|
318
|
+
middleware.collector = collector;
|
|
319
|
+
|
|
320
|
+
return middleware;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Create a custom metrics collector
|
|
325
|
+
* @returns {MetricsCollector} New metrics collector instance
|
|
326
|
+
*/
|
|
327
|
+
function createCollector() {
|
|
328
|
+
return new MetricsCollector();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Get global metrics collector
|
|
333
|
+
* @returns {MetricsCollector} Global collector instance
|
|
334
|
+
*/
|
|
335
|
+
function getGlobalCollector() {
|
|
336
|
+
return globalCollector;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
module.exports = metrics;
|
|
340
|
+
module.exports.createCollector = createCollector;
|
|
341
|
+
module.exports.getGlobalCollector = getGlobalCollector;
|
|
328
342
|
module.exports.MetricsCollector = MetricsCollector;
|