feat:enable stat reporting as default.

pull/1080/head
Haotian Zhang 2 years ago
parent edeeaded92
commit f97a1b2450

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.registry; package com.tencent.cloud.polaris.registry;
import java.util.Objects;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; 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.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.util.OkHttpUtil; 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.core.ProviderAPI;
import com.tencent.polaris.api.exception.PolarisException; 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.pojo.Instance;
import com.tencent.polaris.api.rpc.InstanceDeregisterRequest; import com.tencent.polaris.api.rpc.InstanceDeregisterRequest;
import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest; import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest;
@ -61,9 +66,11 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
private final ScheduledExecutorService heartbeatExecutor; private final ScheduledExecutorService heartbeatExecutor;
private final PolarisStatProperties polarisStatProperties;
public PolarisServiceRegistry(PolarisDiscoveryProperties polarisDiscoveryProperties, public PolarisServiceRegistry(PolarisDiscoveryProperties polarisDiscoveryProperties,
PolarisDiscoveryHandler polarisDiscoveryHandler, PolarisDiscoveryHandler polarisDiscoveryHandler,
StaticMetadataManager staticMetadataManager) { StaticMetadataManager staticMetadataManager, PolarisStatProperties polarisStatProperties) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.polarisDiscoveryHandler = polarisDiscoveryHandler; this.polarisDiscoveryHandler = polarisDiscoveryHandler;
this.staticMetadataManager = staticMetadataManager; this.staticMetadataManager = staticMetadataManager;
@ -75,6 +82,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
else { else {
this.heartbeatExecutor = null; this.heartbeatExecutor = null;
} }
this.polarisStatProperties = polarisStatProperties;
} }
@Override @Override
@ -117,6 +126,21 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(), LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(),
registration.getServiceId(), registration.getHost(), registration.getPort(), registration.getServiceId(), registration.getHost(), registration.getPort(),
staticMetadataManager.getMergedStaticMetadata()); 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) { catch (Exception e) {
LOGGER.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, 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.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; 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 com.tencent.polaris.client.api.SDKContext;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -56,8 +57,9 @@ public class PolarisServiceRegistryAutoConfiguration {
@Bean @Bean
public PolarisServiceRegistry polarisServiceRegistry( public PolarisServiceRegistry polarisServiceRegistry(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryHandler polarisDiscoveryHandler, PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryHandler polarisDiscoveryHandler,
StaticMetadataManager staticMetadataManager) { StaticMetadataManager staticMetadataManager, PolarisStatProperties polarisStatProperties) {
return new PolarisServiceRegistry(polarisDiscoveryProperties, polarisDiscoveryHandler, staticMetadataManager); return new PolarisServiceRegistry(polarisDiscoveryProperties, polarisDiscoveryHandler,
staticMetadataManager, polarisStatProperties);
} }
@Bean @Bean

@ -30,7 +30,7 @@ public class PolarisStatProperties {
/** /**
* If state reporter enabled. * If state reporter enabled.
*/ */
private boolean enabled = false; private boolean enabled = true;
/** /**
* Local host for prometheus to pull. * Local host for prometheus to pull.
@ -38,9 +38,9 @@ public class PolarisStatProperties {
private String host; 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. * Path for prometheus to pull.

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

@ -39,7 +39,7 @@ import static org.springframework.http.HttpStatus.Series.SERVER_ERROR;
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = RpcEnhancementReporterPropertiesTest.TestApplication.class) @SpringBootTest(classes = RpcEnhancementReporterPropertiesTest.TestApplication.class)
@ActiveProfiles("test") @ActiveProfiles("disable")
public class RpcEnhancementReporterPropertiesTest { public class RpcEnhancementReporterPropertiesTest {
@Autowired @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.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.rpc.ServiceCallResult;
import feign.Request; import feign.Request;
import feign.Response; import feign.Response;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -36,7 +37,10 @@ import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/** /**
* Test for {@link ExceptionPolarisReporter}. * Test for {@link ExceptionPolarisReporter}.
@ -58,7 +62,7 @@ public class ExceptionPolarisReporterTest {
public static void beforeClass() { public static void beforeClass() {
mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class); mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class);
mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class))) mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class)))
.thenReturn(null); .thenReturn(mock(ServiceCallResult.class));
} }
@AfterClass @AfterClass
@ -83,10 +87,16 @@ public class ExceptionPolarisReporterTest {
// mock response // mock response
Response response = mock(Response.class); Response response = mock(Response.class);
EnhancedFeignContext context = new EnhancedFeignContext(); EnhancedFeignContext context = mock(EnhancedFeignContext.class);
context.setRequest(request); doReturn(request).when(context).getRequest();
context.setResponse(response); 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); exceptionPolarisReporter.run(context);
verify(context, times(1)).getRequest();
} }
@Test @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.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.rpc.ServiceCallResult;
import feign.Request; import feign.Request;
import feign.Response; import feign.Response;
import org.junit.AfterClass; 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.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/** /**
* Test for {@link SuccessPolarisReporter}. * Test for {@link SuccessPolarisReporter}.
@ -59,7 +62,7 @@ public class SuccessPolarisReporterTest {
public static void beforeClass() { public static void beforeClass() {
mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class); mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class);
mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class))) mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class)))
.thenReturn(null); .thenReturn(mock(ServiceCallResult.class));
} }
@AfterClass @AfterClass
@ -85,10 +88,16 @@ public class SuccessPolarisReporterTest {
Response response = mock(Response.class); Response response = mock(Response.class);
doReturn(502).when(response).status(); doReturn(502).when(response).status();
EnhancedFeignContext context = new EnhancedFeignContext(); EnhancedFeignContext context = mock(EnhancedFeignContext.class);
context.setRequest(request); doReturn(request).when(context).getRequest();
context.setResponse(response); 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); successPolarisReporter.run(context);
verify(context, times(1)).getRequest();
} }
@Test @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.enabled=true
spring.cloud.polaris.stat.port=20000 spring.cloud.polaris.stat.port=20000
spring.cloud.polaris.stat.path=/xxx 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