Added sorting to label definition

This commit is contained in:
Rick van Drongelen 2025-07-03 15:27:48 +02:00
parent 2e597c7697
commit 9e4d6f1b70
No known key found for this signature in database

View file

@ -40,7 +40,7 @@ class Prometheus {
return Prometheus.sanitizeForPrometheus(tag.name); return Prometheus.sanitizeForPrometheus(tag.name);
}).filter((tagName) => { }).filter((tagName) => {
return tagName !== ""; return tagName !== "";
})); }).sort(this.sortTags));
const commonLabels = [ const commonLabels = [
...tags, ...tags,
@ -86,7 +86,7 @@ class Prometheus {
static sanitizeForPrometheus(text) { static sanitizeForPrometheus(text) {
text = text.replace(/[^a-zA-Z0-9_]/g, ""); text = text.replace(/[^a-zA-Z0-9_]/g, "");
text = text.replace(/^[^a-zA-Z_]+/, ""); text = text.replace(/^[^a-zA-Z_]+/, "");
return text.toLowerCase(); return text;
} }
/** /**
@ -106,7 +106,7 @@ class Prometheus {
mappedTags[sanitizedTag] = []; mappedTags[sanitizedTag] = [];
} }
let tagValue = Prometheus.sanitizeForPrometheus(tag.value); let tagValue = Prometheus.sanitizeForPrometheus(tag.value || "");
if (tagValue !== "") { if (tagValue !== "") {
mappedTags[sanitizedTag].push(tagValue); mappedTags[sanitizedTag].push(tagValue);
} }
@ -115,7 +115,7 @@ class Prometheus {
}); });
// Order the tags alphabetically // Order the tags alphabetically
return Object.keys(mappedTags).sort().reduce((obj, key) => { return Object.keys(mappedTags).sort(this.sortTags).reduce((obj, key) => {
obj[key] = mappedTags[key]; obj[key] = mappedTags[key];
return obj; return obj;
}, {}); }, {});
@ -188,6 +188,27 @@ class Prometheus {
console.error(e); console.error(e);
} }
} }
/**
* Sort the tags alphabetically, case-insensitive.
* @param a {string}
* @param b {string}
* @returns {number}
*/
sortTags(a, b) {
const aLowerCase = a.toLowerCase();
const bLowerCase = b.toLowerCase();
if (aLowerCase < bLowerCase) {
return -1;
}
if (aLowerCase > bLowerCase) {
return 1;
}
return 0;
}
} }
module.exports = { module.exports = {