From d5b1ae68d57cfb108383fcca5f5b0655c89c3248 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 27 Feb 2023 17:22:19 +0800 Subject: [PATCH] feat:enable stat reporting as default. (#864) --- CHANGELOG.md | 1 + .../registry/PolarisServiceRegistry.java | 26 ++++++++- ...larisServiceRegistryAutoConfiguration.java | 6 +- .../stat/config/PolarisStatProperties.java | 6 +- ...itional-spring-configuration-metadata.json | 6 +- .../RpcEnhancementReporterPropertiesTest.java | 2 +- .../ExceptionPolarisReporterTest.java | 18 ++++-- .../reporter/SuccessPolarisReporterTest.java | 17 ++++-- .../PolarisStatPropertiesDisableTest.java | 55 +++++++++++++++++++ .../resources/application-disable.properties | 8 +++ .../resources/application-test.properties | 5 -- 11 files changed, 127 insertions(+), 23 deletions(-) create mode 100644 spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java create mode 100644 spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties diff --git a/CHANGELOG.md b/CHANGELOG.md index ed7c4724a..8bdae777d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,4 @@ - [refactor:move loadbalancer to discovery module.](https://github.com/Tencent/spring-cloud-tencent/pull/845) - [feature:add PolarisRateLimiterLimitedFallback spi. ](https://github.com/Tencent/spring-cloud-tencent/pull/858) - [fix:fix the error capture of rate limit exception.](https://github.com/Tencent/spring-cloud-tencent/pull/861) +- [feat:enable stat reporting as default.](https://github.com/Tencent/spring-cloud-tencent/pull/864) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 17ea5307d..10c29d0b9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.registry; +import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -25,8 +26,12 @@ import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.util.OkHttpUtil; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties; +import com.tencent.polaris.api.config.global.StatReporterConfig; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.exception.PolarisException; +import com.tencent.polaris.api.plugin.common.PluginTypes; +import com.tencent.polaris.api.plugin.stat.StatReporter; import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.rpc.InstanceDeregisterRequest; import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest; @@ -61,9 +66,11 @@ public class PolarisServiceRegistry implements ServiceRegistry ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class))) - .thenReturn(null); + .thenReturn(mock(ServiceCallResult.class)); } @AfterClass @@ -83,10 +87,16 @@ public class ExceptionPolarisReporterTest { // mock response Response response = mock(Response.class); - EnhancedFeignContext context = new EnhancedFeignContext(); - context.setRequest(request); - context.setResponse(response); + EnhancedFeignContext context = mock(EnhancedFeignContext.class); + doReturn(request).when(context).getRequest(); + doReturn(response).when(context).getResponse(); + // test not report + exceptionPolarisReporter.run(context); + verify(context, times(0)).getRequest(); + // test do report + doReturn(true).when(reporterProperties).isEnabled(); exceptionPolarisReporter.run(context); + verify(context, times(1)).getRequest(); } @Test diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporterTest.java index 56bd11817..edf9ac0cb 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporterTest.java @@ -22,6 +22,7 @@ import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext; import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.RetStatus; +import com.tencent.polaris.api.rpc.ServiceCallResult; import feign.Request; import feign.Response; import org.junit.AfterClass; @@ -38,6 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; /** * Test for {@link SuccessPolarisReporter}. @@ -59,7 +62,7 @@ public class SuccessPolarisReporterTest { public static void beforeClass() { mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class); mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class))) - .thenReturn(null); + .thenReturn(mock(ServiceCallResult.class)); } @AfterClass @@ -85,10 +88,16 @@ public class SuccessPolarisReporterTest { Response response = mock(Response.class); doReturn(502).when(response).status(); - EnhancedFeignContext context = new EnhancedFeignContext(); - context.setRequest(request); - context.setResponse(response); + EnhancedFeignContext context = mock(EnhancedFeignContext.class); + doReturn(request).when(context).getRequest(); + doReturn(response).when(context).getResponse(); + // test not report + successPolarisReporter.run(context); + verify(context, times(0)).getRequest(); + // test do report + doReturn(true).when(reporterProperties).isEnabled(); successPolarisReporter.run(context); + verify(context, times(1)).getRequest(); } @Test diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java new file mode 100644 index 000000000..c333cc439 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java @@ -0,0 +1,55 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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.tencent.cloud.rpc.enhancement.stat.config; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link PolarisStatProperties}. + * + * @author Haotian Zhang + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PolarisStatPropertiesDisableTest.TestApplication.class) +@ActiveProfiles("disable") +public class PolarisStatPropertiesDisableTest { + + @Autowired + private PolarisStatProperties polarisStatProperties; + + @Test + public void testDefaultInitialization() { + assertThat(polarisStatProperties).isNotNull(); + assertThat(polarisStatProperties.isEnabled()).isFalse(); + assertThat(polarisStatProperties.getHost()).isBlank(); + } + + @SpringBootApplication + protected static class TestApplication { + + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties new file mode 100644 index 000000000..a6af090d7 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties @@ -0,0 +1,8 @@ +spring.cloud.polaris.stat.enabled=false +spring.cloud.polaris.stat.port=20000 +spring.cloud.polaris.stat.path=/xxx +spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error=false +spring.cloud.tencent.rpc-enhancement.reporter.series[0]=CLIENT_ERROR +spring.cloud.tencent.rpc-enhancement.reporter.series[1]=SERVER_ERROR +spring.cloud.tencent.rpc-enhancement.reporter.statuses[0]=MULTIPLE_CHOICES +spring.cloud.tencent.rpc-enhancement.reporter.statuses[1]=MOVED_PERMANENTLY diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties index 34e566e79..96d94d0d0 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties +++ b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties @@ -1,8 +1,3 @@ spring.cloud.polaris.stat.enabled=true spring.cloud.polaris.stat.port=20000 spring.cloud.polaris.stat.path=/xxx -spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error=false -spring.cloud.tencent.rpc-enhancement.reporter.series[0]=CLIENT_ERROR -spring.cloud.tencent.rpc-enhancement.reporter.series[1]=SERVER_ERROR -spring.cloud.tencent.rpc-enhancement.reporter.statuses[0]=MULTIPLE_CHOICES -spring.cloud.tencent.rpc-enhancement.reporter.statuses[1]=MOVED_PERMANENTLY