[ISSUE #12130] add metadata as labels in prometheus http sd (#12144)

* add metadata as labels in prometheus http sd

* fix style
This commit is contained in:
Kurok1 2024-06-03 10:41:10 +08:00 committed by GitHub
parent d1bd44625f
commit da78aeea0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -39,7 +39,9 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
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 class PrometheusControllerTest {
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