Refactor Prometheus Module (#10827)

* Refactor Prometheus Module

* Complete Test Case

* format

* format
This commit is contained in:
ZhangShenao 2023-07-25 09:33:14 +08:00 committed by GitHub
parent 8adca685f6
commit 95df397616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 31 deletions

View File

@ -35,6 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
/**
@ -61,7 +62,7 @@ public class PrometheusController {
* @throws NacosException NacosException.
*/
@GetMapping(value = ApiConstants.PROMETHEUS_CONTROLLER_PATH, produces = "application/json; charset=UTF-8")
public ResponseEntity metric() throws NacosException {
public ResponseEntity<String> metric() throws NacosException {
ArrayNode arrayNode = JacksonUtils.createEmptyArrayNode();
Set<Instance> targetSet = new HashSet<>();
Set<String> allNamespaces = serviceManager.getAllNamespaces();
@ -88,25 +89,10 @@ public class PrometheusController {
* @throws NacosException NacosException.
*/
@GetMapping(value = ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH, produces = "application/json; charset=UTF-8")
public ResponseEntity metricNamespace(@PathVariable("namespaceId") String namespaceId) throws NacosException {
ArrayNode arrayNode = JacksonUtils.createEmptyArrayNode();
Set<Instance> targetSet = new HashSet<>();
Set<String> allNamespaces = serviceManager.getAllNamespaces();
if (!allNamespaces.contains(namespaceId)) {
return ResponseEntity.ok().body(arrayNode.toString());
}
public ResponseEntity<String> metricNamespace(@PathVariable("namespaceId") String namespaceId)
throws NacosException {
ArrayNode arrayNode = getServiceArrayNode(namespaceId, s -> true);
Set<Service> singletons = serviceManager.getSingletons(namespaceId);
for (Service service : singletons) {
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespaceId,
service.getGroupedServiceName());
targetSet.addAll(instances);
}
PrometheusUtils.assembleArrayNodes(targetSet, arrayNode);
return ResponseEntity.ok().body(arrayNode.toString());
}
@ -116,18 +102,25 @@ public class PrometheusController {
* @throws NacosException NacosException.
*/
@GetMapping(value = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH, produces = "application/json; charset=UTF-8")
public ResponseEntity metricNamespaceService(@PathVariable("namespaceId") String namespaceId,
public ResponseEntity<String> metricNamespaceService(@PathVariable("namespaceId") String namespaceId,
@PathVariable("service") String service) throws NacosException {
ArrayNode arrayNode = getServiceArrayNode(namespaceId, s -> s.getName().equals(service));
return ResponseEntity.ok().body(arrayNode.toString());
}
private ArrayNode getServiceArrayNode(String namespaceId, Predicate<Service> serviceFilter) throws NacosException {
ArrayNode arrayNode = JacksonUtils.createEmptyArrayNode();
Set<Instance> targetSet = new HashSet<>();
Set<String> allNamespaces = serviceManager.getAllNamespaces();
if (!allNamespaces.contains(namespaceId)) {
return ResponseEntity.ok().body(arrayNode.toString());
return arrayNode;
}
Set<Instance> targetSet = new HashSet<>();
Set<Service> singletons = serviceManager.getSingletons(namespaceId);
for (Service existService : singletons) {
if (!existService.getName().equals(service)) {
if (!serviceFilter.test(existService)) {
continue;
}
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespaceId,
@ -139,6 +132,6 @@ public class PrometheusController {
PrometheusUtils.assembleArrayNodes(targetSet, arrayNode);
return ResponseEntity.ok().body(arrayNode.toString());
return arrayNode;
}
}

View File

@ -67,8 +67,6 @@ public class PrometheusControllerTest {
private final String name = "C";
private Instance instance;
private List testInstanceList;
private MockMvc mockMvc;
@ -79,7 +77,7 @@ public class PrometheusControllerTest {
service = Service.newService(nameSpace, group, name);
serviceManager.getSingleton(service);
testInstanceList = new ArrayList<>();
instance = new Instance();
Instance instance = new Instance();
instance.setClusterName("A");
instance.setIp("127.0.0.1");
instance.setPort(8080);
@ -88,7 +86,7 @@ public class PrometheusControllerTest {
}
@After
public void tearDown() throws Exception {
public void tearDown() {
ServiceManager serviceManager = ServiceManager.getInstance();
serviceManager.removeSingleton(service);
}
@ -119,13 +117,25 @@ public class PrometheusControllerTest {
public void testMetricNamespaceService() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(
testInstanceList);
String promethesuNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}",
String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}",
nameSpace);
promethesuNamespaceServicePath = promethesuNamespaceServicePath.replace("{service}", service.getName());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(promethesuNamespaceServicePath);
prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", service.getName());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
}
@Test
public void testEmptyMetricNamespaceService() throws Exception {
String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}",
nameSpace);
prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}",
"D"); //query non-existed service
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
assertEquals(0, JacksonUtils.toObj(response.getContentAsString()).size());
}
}