feat:enable stat reporting as default. (#863)

* feat:enable stat reporting as default.

* feat:enable stat reporting as default.
pull/866/head
Haotian Zhang 2 years ago committed by GitHub
parent d3c481038b
commit b524d1c8e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,3 +7,4 @@
- [feat:update spring framework version of 2022 branch.](https://github.com/Tencent/spring-cloud-tencent/pull/851)
- [feature:add PolarisRateLimiterLimitedFallback spi.](https://github.com/Tencent/spring-cloud-tencent/pull/857)
- [fix:fix the error capture of rate limit exception.](https://github.com/Tencent/spring-cloud-tencent/pull/860)
- [feat:enable stat reporting as default.](https://github.com/Tencent/spring-cloud-tencent/pull/863)

@ -94,7 +94,7 @@
<spring.framework.version>6.0.5</spring.framework.version>
<!-- Spring Boot -->
<spring.boot.version>3.0.2</spring.boot.version>
<spring.boot.version>3.0.3</spring.boot.version>
<!-- Spring Cloud -->
<spring.cloud.version>2022.0.1</spring.cloud.version>

@ -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<PolarisRegistrati
private final ScheduledExecutorService heartbeatExecutor;
private final PolarisStatProperties polarisStatProperties;
public PolarisServiceRegistry(PolarisDiscoveryProperties polarisDiscoveryProperties,
PolarisDiscoveryHandler polarisDiscoveryHandler,
StaticMetadataManager staticMetadataManager) {
StaticMetadataManager staticMetadataManager, PolarisStatProperties polarisStatProperties) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
this.staticMetadataManager = staticMetadataManager;
@ -75,6 +82,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
else {
this.heartbeatExecutor = null;
}
this.polarisStatProperties = polarisStatProperties;
}
@Override
@ -118,6 +127,21 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(),
registration.getServiceId(), registration.getHost(), registration.getPort(),
staticMetadataManager.getMergedStaticMetadata());
if (Objects.nonNull(polarisStatProperties) && polarisStatProperties.isEnabled()) {
try {
StatReporter statReporter = (StatReporter) polarisDiscoveryHandler.getSdkContext().getPlugins()
.getPlugin(PluginTypes.STAT_REPORTER.getBaseType(), StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS);
if (Objects.nonNull(statReporter)) {
LOGGER.info("Stat server started on port: " + statReporter.metaInfo().getPort() + " (http)");
}
else {
LOGGER.warn("Plugin StatReporter not found");
}
}
catch (Exception e) {
LOGGER.warn("Stat server started error, ", e);
}
}
}
catch (Exception e) {
LOGGER.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e);

@ -24,6 +24,7 @@ import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties;
import com.tencent.polaris.client.api.SDKContext;
import org.springframework.beans.factory.annotation.Autowired;
@ -55,8 +56,9 @@ public class PolarisServiceRegistryAutoConfiguration {
@Bean
public PolarisServiceRegistry polarisServiceRegistry(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryHandler polarisDiscoveryHandler,
StaticMetadataManager staticMetadataManager) {
return new PolarisServiceRegistry(polarisDiscoveryProperties, polarisDiscoveryHandler, staticMetadataManager);
StaticMetadataManager staticMetadataManager, PolarisStatProperties polarisStatProperties) {
return new PolarisServiceRegistry(polarisDiscoveryProperties, polarisDiscoveryHandler,
staticMetadataManager, polarisStatProperties);
}
@Bean

@ -30,7 +30,7 @@ public class PolarisStatProperties {
/**
* If state reporter enabled.
*/
private boolean enabled = false;
private boolean enabled = true;
/**
* Local host for prometheus to pull.
@ -38,9 +38,9 @@ public class PolarisStatProperties {
private String host;
/**
* Port for prometheus to pull.
* Port for prometheus to pull. 0 for random from 20000 to 65535.
*/
private int port = 28080;
private int port = 0;
/**
* Path for prometheus to pull.

@ -33,7 +33,7 @@
{
"name": "spring.cloud.polaris.stat.enabled",
"type": "java.lang.Boolean",
"defaultValue": false,
"defaultValue": true,
"description": "Enable polaris stat reporter or not."
},
{
@ -44,8 +44,8 @@
{
"name": "spring.cloud.polaris.stat.port",
"type": "java.lang.Integer",
"defaultValue": "28080",
"description": "Port for prometheus to pull."
"defaultValue": "0",
"description": "Port for prometheus to pull. 0 for random from 20000 to 65535."
},
{
"name": "spring.cloud.polaris.stat.path",

@ -39,7 +39,7 @@ import static org.springframework.http.HttpStatus.Series.SERVER_ERROR;
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RpcEnhancementReporterPropertiesTest.TestApplication.class)
@ActiveProfiles("test")
@ActiveProfiles("disable")
public class RpcEnhancementReporterPropertiesTest {
@Autowired

@ -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;
@ -36,7 +37,10 @@ import org.mockito.junit.MockitoJUnitRunner;
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 ExceptionPolarisReporter}.
@ -58,7 +62,7 @@ public class ExceptionPolarisReporterTest {
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
@ -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

@ -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

@ -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 {
}
}

@ -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

@ -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

Loading…
Cancel
Save