refactor:refactor stat module. (#879)

pull/884/head
Haotian Zhang 2 years ago committed by GitHub
parent 6144b0e923
commit eb6ec18e3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,3 +11,4 @@
- [docs:support auto snapshot release in GitHub Action.](https://github.com/Tencent/spring-cloud-tencent/pull/870) - [docs:support auto snapshot release in GitHub Action.](https://github.com/Tencent/spring-cloud-tencent/pull/870)
- [feature:add User-Agent:polaris for healthyCheck api.](https://github.com/Tencent/spring-cloud-tencent/pull/873) - [feature:add User-Agent:polaris for healthyCheck api.](https://github.com/Tencent/spring-cloud-tencent/pull/873)
- [optimize:optimize ServiceRuleManager.](https://github.com/Tencent/spring-cloud-tencent/pull/878) - [optimize:optimize ServiceRuleManager.](https://github.com/Tencent/spring-cloud-tencent/pull/878)
- [refactor:refactor stat module.](https://github.com/Tencent/spring-cloud-tencent/pull/879)

@ -46,11 +46,6 @@
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId> <artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
</dependency>
<!-- Spring Cloud --> <!-- Spring Cloud -->
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>

@ -73,11 +73,6 @@
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-tencent-gateway-plugin</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -73,7 +73,7 @@
<revision>1.10.0-2020.0.5-SNAPSHOT</revision> <revision>1.10.0-2020.0.5-SNAPSHOT</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.11.0-SNAPSHOT</polaris.version> <polaris.version>1.11.0</polaris.version>
<guava.version>31.0.1-jre</guava.version> <guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version> <logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version> <mocktio.version>4.5.1</mocktio.version>
@ -175,12 +175,6 @@
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
<version>${revision}</version>
</dependency>
<!-- third part framework dependencies --> <!-- third part framework dependencies -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>

@ -28,10 +28,6 @@
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
</dependency>
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>--> <!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>spring-cloud-tencent</artifactId> <artifactId>spring-cloud-tencent</artifactId>
@ -17,7 +17,6 @@
<modules> <modules>
<module>spring-cloud-tencent-featureenv-plugin</module> <module>spring-cloud-tencent-featureenv-plugin</module>
<module>spring-cloud-tencent-gateway-plugin</module> <module>spring-cloud-tencent-gateway-plugin</module>
<module>spring-cloud-tencent-pushgateway-plugin</module>
</modules> </modules>
</project> </project>

@ -1,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>stat-pushgateway</artifactId>
<version>1.10.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -1,42 +0,0 @@
/*
* 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.plugin.pushgateway;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* @author lingxiao.wlx
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@EnableConfigurationProperties(PolarisStatPushGatewayProperties.class)
public class PolarisStatPushGatewayAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public PolarisStatPushGatewayModifier polarisStatPushGatewayModifier(PolarisStatPushGatewayProperties polarisStatPushGatewayProperties,
Environment environment) {
return new PolarisStatPushGatewayModifier(polarisStatPushGatewayProperties, environment);
}
}

@ -1,31 +0,0 @@
/*
* 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.plugin.pushgateway;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author lingxiao.wlx
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("spring.cloud.polaris.enabled")
@Import(PolarisStatPushGatewayAutoConfiguration.class)
public class PolarisStatPushGatewayBootstrapConfiguration {
}

@ -1,70 +0,0 @@
/*
* 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.plugin.pushgateway;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.stat.pushgateway.handler.PrometheusPushHandlerConfig;
import org.springframework.core.env.Environment;
/**
* @author lingxiao.wlx
*/
public class PolarisStatPushGatewayModifier implements PolarisConfigModifier {
private static final String POLARIS_STAT_ENABLED = "spring.cloud.polaris.stat.enabled";
private static final String PROMETHEUS_PUSH_GATEWAY_PLUGIN_NAME = "prometheus-pushgateway";
private final PolarisStatPushGatewayProperties polarisStatPushGatewayProperties;
private final Environment environment;
public PolarisStatPushGatewayModifier(PolarisStatPushGatewayProperties polarisStatPushGatewayProperties,
Environment environment) {
this.polarisStatPushGatewayProperties = polarisStatPushGatewayProperties;
this.environment = environment;
}
@Override
public void modify(ConfigurationImpl configuration) {
// Turn on stat reporter configuration.
Boolean statEnabled = environment.getProperty(POLARIS_STAT_ENABLED, Boolean.class, false);
if (!statEnabled) {
configuration.getGlobal().getStatReporter().setEnable(polarisStatPushGatewayProperties.isEnabled());
}
// Set prometheus plugin.
if (polarisStatPushGatewayProperties.isEnabled()) {
PrometheusPushHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
.getPluginConfig(PROMETHEUS_PUSH_GATEWAY_PLUGIN_NAME, PrometheusPushHandlerConfig.class);
prometheusHandlerConfig.setPushgatewayAddress(polarisStatPushGatewayProperties.getAddress());
prometheusHandlerConfig.setPushInterval(polarisStatPushGatewayProperties.getPushInterval());
configuration.getGlobal().getStatReporter()
.setPluginConfig(PROMETHEUS_PUSH_GATEWAY_PLUGIN_NAME, prometheusHandlerConfig);
}
}
@Override
public int getOrder() {
// run after prometheus stat reporter.
return ContextConstant.ModifierOrder.STAT_REPORTER_ORDER + 1;
}
}

@ -1,69 +0,0 @@
/*
* 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.plugin.pushgateway;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* The properties for stat pushGateway reporter.
*
* @author lingxiao.wlx
*/
@ConfigurationProperties("spring.cloud.polaris.stat.pushgateway")
public class PolarisStatPushGatewayProperties {
/**
* If state pushGateway reporter enabled.
*/
private boolean enabled = false;
/**
* PushGateway address.
*/
private String address;
/**
* Push metrics interval.
* unit: milliseconds default 30s.
*/
private Long pushInterval = 30 * 1000L;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Long getPushInterval() {
return pushInterval;
}
public void setPushInterval(Long pushInterval) {
this.pushInterval = pushInterval;
}
}

@ -1,23 +0,0 @@
{
"properties": [
{
"name": "spring.cloud.polaris.stat.pushgateway.address",
"type": "java.lang.String",
"description": "PushGateway address.",
"sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties"
},
{
"name": "spring.cloud.polaris.stat.pushgateway.enabled",
"type": "java.lang.Boolean",
"description": "If state pushGateway reporter enabled.",
"sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties",
"defaultValue": false
},
{
"name": "spring.cloud.polaris.stat.pushgateway.push-interval",
"type": "java.lang.Long",
"description": "Push metrics interval. unit: milliseconds default 30s.",
"sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties"
}
]
}

@ -1,4 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayBootstrapConfiguration

@ -1,49 +0,0 @@
/*
* 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.plugin.pushgateway;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisStatPushGatewayAutoConfiguration}.
*
* @author lingxiao.wlx
*/
public class PolarisStatPushGatewayAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class,
PolarisStatPushGatewayAutoConfiguration.class));
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisStatPushGatewayProperties.class);
assertThat(context).hasSingleBean(PolarisStatPushGatewayModifier.class);
assertThat(context).hasSingleBean(PolarisStatPushGatewayProperties.class);
});
}
}

@ -1,47 +0,0 @@
/*
* 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.plugin.pushgateway;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisStatPushGatewayBootstrapConfiguration}.
*
* @author lingxiao.wlx
*/
public class PolarisStatPushGatewayBootstrapConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisStatPushGatewayBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true");
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisStatPushGatewayProperties.class);
assertThat(context).hasSingleBean(PolarisStatPushGatewayModifier.class);
assertThat(context).hasSingleBean(PolarisStatPushGatewayProperties.class);
assertThat(context).hasSingleBean(PolarisStatPushGatewayBootstrapConfiguration.class);
});
}
}

@ -1,51 +0,0 @@
/*
* 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.plugin.pushgateway;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisStatPushGatewayProperties}.
*
* @author lingxiao.wlx
*/
public class PolarisStatPushGatewayPropertiesTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisStatPushGatewayBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.address=127.0.0.1:9091")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000");
@Test
public void polarisStatPushGatewayPropertiesTest() {
contextRunner.run(context -> {
PolarisStatPushGatewayProperties polarisStatPushGatewayProperties = context.getBean(PolarisStatPushGatewayProperties.class);
assertThat(polarisStatPushGatewayProperties.isEnabled()).isTrue();
assertThat(polarisStatPushGatewayProperties.getAddress()).isEqualTo("127.0.0.1:9091");
assertThat(polarisStatPushGatewayProperties.getPushInterval().toString()).isEqualTo("1000");
});
}
}

@ -17,6 +17,7 @@
package com.tencent.cloud.rpc.enhancement.stat.config; package com.tencent.cloud.rpc.enhancement.stat.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
@ -47,6 +48,25 @@ public class PolarisStatProperties {
*/ */
private String path = "/metrics"; private String path = "/metrics";
/**
* If state pushGateway reporter enabled.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.enabled:#{false}}")
private boolean pushGatewayEnabled = false;
/**
* PushGateway address.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.address:}")
private String pushGatewayAddress;
/**
* Push metrics interval.
* unit: milliseconds default 30s.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.push-interval:#{30000}}")
private Long pushGatewayPushInterval = 30 * 1000L;
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
@ -78,4 +98,28 @@ public class PolarisStatProperties {
public void setPath(String path) { public void setPath(String path) {
this.path = path; this.path = path;
} }
public boolean isPushGatewayEnabled() {
return pushGatewayEnabled;
}
public void setPushGatewayEnabled(boolean pushGatewayEnabled) {
this.pushGatewayEnabled = pushGatewayEnabled;
}
public String getPushGatewayAddress() {
return pushGatewayAddress;
}
public void setPushGatewayAddress(String pushGatewayAddress) {
this.pushGatewayAddress = pushGatewayAddress;
}
public Long getPushGatewayPushInterval() {
return pushGatewayPushInterval;
}
public void setPushGatewayPushInterval(Long pushGatewayPushInterval) {
this.pushGatewayPushInterval = pushGatewayPushInterval;
}
} }

@ -47,28 +47,35 @@ public class StatConfigModifier implements PolarisConfigModifier {
public void modify(ConfigurationImpl configuration) { public void modify(ConfigurationImpl configuration) {
// Turn on stat reporter configuration. // Turn on stat reporter configuration.
configuration.getGlobal().getStatReporter().setEnable(polarisStatProperties.isEnabled()); configuration.getGlobal().getStatReporter().setEnable(polarisStatProperties.isEnabled());
PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
// Set prometheus plugin. // Set prometheus plugin.
if (polarisStatProperties.isEnabled()) { if (polarisStatProperties.isEnabled()) {
PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class); if (polarisStatProperties.isPushGatewayEnabled()) {
if (!StringUtils.hasText(polarisStatProperties.getHost())) { // push gateway
polarisStatProperties.setHost(environment.getProperty("spring.cloud.client.ip-address")); prometheusHandlerConfig.setType("push");
prometheusHandlerConfig.setAddress(polarisStatProperties.getPushGatewayAddress());
prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval());
} }
prometheusHandlerConfig.setHost(polarisStatProperties.getHost()); else {
prometheusHandlerConfig.setPort(polarisStatProperties.getPort()); // pull metrics
prometheusHandlerConfig.setPath(polarisStatProperties.getPath()); prometheusHandlerConfig.setType("pull");
configuration.getGlobal().getStatReporter() if (!StringUtils.hasText(polarisStatProperties.getHost())) {
.setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig); polarisStatProperties.setHost(environment.getProperty("spring.cloud.client.ip-address"));
}
prometheusHandlerConfig.setHost(polarisStatProperties.getHost());
prometheusHandlerConfig.setPort(polarisStatProperties.getPort());
prometheusHandlerConfig.setPath(polarisStatProperties.getPath());
}
} }
else { else {
PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
// Set port to -1 to disable stat plugin. // Set port to -1 to disable stat plugin.
prometheusHandlerConfig.setPort(-1); prometheusHandlerConfig.setPort(-1);
configuration.getGlobal().getStatReporter()
.setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
} }
configuration.getGlobal().getStatReporter()
.setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
} }
@Override @Override

@ -52,6 +52,25 @@
"type": "java.lang.String", "type": "java.lang.String",
"defaultValue": "/metrics", "defaultValue": "/metrics",
"description": "Path for prometheus to pull." "description": "Path for prometheus to pull."
},
{
"name": "spring.cloud.polaris.stat.pushgateway.address",
"type": "java.lang.String",
"description": "PushGateway address.",
"sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties"
},
{
"name": "spring.cloud.polaris.stat.pushgateway.enabled",
"type": "java.lang.Boolean",
"description": "If state pushGateway reporter enabled.",
"sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties",
"defaultValue": false
},
{
"name": "spring.cloud.polaris.stat.pushgateway.push-interval",
"type": "java.lang.Long",
"description": "Push metrics interval. unit: milliseconds default 30s.",
"sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties"
} }
] ]
} }

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

@ -1,55 +0,0 @@
/*
* 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.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisStatProperties}.
*
* @author Haotian Zhang
*/
@ExtendWith(SpringExtension.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 {
}
}

@ -18,13 +18,9 @@
package com.tencent.cloud.rpc.enhancement.stat.config; package com.tencent.cloud.rpc.enhancement.stat.config;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -33,25 +29,49 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = PolarisStatPropertiesTest.TestApplication.class)
@ActiveProfiles("test")
public class PolarisStatPropertiesTest { public class PolarisStatPropertiesTest {
@Autowired private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
private PolarisStatProperties polarisStatProperties; .withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.host=127.0.0.1")
.withPropertyValues("spring.cloud.polaris.stat.port=20000")
.withPropertyValues("spring.cloud.polaris.stat.path=/xxx")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.address=127.0.0.1:9091")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000");
@Test @Test
public void testDefaultInitialization() { public void testDefaultInitialization() {
assertThat(polarisStatProperties).isNotNull(); contextRunner.run(context -> {
assertThat(polarisStatProperties.isEnabled()).isTrue(); PolarisStatProperties polarisStatProperties = context.getBean(PolarisStatProperties.class);
assertThat(polarisStatProperties.getHost()).isNotBlank();
assertThat(polarisStatProperties.getPort()).isEqualTo(20000); assertThat(polarisStatProperties).isNotNull();
assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx"); assertThat(polarisStatProperties.isEnabled()).isTrue();
assertThat(polarisStatProperties.getHost()).isNotBlank();
assertThat(polarisStatProperties.getPort()).isEqualTo(20000);
assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx");
assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue();
assertThat(polarisStatProperties.getPushGatewayAddress()).isEqualTo("127.0.0.1:9091");
assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
});
} }
@SpringBootApplication @Test
protected static class TestApplication { void testGetAndSet() {
PolarisStatProperties polarisStatProperties = new PolarisStatProperties();
// PushGatewayEnabled
polarisStatProperties.setPushGatewayEnabled(true);
assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue();
// PushGatewayAddress
polarisStatProperties.setPushGatewayAddress("127.0.0.1:9091");
assertThat(polarisStatProperties.getPushGatewayAddress()).isEqualTo("127.0.0.1:9091");
// PushGatewayPushInterval
polarisStatProperties.setPushGatewayPushInterval(1000L);
assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
} }
} }

@ -0,0 +1,98 @@
/*
* 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 com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.plugins.stat.prometheus.handler.PrometheusHandlerConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static com.tencent.polaris.api.config.global.StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link StatConfigModifier}.
*
* @author Haotian Zhang
*/
public class StatConfigModifierTest {
private final ApplicationContextRunner pullContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.host=127.0.0.1")
.withPropertyValues("spring.cloud.polaris.stat.port=20000")
.withPropertyValues("spring.cloud.polaris.stat.path=/xxx");
private final ApplicationContextRunner pushContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.address=127.0.0.1:9091")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000");
private final ApplicationContextRunner disabledContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=false");
@Test
void testPull() {
pullContextRunner.run(context -> {
SDKContext sdkContext = context.getBean(SDKContext.class);
PrometheusHandlerConfig prometheusHandlerConfig = sdkContext.getConfig().getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
assertThat(prometheusHandlerConfig.getType()).isEqualTo("pull");
assertThat(prometheusHandlerConfig.getHost()).isEqualTo("127.0.0.1");
assertThat(prometheusHandlerConfig.getPort()).isEqualTo(20000);
assertThat(prometheusHandlerConfig.getPath()).isEqualTo("/xxx");
});
}
@Test
void testPush() {
pushContextRunner.run(context -> {
SDKContext sdkContext = context.getBean(SDKContext.class);
PrometheusHandlerConfig prometheusHandlerConfig = sdkContext.getConfig().getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
assertThat(prometheusHandlerConfig.getType()).isEqualTo("push");
assertThat(prometheusHandlerConfig.getAddress()).isEqualTo("127.0.0.1:9091");
assertThat(prometheusHandlerConfig.getPushInterval()).isEqualTo(1000);
});
}
@Test
void testDisabled() {
disabledContextRunner.run(context -> {
SDKContext sdkContext = context.getBean(SDKContext.class);
PrometheusHandlerConfig prometheusHandlerConfig = sdkContext.getConfig().getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
assertThat(prometheusHandlerConfig.getPort()).isEqualTo(-1);
});
}
@SpringBootApplication
protected static class TestApplication {
}
}

@ -1,8 +0,0 @@
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,3 +1,5 @@
spring.cloud.polaris.stat.enabled=true spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error=false
spring.cloud.polaris.stat.port=20000 spring.cloud.tencent.rpc-enhancement.reporter.series[0]=CLIENT_ERROR
spring.cloud.polaris.stat.path=/xxx 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