From be17fbd2354d3434eb20ddf3709e79f9c433e9d1 Mon Sep 17 00:00:00 2001 From: Kurok1 Date: Wed, 29 May 2024 16:26:14 +0800 Subject: [PATCH 1/2] add metadata as labels in prometheus http sd --- .../prometheus/utils/PrometheusUtils.java | 38 +++++++++++++------ .../controller/PrometheusControllerTest.java | 12 +++++- 2 files changed, 37 insertions(+), 13 deletions(-) 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 9fd377af57e..887167568e8 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 @@ -36,21 +36,37 @@ public class PrometheusUtils { /** * Assemble arrayNodes for prometheus sd api. - * */ public static void assembleArrayNodes(Set targetSet, ArrayNode arrayNode) { Map> groupingInsMap = targetSet.stream().collect(groupingBy(Instance::getClusterName)); groupingInsMap.forEach((key, value) -> { - ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode(); - ArrayNode targetsNode = JacksonUtils.createEmptyArrayNode(); - ObjectNode labelNode = JacksonUtils.createEmptyJsonNode(); - value.forEach(e -> { - targetsNode.add(e.getIp() + ":" + e.getPort()); - }); - labelNode.put("__meta_clusterName", key); - jsonNode.replace("targets", targetsNode); - jsonNode.replace("labels", labelNode); - arrayNode.add(jsonNode); + for (Instance instance : value) { + ObjectNode jsonNode = assembleInstanceToArrayNode(key, instance); + arrayNode.add(jsonNode); + } }); } + + /** + * assemble instance to json node, and export metadata to label + * + * @param clusterName the cluster name + * @param instance instance info + */ + private static ObjectNode assembleInstanceToArrayNode(String clusterName, Instance instance) { + ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode(); + ArrayNode targetsNode = JacksonUtils.createEmptyArrayNode(); + ObjectNode labelNode = JacksonUtils.createEmptyJsonNode(); + + targetsNode.add(instance.getIp() + ":" + instance.getPort()); + //mark cluster name + labelNode.put("__meta_clusterName", clusterName); + //export metadata + Map metadata = instance.getMetadata(); + metadata.forEach(labelNode::put); + + jsonNode.replace("targets", targetsNode); + jsonNode.replace("labels", labelNode); + return jsonNode; + } } diff --git a/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java b/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java index 23dba8fa7f3..52f638d62c4 100644 --- a/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java +++ b/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java @@ -39,7 +39,9 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; @@ -77,12 +79,18 @@ public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosEx service = Service.newService(nameSpace, group, name); serviceManager.getSingleton(service); testInstanceList = new ArrayList<>(); + testInstanceList.add(prepareInstance("A", "127.0.0.1", 8080, Collections.singletonMap("__meta_key", "value"))); + testInstanceList.add(prepareInstance("A", "127.0.0.1", 8081, Collections.singletonMap("__meta_key", "value2"))); + mockMvc = MockMvcBuilders.standaloneSetup(prometheusController).build(); + } + + private Instance prepareInstance(String clusterName, String ip, int port, Map metadata) { Instance instance = new Instance(); instance.setClusterName("A"); instance.setIp("127.0.0.1"); instance.setPort(8080); - testInstanceList.add(instance); - mockMvc = MockMvcBuilders.standaloneSetup(prometheusController).build(); + instance.setMetadata(metadata); + return instance; } @After From 6f92dd3f45f2af646acfc82b180342948f9b23cd Mon Sep 17 00:00:00 2001 From: Kurok1 Date: Fri, 31 May 2024 15:41:50 +0800 Subject: [PATCH 2/2] fix style --- .../alibaba/nacos/prometheus/utils/PrometheusUtils.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 887167568e8..61098c02d1f 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 @@ -48,23 +48,22 @@ public static void assembleArrayNodes(Set targetSet, ArrayNode arrayNo } /** - * assemble instance to json node, and export metadata to label + * assemble instance to json node, and export metadata to label. * * @param clusterName the cluster name * @param instance instance info */ private static ObjectNode assembleInstanceToArrayNode(String clusterName, Instance instance) { - ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode(); - ArrayNode targetsNode = JacksonUtils.createEmptyArrayNode(); - ObjectNode labelNode = JacksonUtils.createEmptyJsonNode(); + ArrayNode targetsNode = JacksonUtils.createEmptyArrayNode(); targetsNode.add(instance.getIp() + ":" + instance.getPort()); + ObjectNode labelNode = JacksonUtils.createEmptyJsonNode(); //mark cluster name labelNode.put("__meta_clusterName", clusterName); //export metadata Map metadata = instance.getMetadata(); metadata.forEach(labelNode::put); - + ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode(); jsonNode.replace("targets", targetsNode); jsonNode.replace("labels", labelNode); return jsonNode;