diff --git a/components/server/src/express/ws-handler.ts b/components/server/src/express/ws-handler.ts index f9071dcd7320bf..2efbe64a3834ed 100644 --- a/components/server/src/express/ws-handler.ts +++ b/components/server/src/express/ws-handler.ts @@ -12,6 +12,7 @@ import * as url from 'url'; import * as net from 'net'; import { WsLayer } from './ws-layer'; import { log } from '@gitpod/gitpod-protocol/lib/util/logging'; +import { increaseHttpRequestCounter } from '../prometheus-metrics'; export type HttpServer = http.Server | https.Server; export type Route = string | RegExp; @@ -58,6 +59,10 @@ export class WsExpressHandler { stack.dispatch(ws, request).catch(err => { log.error("websocket stack error", err); ws.terminate(); + }).finally(() => { + const pathname = request.url ? url.parse(request.url).pathname : undefined; + const method = request.method || "UNKNOWN"; + increaseHttpRequestCounter(method, pathname || "unkown-websocket", request.statusCode || 0); }); } diff --git a/components/server/src/server.ts b/components/server/src/server.ts index e0005483c4d4c0..a09a7f86770e5a 100644 --- a/components/server/src/server.ts +++ b/components/server/src/server.ts @@ -81,21 +81,16 @@ export class Server { log.info('Initializing'); // metrics - app.use(async (req, res, next) => { - const startTime = Date.now() - const originalSend = res.send.bind(res); - res.send = (body) => { - const result = originalSend(body); + app.use((req: express.Request, res: express.Response, next: express.NextFunction) => { + const startTime = Date.now(); + req.on("end", () =>{ const method = req.method; - const route = req.route?.path || ""; - if (!req.route) { - log.warn(`route is undefined, url: ${req.url}`); - } + const route = req.route?.path || req.baseUrl || req.url || "unknown"; observeHttpRequestDuration(method, route, res.statusCode, (Date.now() - startTime) / 1000) increaseHttpRequestCounter(method, route, res.statusCode); - return result; - } - next(); + }); + + return next(); }); // Express configuration @@ -212,6 +207,7 @@ export class Server { } }); + // Health check + metrics endpoints this.monApp = this.monitoringEndpointsApp.create();