Skip to content

Commit 3dd647b

Browse files
Merge pull request #159 from Arquisoft/157-arreglo-de-errores-de-sonarcloud
Arreglo fallo en prometheus
2 parents 42c1325 + 926b7d0 commit 3dd647b

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

gateway/gateway-service.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,17 @@ function buildRedirectDestination({ redirectHostname, httpsPort, requestPath = '
308308
return destination.toString();
309309
}
310310

311-
function createRedirectApp({ httpsPort = 443, redirectHostname = DEFAULT_REDIRECT_HTTPS_HOST } = {}) {
311+
function createRedirectApp({
312+
httpsPort = 443,
313+
redirectHostname = DEFAULT_REDIRECT_HTTPS_HOST,
314+
metricsHandler = null,
315+
} = {}) {
312316
const app = express();
313317

318+
if (metricsHandler) {
319+
app.get('/metrics', metricsHandler);
320+
}
321+
314322
app.use((req, res) => {
315323
const destination = buildRedirectDestination({
316324
redirectHostname,
@@ -1068,11 +1076,11 @@ function createApp({
10681076
}
10691077
}
10701078

1071-
return { app, proxyRoutes };
1079+
return { app, metrics, proxyRoutes };
10721080
}
10731081

10741082
function start({ port = DEFAULT_PORT, env = process.env } = {}) {
1075-
const { app } = createApp({ env });
1083+
const { app, metrics } = createApp({ env });
10761084
const tlsOptions = loadTlsOptions(env);
10771085

10781086
if (!tlsOptions) {
@@ -1099,6 +1107,7 @@ function start({ port = DEFAULT_PORT, env = process.env } = {}) {
10991107
const redirectApp = createRedirectApp({
11001108
httpsPort: redirectHttpsPort,
11011109
redirectHostname: redirectHost,
1110+
metricsHandler: metrics.handler,
11021111
});
11031112
httpServer = http.createServer(redirectApp);
11041113
httpServer.listen(port, () => {

gateway/tests/services/gateway-service.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,25 @@ test('createRedirectApp redirects requests to the configured HTTPS port', async
329329
});
330330
});
331331

332+
test('createRedirectApp exposes metrics over HTTP when a metrics handler is configured', async () => {
333+
const { metrics } = createApp({ proxyFactory: noopProxyFactory });
334+
const redirectApp = createRedirectApp({
335+
httpsPort: 8443,
336+
redirectHostname: 'gateway.example.com',
337+
metricsHandler: metrics.handler,
338+
});
339+
340+
await withServer(redirectApp, async (baseUrl) => {
341+
const response = await fetch(`${baseUrl}/metrics`, {
342+
redirect: 'manual',
343+
});
344+
const body = await response.text();
345+
346+
assert.equal(response.status, 200);
347+
assert.match(body, /# TYPE yovi_http_requests_total counter/);
348+
});
349+
});
350+
332351
// Redirect hostnames come from trusted server configuration only.
333352
test('getRedirectHostname falls back to localhost when unset', () => {
334353
assert.equal(getRedirectHostname({}), 'localhost');

0 commit comments

Comments
 (0)