From c536c26d9590a3c8baead95aabfa002c77951fe4 Mon Sep 17 00:00:00 2001 From: "lingxiao,wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Tue, 23 Aug 2022 11:45:49 +0800 Subject: [PATCH] =?UTF-8?q?Feature=EF=BC=9Asupport=20pushGateway=20push=20?= =?UTF-8?q?metrics=20(#504)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../endpoint/PolarisRouterEndpointTest.java | 2 +- spring-cloud-tencent-dependencies/pom.xml | 12 +++ .../discovery-callee-service/pom.xml | 5 + .../src/main/resources/bootstrap.yml | 3 + .../discovery-caller-service/pom.xml | 4 + .../src/main/resources/bootstrap.yml | 3 + spring-cloud-tencent-plugin-starters/pom.xml | 1 + .../pom.xml | 31 ++++++ ...larisStatPushGatewayAutoConfiguration.java | 43 +++++++++ ...StatPushGatewayBootstrapConfiguration.java | 32 +++++++ .../PolarisStatPushGatewayModifier.java | 73 ++++++++++++++ .../PolarisStatPushGatewayProperties.java | 96 +++++++++++++++++++ ...itional-spring-configuration-metadata.json | 35 +++++++ .../main/resources/META-INF/spring.factories | 4 + ...sStatPushGatewayAutoConfigurationTest.java | 47 +++++++++ ...PushGatewayBootstrapConfigurationTest.java | 48 ++++++++++ .../PolarisStatPushGatewayPropertiesTest.java | 54 +++++++++++ .../stat/config/PolarisStatProperties.java | 1 - 19 files changed, 493 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfiguration.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfiguration.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayModifier.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayProperties.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfigurationTest.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfigurationTest.java create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayPropertiesTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d98267c..64c7d8dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [Bugfix: update byte-buddy scope test to compile](https://github.com/Tencent/spring-cloud-tencent/pull/495) - [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/496) - [Feature: zuul supports polaris router](https://github.com/Tencent/spring-cloud-tencent/pull/502) +- [Featureļ¼šsupport pushGateway push metrics](https://github.com/Tencent/spring-cloud-tencent/pull/504) - [Refactor : optimize project and code](https://github.com/Tencent/spring-cloud-tencent/pull/506) - [Fix typo & Code optimization](https://github.com/Tencent/spring-cloud-tencent/pull/507) - [Code optimization for rpc-enhancement module](https://github.com/Tencent/spring-cloud-tencent/pull/510) diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointTest.java index 27183c2a..295ca2ef 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointTest.java @@ -42,7 +42,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; /** - * Test for {@link PolarisRouterEndpoint} + * Test for {@link PolarisRouterEndpoint}. * @author lepdou 2022-07-25 */ @RunWith(MockitoJUnitRunner.class) diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index ed81b2de..46379fe3 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -170,6 +170,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-tencent-pushgateway-plugin + ${revision} + + com.google.guava @@ -252,6 +258,12 @@ byte-buddy ${byte-buddy.version} + + + com.tencent.polaris + stat-pushgateway + ${polaris.version} + diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml index 235ed173..5d76407e 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml @@ -19,6 +19,11 @@ 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-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index 1e42dc34..f2fbae18 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 fd2eed66..f1e88d78 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 d0f01ffa..a471b124 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 @@ -18,6 +18,9 @@ spring: stat: enabled: true port: 28081 + pushgateway: + enabled: true + address: 127.0.0.1 # 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 index 12731509..cba7562e 100644 --- a/spring-cloud-tencent-plugin-starters/pom.xml +++ b/spring-cloud-tencent-plugin-starters/pom.xml @@ -17,6 +17,7 @@ spring-cloud-tencent-featureenv-plugin spring-cloud-tencent-gateway-plugin + 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 00000000..b6650d68 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml @@ -0,0 +1,31 @@ + + + + spring-cloud-tencent-plugin-starters + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-tencent-pushgateway-plugin + + + + com.tencent.polaris + stat-pushgateway + + + com.tencent.cloud + spring-cloud-tencent-polaris-context + + + 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 00000000..0ae1f13f --- /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,43 @@ +/* + * 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 00000000..9fa4b060 --- /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,32 @@ +/* + * 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 00000000..8b45a2b5 --- /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,73 @@ +/* + * 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.setPushgatewayNamespace(polarisStatPushGatewayProperties.getNamespace()); + prometheusHandlerConfig.setPushgatewayService(polarisStatPushGatewayProperties.getService()); + 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 00000000..b2db8833 --- /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,96 @@ +/* + * 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; + + /** + * Service for pushGateway. + */ + private String service; + + /** + * Namespace for pushGateway. + */ + private String namespace; + + /** + * 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 String getService() { + return service; + } + + public void setService(String service) { + this.service = service; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + 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 00000000..d1fb8e4f --- /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,35 @@ +{ + "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.namespace", + "type": "java.lang.String", + "description": "Namespace for pushGateway.", + "sourceType": "com.tencent.cloud.plugin.pushgateway.PolarisStatPushGatewayProperties" + }, + { + "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" + }, + { + "name": "spring.cloud.polaris.stat.pushgateway.service", + "type": "java.lang.String", + "description": "Service for pushGateway.", + "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 00000000..468a1506 --- /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 00000000..a04a3157 --- /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,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 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 00000000..b703d900 --- /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,48 @@ +/* + * 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 00000000..50c40a61 --- /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,54 @@ +/* + * 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.namespace=default") + .withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000") + .withPropertyValues("spring.cloud.polaris.stat.pushgateway.service=grpc://183.47.111.80:8091"); + + @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("default", polarisStatPushGatewayProperties.getNamespace()); + Assert.assertEquals("1000", polarisStatPushGatewayProperties.getPushInterval().toString()); + Assert.assertEquals("grpc://183.47.111.80:8091", polarisStatPushGatewayProperties.getService()); + }); + } +} 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 b8b91820..889b18e8 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; }