----初始化项目

This commit is contained in:
2025-09-19 20:49:14 +08:00
parent b345d2828d
commit df7765c400
2867 changed files with 359313 additions and 89 deletions

84
prometheus/pom.xml Normal file
View File

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2021 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-prometheus</artifactId>
<packaging>jar</packaging>
<name>nacos-prometheus ${project.version}</name>
<url>https://nacos.io</url>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-api</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-naming</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.alibaba.nacos.prometheus.PrometheusApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,35 @@
/*
* 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;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* PrometheusApp starter.
*
* @author karsonto
*/
@EnableScheduling
@SpringBootApplication(scanBasePackages = {"com.alibaba.nacos"})
public class PrometheusApp {
public static void main(String[] args) {
SpringApplication.run(PrometheusApp.class, args);
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.api;
/**
* Api Constants.
*
* @author karsonto
*/
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

@ -0,0 +1,45 @@
/*
* 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.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
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;
/**
* prometheus auth configuration, avoid spring security configuration override.
*
* @author vividfish
*/
@Configuration
public class PrometheusSecurityConfiguration {
@Bean
public WebSecurityCustomizer prometheusWebSecurityCustomizer() {
return web -> {
web.ignoring().mvcMatchers(PROMETHEUS_CONTROLLER_PATH);
web.ignoring().mvcMatchers(PROMETHEUS_CONTROLLER_NAMESPACE_PATH);
web.ignoring().mvcMatchers(PROMETHEUS_CONTROLLER_SERVICE_PATH);
};
}
}

View File

@ -0,0 +1,137 @@
/*
* 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.controller;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.common.utils.JacksonUtils;
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 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.Set;
import java.util.function.Predicate;
/**
* Support Prometheus SD Controller.
*
* @author karsonto
*/
@RestController
@ConditionalOnProperty(name = "nacos.prometheus.metrics.enabled", havingValue = "true")
public class PrometheusController {
@Autowired
private InstanceOperatorClientImpl instanceServiceV2;
private final ServiceManager serviceManager;
public PrometheusController() {
this.serviceManager = ServiceManager.getInstance();
}
/**
* Get all service instances.
*
* @throws NacosException NacosException.
*/
@GetMapping(value = ApiConstants.PROMETHEUS_CONTROLLER_PATH, produces = "application/json; charset=UTF-8")
public ResponseEntity<String> metric() throws NacosException {
ArrayNode arrayNode = JacksonUtils.createEmptyArrayNode();
Set<Instance> targetSet = new HashSet<>();
Set<String> allNamespaces = serviceManager.getAllNamespaces();
for (String namespace : allNamespaces) {
Set<Service> singletons = serviceManager.getSingletons(namespace);
for (Service service : singletons) {
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespace,
service.getGroupedServiceName());
targetSet.addAll(instances);
}
}
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<String> metricNamespace(@PathVariable("namespaceId") String namespaceId)
throws NacosException {
ArrayNode arrayNode = getServiceArrayNode(namespaceId, s -> true);
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<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<String> allNamespaces = serviceManager.getAllNamespaces();
if (!allNamespaces.contains(namespaceId)) {
return arrayNode;
}
Set<Instance> targetSet = new HashSet<>();
Set<Service> singletons = serviceManager.getSingletons(namespaceId);
for (Service existService : singletons) {
if (!serviceFilter.test(existService)) {
continue;
}
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespaceId,
existService.getGroupedServiceName());
targetSet.addAll(instances);
}
PrometheusUtils.assembleArrayNodes(targetSet, arrayNode);
return arrayNode;
}
}

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.exception;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.api.model.v2.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Exception Handler for Prometheus API.
*
* @author karsonto
* @date 2023/02/01
*/
@Order(-1)
@ControllerAdvice(basePackages = {"com.alibaba.nacos.prometheus.controller"})
@ResponseBody
public class PrometheusApiExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusApiExceptionHandler.class);
@ExceptionHandler(NacosException.class)
public ResponseEntity<Result<String>> handleNacosException(NacosException e) {
LOGGER.error("got exception. {}", e.getErrMsg());
return ResponseEntity.internalServerError().body(Result.failure(e.getErrMsg()));
}
@ExceptionHandler(NacosRuntimeException.class)
public ResponseEntity<Result<String>> handleNacosRuntimeException(NacosRuntimeException e) {
LOGGER.error("got exception. {}", e.getMessage());
return ResponseEntity.status(e.getErrCode()).body(Result.failure(e.getMessage()));
}
}

View File

@ -0,0 +1,83 @@
/*
* 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.filter;
import com.alibaba.nacos.plugin.auth.constant.Constants;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authorization.AuthenticatedAuthorizationManager;
import org.springframework.security.web.access.ExceptionTranslationFilter;
import org.springframework.security.web.access.intercept.AuthorizationFilter;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import static com.alibaba.nacos.prometheus.api.ApiConstants.PROMETHEUS_CONTROLLER_PATH;
/**
* prometheus auth configuration.
*
* @author vividfish
*/
@Configuration
@ConditionalOnProperty(value = Constants.Auth.NACOS_CORE_AUTH_ENABLED, havingValue = "true")
public class PrometheusAuthFilter {
@Bean
public FilterRegistrationBean<BasicAuthenticationFilter> basicAuthenticationFilter(
AuthenticationManager authenticationManager) {
FilterRegistrationBean<BasicAuthenticationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new BasicAuthenticationFilter(authenticationManager));
registration.addUrlPatterns(PROMETHEUS_CONTROLLER_PATH);
registration.setName("prometheusBasicAuthenticationFilter");
registration.setOrder(2);
return registration;
}
@Bean
public FilterRegistrationBean<AnonymousAuthenticationFilter> anonymousAuthenticationFilter() {
FilterRegistrationBean<AnonymousAuthenticationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new AnonymousAuthenticationFilter("annony"));
registration.addUrlPatterns(PROMETHEUS_CONTROLLER_PATH);
registration.setName("prometheusAnonymousAuthenticationFilter");
registration.setOrder(3);
return registration;
}
@Bean
public FilterRegistrationBean<AuthorizationFilter> authorizationFilter() {
FilterRegistrationBean<AuthorizationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new AuthorizationFilter(new AuthenticatedAuthorizationManager<>()));
registration.addUrlPatterns(PROMETHEUS_CONTROLLER_PATH);
registration.setName("prometheusAuthorizationFilter");
registration.setOrder(4);
return registration;
}
@Bean
public FilterRegistrationBean<ExceptionTranslationFilter> exceptionTranslationFilter() {
FilterRegistrationBean<ExceptionTranslationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new ExceptionTranslationFilter(new Http403ForbiddenEntryPoint()));
registration.addUrlPatterns(PROMETHEUS_CONTROLLER_PATH);
registration.setName("prometheusExceptionTranslationFilter");
registration.setOrder(1);
return registration;
}
}

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

@ -0,0 +1,141 @@
/*
* 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.controller;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
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 org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
/**
* {@link PrometheusController} unit tests.
*
* @author karsonto
* @date 2023-02-01 10:56
*/
@RunWith(MockitoJUnitRunner.class)
public class PrometheusControllerTest {
@InjectMocks
private PrometheusController prometheusController;
@Mock
private InstanceOperatorClientImpl instanceServiceV2;
private Service service;
private final String nameSpace = "A";
private final String group = "B";
private final String name = "C";
private List testInstanceList;
private MockMvc mockMvc;
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException {
ServiceManager serviceManager = ServiceManager.getInstance();
service = Service.newService(nameSpace, group, name);
serviceManager.getSingleton(service);
testInstanceList = new ArrayList<>();
Instance instance = new Instance();
instance.setClusterName("A");
instance.setIp("127.0.0.1");
instance.setPort(8080);
testInstanceList.add(instance);
mockMvc = MockMvcBuilders.standaloneSetup(prometheusController).build();
}
@After
public void tearDown() {
ServiceManager serviceManager = ServiceManager.getInstance();
serviceManager.removeSingleton(service);
}
@Test
public void testMetric() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(
testInstanceList);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(ApiConstants.PROMETHEUS_CONTROLLER_PATH);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
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 prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}",
nameSpace);
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());
}
}

View File

@ -0,0 +1,74 @@
/*
* 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.controller.exception;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.prometheus.controller.PrometheusController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@RunWith(SpringRunner.class)
@WebMvcTest(PrometheusApiExceptionHandlerTest.class)
public class PrometheusApiExceptionHandlerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@MockBean
private PrometheusController prometheusController;
@Before
public void before() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testNacosRunTimeExceptionHandler() throws Exception {
// 设置PrometheusController的行为使其抛出NacosRuntimeException并被PrometheusApiExceptionHandler捕获处理
when(prometheusController.metric())
.thenThrow(new NacosRuntimeException(NacosException.INVALID_PARAM))
.thenThrow(new NacosRuntimeException(NacosException.SERVER_ERROR))
.thenThrow(new NacosRuntimeException(503));
// 执行请求并验证响应码
ResultActions resultActions = mockMvc.perform(get("/prometheus"));
resultActions.andExpect(MockMvcResultMatchers.status().is(NacosException.INVALID_PARAM));
// 执行请求并验证响应码
ResultActions resultActions1 = mockMvc.perform(get("/prometheus"));
resultActions1.andExpect(MockMvcResultMatchers.status().is(NacosException.SERVER_ERROR));
// 执行请求并验证响应码
ResultActions resultActions2 = mockMvc.perform(get("/prometheus"));
resultActions2.andExpect(MockMvcResultMatchers.status().is(503));
}
}