From 30e5bffaf223c892afc01cf2fbbf1f599d3ef525 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 20 Aug 2021 02:41:46 -0400 Subject: [PATCH] stats: Add metrics for all HTTP status codes --- src/node/hooks/express.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/node/hooks/express.js b/src/node/hooks/express.js index c16374b0c..bc820ee3e 100644 --- a/src/node/hooks/express.js +++ b/src/node/hooks/express.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('underscore'); +const assert = require('assert').strict; const cookieParser = require('cookie-parser'); const events = require('events'); const express = require('express'); @@ -112,14 +113,14 @@ exports.restartServer = async () => { exports.server = http.createServer(app); } - // This error-handling middleware is installed first to ensure that no other middleware can - // prevent the stats from being updated. - app.use((err, req, res, next) => { - stats.meter('http500').mark(); - next(err); - }); - app.use((req, res, next) => { + // This stats logic should be in the first middleware to ensure that no other middleware can + // prevent the stats from being updated. + res.on('finish', () => { + assert(res.statusCode); + stats.meter(`http${res.statusCode}`).mark(); + }); + // res.header("X-Frame-Options", "deny"); // breaks embedded pads if (settings.ssl) { // we use SSL