Skip to content

Commit

Permalink
[ISSUE #10662]Prometheus-sd add namespace and service api (#10663)
Browse files Browse the repository at this point in the history
* add apis

* add tests

* fix checkstyle

* param namespace to namespaceId

* namespace to namespaceId

* fix test case

* fix test case
  • Loading branch information
Joey777210 committed Jul 17, 2023
1 parent 318892d commit f986916
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
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}";

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
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;


/**
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
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.
Expand Down Expand Up @@ -73,26 +72,73 @@ public ResponseEntity metric() throws NacosException {
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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,29 @@ public void testMetric() throws Exception {
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());
}

}

0 comments on commit f986916

Please sign in to comment.