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..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 @@ -36,21 +36,36 @@ 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) { + + 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; + } } 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