Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading