Use statuscode 499 for requests that are closed before response is sent

This happens, for example, when a http proxy in front of the application is configured with a timeout and the node server is too slow to respond.

Currently such timeouts are counted as 200s by express-prom-bundle. This PR changes that to 499 "Client Closed Request". This way it's possible to tell them apart.
This commit is contained in:
Pasi Tuominen
2023-05-05 14:48:37 +03:00
parent 0bda9934c1
commit efbab7dcdb
2 changed files with 12 additions and 2 deletions

View File

@@ -6,7 +6,11 @@ const normalizeStatusCode = require('../src/normalizeStatusCode');
describe('normalizeStatusCode', () => {
it('returns run callback if configured', () => {
expect(
normalizeStatusCode({status_code: 500})
normalizeStatusCode({status_code: 500, headersSent: true})
).toBe(500);
});
it('returns 499 if headers are not sent', () => {
expect(normalizeStatusCode({statusCode: 200, headersSent: false})).toBe(499);
});
});

View File

@@ -1,5 +1,11 @@
'use strict';
const CLIENT_CLOSED_REQUEST_CODE = 499;
module.exports = function(res) {
return res.status_code || res.statusCode;
if (res.headersSent) {
return res.status_code || res.statusCode;
} else {
return CLIENT_CLOSED_REQUEST_CODE;
}
};