(req, res, next)
| 36 | |
| 37 | // Middleware function |
| 38 | const middleware = (req, res, next) => { |
| 39 | const startTime = Date.now(); |
| 40 | |
| 41 | // Store original end method |
| 42 | const originalEnd = res.end; |
| 43 | |
| 44 | // Override end method to capture metrics |
| 45 | res.end = function (...args) { |
| 46 | const duration = (Date.now() - startTime) / 1000; // Convert to seconds |
| 47 | const route = req.route || req.url || "unknown"; |
| 48 | const method = req.method || "unknown"; |
| 49 | const statusCode = res.statusCode || 0; |
| 50 | |
| 51 | // Build labels object |
| 52 | const labels = { |
| 53 | method: config.includeMethod ? method : "HTTP", |
| 54 | route: config.includePath ? route : "", |
| 55 | status_code: config.includeStatusCode ? statusCode.toString() : "", |
| 56 | ...config.customLabels |
| 57 | }; |
| 58 | |
| 59 | // Record metrics |
| 60 | httpRequestDuration.observe(labels, duration); |
| 61 | httpRequestsTotal.inc(labels); |
| 62 | |
| 63 | // Call original end method |
| 64 | originalEnd.apply(this, args); |
| 65 | }; |
| 66 | |
| 67 | if (typeof next === "function") { |
| 68 | next(); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | return {middleware, register}; |
| 73 | } |
no outgoing calls
no test coverage detected