diff --git a/CHANGELOG.md b/CHANGELOG.md index 6de5501b0..71f459460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,3 +21,4 @@ - [Feature: Add router actuate endpoint](https://github.com/Tencent/spring-cloud-tencent/pull/519) - [Code optimization for rpc-enhancement module](https://github.com/Tencent/spring-cloud-tencent/pull/525) - [Feature: Optimized configuration update](https://github.com/Tencent/spring-cloud-tencent/pull/527) +- [Feature:support pushGateway push metrics](https://github.com/Tencent/spring-cloud-tencent/pull/531) diff --git a/README-zh.md b/README-zh.md index 00fcb0823..327feecbf 100644 --- a/README-zh.md +++ b/README-zh.md @@ -70,7 +70,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 com.tencent.cloud spring-cloud-tencent-dependencies - ${version} + 1.6.0-2020.0.5 pom import diff --git a/README.md b/README.md index d14ae6274..812a81d17 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ [![codecov.io](https://codecov.io/gh/Tencent/spring-cloud-tencent/branch/2020.0/graph/badge.svg)](https://codecov.io/gh/Tencent/spring-cloud-tencent?branch=2020.0) [![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/Tencent/spring-cloud-tencent.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Tencent/spring-cloud-tencent/context:java) - English | [简体中文](./README-zh.md) ## Introduction @@ -73,7 +72,7 @@ For example: com.tencent.cloud spring-cloud-tencent-dependencies - ${version} + 1.6.0-2020.0.5 pom import diff --git a/pom.xml b/pom.xml index 9058818aa..994cbcd02 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ spring-cloud-starter-tencent-polaris-ratelimit spring-cloud-starter-tencent-polaris-circuitbreaker spring-cloud-starter-tencent-polaris-router + spring-cloud-tencent-plugin-starters spring-cloud-tencent-dependencies spring-cloud-tencent-examples spring-cloud-tencent-coverage diff --git a/spring-cloud-tencent-coverage/pom.xml b/spring-cloud-tencent-coverage/pom.xml index 277425753..2fb6d5b66 100644 --- a/spring-cloud-tencent-coverage/pom.xml +++ b/spring-cloud-tencent-coverage/pom.xml @@ -68,7 +68,12 @@ com.tencent.cloud spring-cloud-starter-tencent-polaris-config - + + + com.tencent.cloud + spring-cloud-tencent-pushgateway-plugin + + diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index e03cdbb9a..ecc23b430 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 1.7.0-2020.0.5-SNAPSHOT - 1.7.2 + 1.7.3 1.2.11 4.5.1 1.12.10 @@ -81,6 +81,7 @@ 3.16.1 1.69 31.0.1-jre + 0.11.0 3.2.0 @@ -159,6 +160,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-tencent-pushgateway-plugin + ${revision} + + com.google.guava @@ -222,6 +229,18 @@ ${bcprov-jdk15on.version} + + net.bytebuddy + byte-buddy + ${byte-buddy.version} + + + + io.prometheus + simpleclient_pushgateway + ${prometheus.version} + + org.mockito mockito-inline @@ -235,12 +254,6 @@ ${mocktio.version} test - - - net.bytebuddy - byte-buddy - ${byte-buddy.version} - diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index 1e42dc349..bc5a699ef 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -14,6 +14,9 @@ spring: stat: enabled: true port: 28082 + # pushgateway: + # enabled: true + # address: 127.0.0.1:9091 tencent: metadata: content: diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml index 1711bca9e..7142ad11a 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml @@ -23,6 +23,10 @@ spring-cloud-starter-tencent-polaris-discovery + + com.tencent.cloud + spring-cloud-tencent-pushgateway-plugin + diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index 62b944010..5384f4ac2 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -22,6 +22,9 @@ spring: stat: enabled: true port: 28081 +# pushgateway: +# enabled: true +# address: 127.0.0.1:9091 # consul: # port: 8500 # host: 127.0.0.1 diff --git a/spring-cloud-tencent-plugin-starters/pom.xml b/spring-cloud-tencent-plugin-starters/pom.xml new file mode 100644 index 000000000..1d139ae7a --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/pom.xml @@ -0,0 +1,21 @@ + + + + spring-cloud-tencent + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-tencent-plugin-starters + pom + Spring Cloud Starter Tencent Solution + + + spring-cloud-tencent-pushgateway-plugin + + + diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml new file mode 100644 index 000000000..025e59d61 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml @@ -0,0 +1,32 @@ + + + + spring-cloud-tencent-plugin-starters + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-tencent-pushgateway-plugin + + + + com.tencent.cloud + spring-cloud-tencent-polaris-context + + + + com.tencent.polaris + stat-pushgateway + + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfiguration.java new file mode 100644 index 000000000..1ef1c3853 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfiguration.java @@ -0,0 +1,42 @@ +/* + * 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); + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfiguration.java new file mode 100644 index 000000000..67c336613 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfiguration.java @@ -0,0 +1,31 @@ +/* + * 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 { +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayModifier.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayModifier.java new file mode 100644 index 000000000..b680c53c0 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayModifier.java @@ -0,0 +1,70 @@ +/* + * 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; + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayProperties.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayProperties.java new file mode 100644 index 000000000..7b6f681c2 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayProperties.java @@ -0,0 +1,69 @@ +/* + * 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; + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..2976dc70f --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,23 @@ +{ + "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" + } + ] +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..468a15060 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/spring.factories @@ -0,0 +1,4 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayAutoConfiguration +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayBootstrapConfiguration diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfigurationTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfigurationTest.java new file mode 100644 index 000000000..0608c2a9b --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfigurationTest.java @@ -0,0 +1,46 @@ +/* + * 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.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(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); + }); + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfigurationTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfigurationTest.java new file mode 100644 index 000000000..2ce019691 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfigurationTest.java @@ -0,0 +1,47 @@ +/* + * 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.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); + }); + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayPropertiesTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayPropertiesTest.java new file mode 100644 index 000000000..0301c8149 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayPropertiesTest.java @@ -0,0 +1,50 @@ +/* + * 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.Assert; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +/** + * 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); + Assert.assertTrue(polarisStatPushGatewayProperties.isEnabled()); + Assert.assertEquals("127.0.0.1:9091", polarisStatPushGatewayProperties.getAddress()); + Assert.assertEquals("1000", polarisStatPushGatewayProperties.getPushInterval().toString()); + }); + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java index b8b918202..889b18e82 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java @@ -47,7 +47,6 @@ public class PolarisStatProperties { */ private String path = "/metrics"; - public boolean isEnabled() { return enabled; } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java index 6306f68d3..8496f70b0 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java @@ -61,6 +61,14 @@ public class StatConfigModifier implements PolarisConfigModifier { configuration.getGlobal().getStatReporter() .setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig); } + else { + PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter() + .getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class); + // Set port to -1 to disable stat plugin. + prometheusHandlerConfig.setPort(-1); + configuration.getGlobal().getStatReporter() + .setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig); + } } @Override