mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-05-20 14:02:34 +02:00
Added monitor_tags to the prometheus metrics.
This commit is contained in:
parent
331027c896
commit
ed1b696d5b
2 changed files with 38 additions and 4 deletions
|
@ -328,7 +328,7 @@ class Monitor extends BeanModel {
|
||||||
let previousBeat = null;
|
let previousBeat = null;
|
||||||
let retries = 0;
|
let retries = 0;
|
||||||
|
|
||||||
this.prometheus = new Prometheus(this);
|
this.prometheus = new Prometheus(this, await this.getTags());
|
||||||
|
|
||||||
const beat = async () => {
|
const beat = async () => {
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ const commonLabels = [
|
||||||
"monitor_url",
|
"monitor_url",
|
||||||
"monitor_hostname",
|
"monitor_hostname",
|
||||||
"monitor_port",
|
"monitor_port",
|
||||||
|
"monitor_tags"
|
||||||
];
|
];
|
||||||
|
|
||||||
const monitorCertDaysRemaining = new PrometheusClient.Gauge({
|
const monitorCertDaysRemaining = new PrometheusClient.Gauge({
|
||||||
|
@ -37,17 +38,51 @@ class Prometheus {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {object} monitor Monitor object to monitor
|
* @param {object} monitor Monitor object to monitor
|
||||||
|
* @param {Array<object>} tags Tags to add to the monitor
|
||||||
*/
|
*/
|
||||||
constructor(monitor) {
|
constructor(monitor, tags) {
|
||||||
|
let sanitizedTags = this.sanitizeTags(tags);
|
||||||
|
|
||||||
|
if (sanitizedTags.length <= 0) {
|
||||||
|
sanitizedTags = "null";
|
||||||
|
}
|
||||||
|
|
||||||
this.monitorLabelValues = {
|
this.monitorLabelValues = {
|
||||||
monitor_name: monitor.name,
|
monitor_name: monitor.name,
|
||||||
monitor_type: monitor.type,
|
monitor_type: monitor.type,
|
||||||
monitor_url: monitor.url,
|
monitor_url: monitor.url,
|
||||||
monitor_hostname: monitor.hostname,
|
monitor_hostname: monitor.hostname,
|
||||||
monitor_port: monitor.port
|
monitor_port: monitor.port,
|
||||||
|
monitor_tags: sanitizedTags
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize tags to remove non-ASCII characters
|
||||||
|
* See https://github.com/louislam/uptime-kuma/pull/4704#issuecomment-2366524692
|
||||||
|
* @param {Array<object>} tags The tags to sanitize
|
||||||
|
* @returns {*[]} The sanitized tags
|
||||||
|
*/
|
||||||
|
sanitizeTags(tags) {
|
||||||
|
const nonAsciiRegex = /[^\x00-\x7F]/g;
|
||||||
|
const sanitizedTags = [];
|
||||||
|
tags.forEach((tag) => {
|
||||||
|
if (tag.name.match(nonAsciiRegex) || tag.value.match(nonAsciiRegex)) {
|
||||||
|
// If the tag name or value contains non-ASCII characters, skip it
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tag.value !== "") {
|
||||||
|
sanitizedTags.push(tag.name + ":" + tag.value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sanitizedTags.push(tag.name);
|
||||||
|
});
|
||||||
|
|
||||||
|
return sanitizedTags;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the metrics page
|
* Update the metrics page
|
||||||
* @param {object} heartbeat Heartbeat details
|
* @param {object} heartbeat Heartbeat details
|
||||||
|
@ -55,7 +90,6 @@ class Prometheus {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
update(heartbeat, tlsInfo) {
|
update(heartbeat, tlsInfo) {
|
||||||
|
|
||||||
if (typeof tlsInfo !== "undefined") {
|
if (typeof tlsInfo !== "undefined") {
|
||||||
try {
|
try {
|
||||||
let isValid;
|
let isValid;
|
||||||
|
|
Loading…
Add table
Reference in a new issue