mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-07-19 07:44:02 +02:00
Adding monitor tags as individual prometheus labels instead of comma separating them into one label
This commit is contained in:
parent
073b12da51
commit
fe8c839237
2 changed files with 91 additions and 49 deletions
|
@ -1,38 +1,11 @@
|
||||||
const PrometheusClient = require("prom-client");
|
const PrometheusClient = require("prom-client");
|
||||||
const { log } = require("../src/util");
|
const { log } = require("../src/util");
|
||||||
|
const { R } = require("redbean-node");
|
||||||
|
|
||||||
const commonLabels = [
|
let monitorCertDaysRemaining = null;
|
||||||
"monitor_id",
|
let monitorCertIsValid = null;
|
||||||
"monitor_name",
|
let monitorResponseTime = null;
|
||||||
"monitor_type",
|
let monitorStatus = null;
|
||||||
"monitor_url",
|
|
||||||
"monitor_hostname",
|
|
||||||
"monitor_port",
|
|
||||||
"monitor_tags"
|
|
||||||
];
|
|
||||||
|
|
||||||
const monitorCertDaysRemaining = new PrometheusClient.Gauge({
|
|
||||||
name: "monitor_cert_days_remaining",
|
|
||||||
help: "The number of days remaining until the certificate expires",
|
|
||||||
labelNames: commonLabels
|
|
||||||
});
|
|
||||||
|
|
||||||
const monitorCertIsValid = new PrometheusClient.Gauge({
|
|
||||||
name: "monitor_cert_is_valid",
|
|
||||||
help: "Is the certificate still valid? (1 = Yes, 0= No)",
|
|
||||||
labelNames: commonLabels
|
|
||||||
});
|
|
||||||
const monitorResponseTime = new PrometheusClient.Gauge({
|
|
||||||
name: "monitor_response_time",
|
|
||||||
help: "Monitor Response Time (ms)",
|
|
||||||
labelNames: commonLabels
|
|
||||||
});
|
|
||||||
|
|
||||||
const monitorStatus = new PrometheusClient.Gauge({
|
|
||||||
name: "monitor_status",
|
|
||||||
help: "Monitor Status (1 = UP, 0= DOWN, 2= PENDING, 3= MAINTENANCE)",
|
|
||||||
labelNames: commonLabels
|
|
||||||
});
|
|
||||||
|
|
||||||
class Prometheus {
|
class Prometheus {
|
||||||
monitorLabelValues = {};
|
monitorLabelValues = {};
|
||||||
|
@ -49,31 +22,95 @@ class Prometheus {
|
||||||
monitor_url: monitor.url,
|
monitor_url: monitor.url,
|
||||||
monitor_hostname: monitor.hostname,
|
monitor_hostname: monitor.hostname,
|
||||||
monitor_port: monitor.port,
|
monitor_port: monitor.port,
|
||||||
|
...this.mapTagsToLabels(tags)
|
||||||
};
|
};
|
||||||
let sanitizedTags = this.sanitizeTags(tags);
|
|
||||||
if (sanitizedTags.length) {
|
|
||||||
this.monitorLabelValues.monitor_tags = sanitizedTags;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitize tags to ensure they can be processed by Prometheus.
|
* Initialize Prometheus metrics, and add all available tags as possible labels.
|
||||||
* See https://github.com/louislam/uptime-kuma/pull/4704#issuecomment-2366524692
|
* This should be called once at the start of the application.
|
||||||
* @param {Array<{name: string, value:?string}>} tags The tags to sanitize
|
* New tags will NOT be added dynamically, a restart is sadly required to add new tags to the metrics.
|
||||||
* @returns {string[]} The sanitized tags
|
* Existing tags added to monitors will be updated automatically.
|
||||||
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
sanitizeTags(tags) {
|
static async init() {
|
||||||
return tags.reduce((sanitizedTags, tag) => {
|
const tags = (await R.findAll("tag")).map((tag) => {
|
||||||
let tagText = tag.value ? `${tag.name}_${tag.value}` : tag.name;
|
return Prometheus.sanitizeForPrometheus(tag.name);
|
||||||
tagText = tagText.replace(/[^a-zA-Z0-9_]/g, "");
|
}).filter((tagName) => {
|
||||||
tagText = tagText.replace(/^[^a-zA-Z_]+/, "");
|
return tagName !== "";
|
||||||
|
});
|
||||||
|
|
||||||
if (tagText !== "") {
|
const commonLabels = [
|
||||||
sanitizedTags.push(tagText);
|
"monitor_id",
|
||||||
|
"monitor_name",
|
||||||
|
"monitor_type",
|
||||||
|
"monitor_url",
|
||||||
|
"monitor_hostname",
|
||||||
|
"monitor_port",
|
||||||
|
...tags // Add all available tags as possible labels
|
||||||
|
];
|
||||||
|
|
||||||
|
monitorCertDaysRemaining = new PrometheusClient.Gauge({
|
||||||
|
name: "monitor_cert_days_remaining",
|
||||||
|
help: "The number of days remaining until the certificate expires",
|
||||||
|
labelNames: commonLabels
|
||||||
|
});
|
||||||
|
|
||||||
|
monitorCertIsValid = new PrometheusClient.Gauge({
|
||||||
|
name: "monitor_cert_is_valid",
|
||||||
|
help: "Is the certificate still valid? (1 = Yes, 0= No)",
|
||||||
|
labelNames: commonLabels
|
||||||
|
});
|
||||||
|
|
||||||
|
monitorResponseTime = new PrometheusClient.Gauge({
|
||||||
|
name: "monitor_response_time",
|
||||||
|
help: "Monitor Response Time (ms)",
|
||||||
|
labelNames: commonLabels
|
||||||
|
});
|
||||||
|
|
||||||
|
monitorStatus = new PrometheusClient.Gauge({
|
||||||
|
name: "monitor_status",
|
||||||
|
help: "Monitor Status (1 = UP, 0= DOWN, 2= PENDING, 3= MAINTENANCE)",
|
||||||
|
labelNames: commonLabels
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return sanitizedTags;
|
/**
|
||||||
}, []);
|
* Sanitize a string to ensure it can be used as a Prometheus label or value.
|
||||||
|
* See https://github.com/louislam/uptime-kuma/pull/4704#issuecomment-2366524692
|
||||||
|
* @param {string} text The text to sanitize
|
||||||
|
* @returns {string} The sanitized text
|
||||||
|
*/
|
||||||
|
static sanitizeForPrometheus(text) {
|
||||||
|
text = text.replace(/[^a-zA-Z0-9_]/g, "");
|
||||||
|
text = text.replace(/^[^a-zA-Z_]+/, "");
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map the tags value to valid labels used in Prometheus. Sanitize them in the process.
|
||||||
|
* @param {Array<{name: string, value:?string}>} tags The tags to map
|
||||||
|
* @returns {Array<string, string>} The mapped tags, usable as labels
|
||||||
|
*/
|
||||||
|
mapTagsToLabels(tags) {
|
||||||
|
let mappedTags = {};
|
||||||
|
tags.forEach((tag) => {
|
||||||
|
let sanitizedTag = Prometheus.sanitizeForPrometheus(tag.name);
|
||||||
|
if (sanitizedTag === "") {
|
||||||
|
return; // Skip empty tag names
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mappedTags[sanitizedTag] === undefined) {
|
||||||
|
mappedTags[sanitizedTag] = "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
let tagValue = Prometheus.sanitizeForPrometheus(tag.value);
|
||||||
|
if (tagValue !== "") {
|
||||||
|
mappedTags[sanitizedTag] = mappedTags[sanitizedTag] !== "null" ? mappedTags[sanitizedTag] + `,${tagValue}` : tagValue;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return mappedTags;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -108,6 +108,8 @@ const { apiAuth } = require("./auth");
|
||||||
const { login } = require("./auth");
|
const { login } = require("./auth");
|
||||||
const passwordHash = require("./password-hash");
|
const passwordHash = require("./password-hash");
|
||||||
|
|
||||||
|
const { Prometheus } = require("./prometheus");
|
||||||
|
|
||||||
const hostname = config.hostname;
|
const hostname = config.hostname;
|
||||||
|
|
||||||
if (hostname) {
|
if (hostname) {
|
||||||
|
@ -192,6 +194,9 @@ let needSetup = false;
|
||||||
server.entryPage = await Settings.get("entryPage");
|
server.entryPage = await Settings.get("entryPage");
|
||||||
await StatusPage.loadDomainMappingList();
|
await StatusPage.loadDomainMappingList();
|
||||||
|
|
||||||
|
log.debug("server", "Initializing Prometheus");
|
||||||
|
await Prometheus.init();
|
||||||
|
|
||||||
log.debug("server", "Adding route");
|
log.debug("server", "Adding route");
|
||||||
|
|
||||||
// ***************************
|
// ***************************
|
||||||
|
|
Loading…
Add table
Reference in a new issue