Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eject plugins fra HPM for å redusere støy #1694

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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