Repository: louislam/uptime-kuma
File: server/prometheus.js (JavaScript)

Code before:
        if (typeof tlsInfo !== "undefined") {
            try {
                let isValid;
                if (tlsInfo.valid === true) {
                    isValid = 1;
                } else {
                    isValid = 0;
                }
                monitorCertIsValid.set(this.monitorLabelValues, isValid);
            } catch (e) {
                log.error("prometheus", "Caught error");
                log.error("prometheus", e);
            }

            try {
                if (tlsInfo.certInfo != null) {
                    monitorCertDaysRemaining.set(this.monitorLabelValues, tlsInfo.certInfo.daysRemaining);
                }
            } catch (e) {
                log.error("prometheus", "Caught error");
                log.error("prometheus", e);
            }
        }

        if (uptime) {
            if (typeof uptime.avgPing !== "undefined") {
                try {
                    if (typeof uptime.avgPing === "number") {
                        monitorAverageResponseTime.set(
                            this.monitorLabelValues,
                            uptime.avgPing
                        );
                    } else {
                        // Is it good?
                        monitorAverageResponseTime.set(
                            this.monitorLabelValues,
                            -1
                        );
                    }
                } catch (e) {
                    log.error("prometheus", "Caught error");
                    log.error("prometheus", e);
                }
            }
            if (typeof uptime.data24h !== "undefined") {
                try {
                    if (typeof uptime.data24h === "number") {
                        monitorUptime24h.set(
                            this.monitorLabelValues,
                            uptime.data24h
                        );

Review comment (suggestion):
I have no clue what war crimes we did commit here.
(Looking at the surrounding code, this fits in..)

I have no clue why the whole `typeof` verifcation exists in this class.
Seems strange.

We know what the type we pass into this function is..

Code after:
        });

        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,
        });
    }

    /**
     * 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 {object} 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] = [];
            }

            let tagValue = Prometheus.sanitizeForPrometheus(tag.value || "");
            if (tagValue !== "") {
                mappedTags[sanitizedTag].push(tagValue);
            }

            mappedTags[sanitizedTag] = mappedTags[sanitizedTag].sort();
        });