Skip to content

Commit

Permalink
Merge pull request #1694 from navikt/better_hpm_log
Browse files Browse the repository at this point in the history
eject plugins fra HPM for å redusere støy
  • Loading branch information
kenglxn authored Sep 28, 2023
2 parents 8a668c0 + 616b802 commit a4d1a00
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import path from 'path';
import express from 'express';
import Mustache from 'mustache';
import httpProxyMiddleware, { responseInterceptor } from 'http-proxy-middleware';
import httpProxyMiddleware, {
debugProxyErrorsPlugin,
errorResponsePlugin,
proxyEventsPlugin,
responseInterceptor,
} from 'http-proxy-middleware';
import { createHttpTerminator } from 'http-terminator';
import Prometheus from 'prom-client';
import { createLogger, format, transports } from 'winston';
Expand All @@ -28,6 +33,11 @@ const log_events_counter = new Prometheus.Counter({
help: 'Antall log events fordelt på level',
labelNames: ['level'],
});
const proxy_events_counter = new Prometheus.Counter({
name: 'proxy_events_total',
help: 'Antall proxy events',
labelNames: ['target', 'proxystatus', 'status', 'errcode'],
});

const maskFormat = format((info) => ({
...info,
Expand Down Expand Up @@ -55,6 +65,61 @@ const log = new Proxy(
}
);

// copy with mods from http-proxy-middleware https://github.com/chimurai/http-proxy-middleware/blob/master/src/plugins/default/logger-plugin.ts
const loggerPlugin = (proxyServer, options) => {
proxyServer.on('error', (err, req, res, target) => {
const hostname = req?.headers?.host;
// target is undefined when websocket errors
const errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page
proxy_events_counter.inc({
target: target.host,
proxystatus: null,
status: res.statusCode,
errcode: err.code || 'unknown',
});
const level =
/HPE_INVALID/.test(err.code) ||
['ECONNRESET', 'ENOTFOUND', 'ECONNREFUSED', 'ETIMEDOUT'].includes(err.code)
? 'warn'
: 'error';
log.log(
level,
'[HPM] Error occurred while proxying request %s to %s [%s] (%s)',
`${hostname}${req?.host}${req?.path}`,
`${target?.href}`,
err.code || err,
errReference
);
});

proxyServer.on('proxyRes', (proxyRes, req, res) => {
const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;
const pathUpToSearch = proxyRes.req.path.replace(/\?.*$/, '');
const exchange = `[HPM] ${req.method} ${originalUrl} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${pathUpToSearch} [${proxyRes.statusCode}]`;
proxy_events_counter.inc({
target: proxyRes.req.host,
proxystatus: proxyRes.statusCode,
status: res.statusCode,
errcode: null,
});
log.info(exchange);
});

/**
* When client opens WebSocket connection
*/
proxyServer.on('open', (socket) => {
log.info('[HPM] Client connected: %o', socket.address());
});

/**
* When client closes WebSocket connection
*/
proxyServer.on('close', (req, proxySocket, proxyHead) => {
log.info('[HPM] Client disconnected: %o', proxySocket.address());
});
};

log.info(`Frackend startup: ${JSON.stringify({ NAIS_CLUSTER_NAME, MILJO, GIT_COMMIT })}`);

let BUILD_PATH = path.join(process.cwd(), '../build');
Expand Down Expand Up @@ -132,6 +197,8 @@ const main = async () => {
secure: true,
xfwd: true,
changeOrigin: true,
ejectPlugins: true,
plugins: [debugProxyErrorsPlugin, errorResponsePlugin, loggerPlugin, proxyEventsPlugin],
};
app.use(
'/min-side-arbeidsgiver/tiltaksgjennomforing-api',
Expand Down

0 comments on commit a4d1a00

Please sign in to comment.