From efbab7dcdb10794af03dc3eb3ef05f1eedc5bd1a Mon Sep 17 00:00:00 2001 From: Pasi Tuominen Date: Fri, 5 May 2023 14:48:37 +0300 Subject: [PATCH] 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. --- spec/normalizeStatusCode.spec.js | 6 +++++- src/normalizeStatusCode.js | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/normalizeStatusCode.spec.js b/spec/normalizeStatusCode.spec.js index d2720db..85e0cca 100644 --- a/spec/normalizeStatusCode.spec.js +++ b/spec/normalizeStatusCode.spec.js @@ -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); + }); }); diff --git a/src/normalizeStatusCode.js b/src/normalizeStatusCode.js index 545bf2f..7c11c5c 100644 --- a/src/normalizeStatusCode.js +++ b/src/normalizeStatusCode.js @@ -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; + } };