Compare commits

...

4 commits

Author SHA1 Message Date
Suven-p
6380df93c0 Fix relative path in test 2024-10-20 11:01:56 +05:45
Suven-p
444661e77c Save message for 503 status code 2024-10-20 10:47:01 +05:45
Suven-p
73ee62810d Bugfix: Add support for base url with path 2024-10-20 09:37:22 +05:45
Suven-p
bf32550f86 Fix linting error 2024-10-20 09:35:13 +05:45
2 changed files with 14 additions and 7 deletions

View file

@ -13,16 +13,20 @@ class RabbitMqMonitorType extends MonitorType {
let baseUrls = []; let baseUrls = [];
try { try {
baseUrls = JSON.parse(monitor.rabbitmqNodes); baseUrls = JSON.parse(monitor.rabbitmqNodes);
} } catch (error) {
catch (error) {
throw new Error("Invalid RabbitMQ Nodes"); throw new Error("Invalid RabbitMQ Nodes");
} }
heartbeat.status = DOWN; heartbeat.status = DOWN;
for (const baseUrl of baseUrls) { for (let baseUrl of baseUrls) {
try { try {
// Without a trailing slash, path in baseUrl will be removed. https://example.com/api -> https://example.com
if ( !baseUrl.endsWith("/") ) {
baseUrl += "/";
}
const options = { const options = {
url: new URL("/api/health/checks/alarms", baseUrl).href, // Do not start with slash, it will strip the trailing slash from baseUrl
url: new URL("api/health/checks/alarms/", baseUrl).href,
method: "get", method: "get",
timeout: monitor.timeout * 1000, timeout: monitor.timeout * 1000,
headers: { headers: {
@ -31,7 +35,8 @@ class RabbitMqMonitorType extends MonitorType {
}, },
// Use axios signal to handle connection timeouts https://stackoverflow.com/a/74739938 // Use axios signal to handle connection timeouts https://stackoverflow.com/a/74739938
signal: axiosAbortSignal((monitor.timeout + 10) * 1000), signal: axiosAbortSignal((monitor.timeout + 10) * 1000),
validateStatus: () => true, // Capture reason for 503 status
validateStatus: (status) => status === 200 || status === 503,
}; };
log.debug("monitor", `[${monitor.name}] Axios Request: ${JSON.stringify(options)}`); log.debug("monitor", `[${monitor.name}] Axios Request: ${JSON.stringify(options)}`);
const res = await axios.request(options); const res = await axios.request(options);
@ -40,6 +45,8 @@ class RabbitMqMonitorType extends MonitorType {
heartbeat.status = UP; heartbeat.status = UP;
heartbeat.msg = "OK"; heartbeat.msg = "OK";
break; break;
} else if (res.status === 503) {
heartbeat.msg = res.data.reason;
} else { } else {
heartbeat.msg = `${res.status} - ${res.statusText}`; heartbeat.msg = `${res.status} - ${res.statusText}`;
} }

View file

@ -1,8 +1,8 @@
const { describe, test } = require("node:test"); const { describe, test } = require("node:test");
const assert = require("node:assert"); const assert = require("node:assert");
const { RabbitMQContainer } = require("@testcontainers/rabbitmq"); const { RabbitMQContainer } = require("@testcontainers/rabbitmq");
const { RabbitMqMonitorType } = require("../../../server/monitor-types/rabbitmq"); const { RabbitMqMonitorType } = require("../../server/monitor-types/rabbitmq");
const { UP, DOWN, PENDING } = require("../../../src/util"); const { UP, DOWN, PENDING } = require("../../src/util");
describe("RabbitMQ Single Node", { describe("RabbitMQ Single Node", {
skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64"), skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64"),