Skip to content

Commit

Permalink
[ISSUE #12130] add metadata as labels in prometheus http sd (#12144)
Browse files Browse the repository at this point in the history
* add metadata as labels in prometheus http sd

* fix style
  • Loading branch information
Kurok1 authored Jun 3, 2024
1 parent d1bd446 commit da78aee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,36 @@ public class PrometheusUtils {

/**
* Assemble arrayNodes for prometheus sd api.
*
*/
public static void assembleArrayNodes(Set<Instance> targetSet, ArrayNode arrayNode) {
Map<String, List<Instance>> 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<String, String> metadata = instance.getMetadata();
metadata.forEach(labelNode::put);
ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode();
jsonNode.replace("targets", targetsNode);
jsonNode.replace("labels", labelNode);
return jsonNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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
Expand Down

0 comments on commit da78aee

Please sign in to comment.