From 92a3d17681a0647783ccc6789730377f9ec9ea6c Mon Sep 17 00:00:00 2001 From: Thomas Lee Date: Mon, 1 Jul 2024 13:53:32 +0800 Subject: [PATCH] [ISSUE #12196] FIX prometheus http sd invalid label names (#12239) * prometheus http sd: fix invalid label names * Update PrometheusUtils.java --- .../com/alibaba/nacos/prometheus/utils/PrometheusUtils.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prometheus/src/main/java/com/alibaba/nacos/prometheus/utils/PrometheusUtils.java b/prometheus/src/main/java/com/alibaba/nacos/prometheus/utils/PrometheusUtils.java index 61098c02d..8ce8cb07a 100644 --- a/prometheus/src/main/java/com/alibaba/nacos/prometheus/utils/PrometheusUtils.java +++ b/prometheus/src/main/java/com/alibaba/nacos/prometheus/utils/PrometheusUtils.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import static java.util.stream.Collectors.groupingBy; @@ -62,6 +63,9 @@ public class PrometheusUtils { labelNode.put("__meta_clusterName", clusterName); //export metadata Map metadata = instance.getMetadata(); + // auto convert label names contain with "." and "-" to "_" + metadata = metadata.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().replace(".", "_").replace("-", "_"), e -> e.getValue())); + metadata.forEach(labelNode::put); ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode(); jsonNode.replace("targets", targetsNode);