[ISSUE #10662]Prometheus-sd add namespace and service api (#10663)

* add apis

* add tests

* fix checkstyle

* param namespace to namespaceId

* namespace to namespaceId

* fix test case

* fix test case
This commit is contained in:
Joey777210 2023-07-17 11:20:07 +08:00 committed by GitHub
parent 318892d08d
commit f986916acf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 154 additions and 19 deletions

View File

@ -24,5 +24,9 @@ package com.alibaba.nacos.prometheus.api;
public class ApiConstants {
public static final String PROMETHEUS_CONTROLLER_PATH = "/prometheus";
public static final String PROMETHEUS_CONTROLLER_NAMESPACE_PATH = "/prometheus/namespaceId/{namespaceId}";
public static final String PROMETHEUS_CONTROLLER_SERVICE_PATH = "/prometheus/namespaceId/{namespaceId}/service/{service}";
}

View File

@ -21,7 +21,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import static com.alibaba.nacos.prometheus.api.ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH;
import static com.alibaba.nacos.prometheus.api.ApiConstants.PROMETHEUS_CONTROLLER_PATH;
import static com.alibaba.nacos.prometheus.api.ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH;
/**
@ -36,5 +38,7 @@ public class PrometheusSecurityConfiguration extends WebSecurityConfigurerAdapte
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().mvcMatchers(PROMETHEUS_CONTROLLER_PATH);
web.ignoring().mvcMatchers(PROMETHEUS_CONTROLLER_NAMESPACE_PATH);
web.ignoring().mvcMatchers(PROMETHEUS_CONTROLLER_SERVICE_PATH);
}
}

View File

@ -23,20 +23,19 @@ import com.alibaba.nacos.naming.core.InstanceOperatorClientImpl;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.prometheus.api.ApiConstants;
import com.alibaba.nacos.prometheus.utils.PrometheusUtils;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.groupingBy;
/**
* Support Prometheus SD Controller.
@ -73,26 +72,73 @@ public class PrometheusController {
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespace,
service.getGroupedServiceName());
for (Instance instance : instances) {
targetSet.add(instance);
}
targetSet.addAll(instances);
}
}
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);
PrometheusUtils.assembleArrayNodes(targetSet, arrayNode);
return ResponseEntity.ok().body(arrayNode.toString());
}
/**
* Get service instances from designated namespace.
*
* @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());
}
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());
}
/**
* Get service instances from designated namespace and service.
*
* @throws NacosException NacosException.
*/
@GetMapping(value = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH, produces = "application/json; charset=UTF-8")
public ResponseEntity metricNamespaceService(@PathVariable("namespaceId") String namespaceId,
@PathVariable("service") String service) 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());
}
Set<Service> singletons = serviceManager.getSingletons(namespaceId);
for (Service existService : singletons) {
if (!existService.getName().equals(service)) {
continue;
}
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespaceId,
existService.getGroupedServiceName());
targetSet.addAll(instances);
}
PrometheusUtils.assembleArrayNodes(targetSet, arrayNode);
return ResponseEntity.ok().body(arrayNode.toString());
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.prometheus.utils;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.groupingBy;
/**
* prometheus common utils.
*
* @author Joey777210
*/
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);
});
}
}

View File

@ -103,4 +103,29 @@ public class PrometheusControllerTest {
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
}
@Test
public void testMetricNamespace() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(
testInstanceList);
String prometheusNamespacePath = ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH.replace("{namespaceId}",
nameSpace);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespacePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
}
@Test
public void testMetricNamespaceService() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(
testInstanceList);
String promethesuNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}",
nameSpace);
promethesuNamespaceServicePath = promethesuNamespaceServicePath.replace("{service}", service.getName());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(promethesuNamespaceServicePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
}
}