From a904a8fb9d62c91d49903010b3e9d541b5e1598c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sat, 13 Mar 2021 19:30:59 +0100 Subject: [PATCH] Add hit counter for HTTP requests Add a complementary total hit counter for all HTTP requests. The endpointHitCounter doesn't cover all endpoints and fixing it to cover all endpoints without making it possible for it to create an infinite number of time seriers is not straight forward. --- packages/master/master.js | 43 ++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/master/master.js b/packages/master/master.js index 27ac8869..fa65b2f4 100755 --- a/packages/master/master.js +++ b/packages/master/master.js @@ -80,18 +80,6 @@ let httpServerCloser; let httpsServer; let httpsServerCloser; -app.use(cookieParser()); -app.use(bodyParser.json({ - limit: "10mb", -})); -app.use(bodyParser.urlencoded({ - parameterLimit: 100000, - limit: "10mb", - extended: true, -})); -app.use(fileUpload()); -app.use(compression()); - // Servers the web interface with the root path set apropriately. function serveWeb(route) { @@ -112,10 +100,6 @@ function serveWeb(route) { }; } -// Set folder to serve static content from (the website) -app.use(express.static(path.join(__dirname, "dist", "web"))); -app.use(express.static("static")); // Used for data export files - const slaveMappingGauge = new libPrometheus.Gauge( "clusterio_master_slave_mapping", "Mapping of Slave ID to name", @@ -151,6 +135,11 @@ const instanceMappingGauge = new libPrometheus.Gauge( } ); +const hitCounter = new libPrometheus.Counter( + "clusterio_master_http_hits_total", + "How many HTTP requests in total have been received" +); + const endpointHitCounter = new libPrometheus.Counter( "clusterio_master_http_endpoint_hits_total", "How many requests a particular HTTP endpoint has gotten", @@ -183,6 +172,28 @@ const wsActiveSlavesGauge = new libPrometheus.Gauge( "How many slaves are currently connected to the master" ); + +app.use((req, res, next) => { + hitCounter.inc(); + next(); +}); +app.use(cookieParser()); +app.use(bodyParser.json({ + limit: "10mb", +})); +app.use(bodyParser.urlencoded({ + parameterLimit: 100000, + limit: "10mb", + extended: true, +})); +app.use(fileUpload()); +app.use(compression()); + +// Set folder to serve static content from (the website) +app.use(express.static(path.join(__dirname, "dist", "web"))); +app.use(express.static("static")); // Used for data export files + + // Merges samples from sourceResult to destinationResult function mergeSamples(destinationResult, sourceResult) { // Merge metrics received by multiple slaves