diff --git a/CHANGELOG.md b/CHANGELOG.md index b8c7f472..a0f10a5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [docs:support auto snapshot release in GitHub Action.](https://github.com/Tencent/spring-cloud-tencent/pull/871) - [feature:add User-Agent:polaris for healthyCheck api.](https://github.com/Tencent/spring-cloud-tencent/pull/872) - [optimize:optimize ServiceRuleManager.](https://github.com/Tencent/spring-cloud-tencent/pull/877) +- [refactor:refactor stat module.](https://github.com/Tencent/spring-cloud-tencent/pull/880) diff --git a/spring-cloud-starter-tencent-all/pom.xml b/spring-cloud-starter-tencent-all/pom.xml index 1aaa8bbc..e5c54f51 100644 --- a/spring-cloud-starter-tencent-all/pom.xml +++ b/spring-cloud-starter-tencent-all/pom.xml @@ -46,11 +46,6 @@ spring-cloud-starter-tencent-metadata-transfer - - com.tencent.cloud - spring-cloud-tencent-pushgateway-plugin - - org.springframework.cloud diff --git a/spring-cloud-tencent-coverage/pom.xml b/spring-cloud-tencent-coverage/pom.xml index 9f8a67c7..7a566190 100644 --- a/spring-cloud-tencent-coverage/pom.xml +++ b/spring-cloud-tencent-coverage/pom.xml @@ -73,11 +73,6 @@ com.tencent.cloud spring-cloud-tencent-gateway-plugin - - - 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 4408cffc..704cb943 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 1.10.0-2022.0.1-SNAPSHOT - 1.11.0-SNAPSHOT + 1.11.0 31.1-jre 4.9.0 1.12.19 @@ -170,12 +170,6 @@ ${revision} - - com.tencent.cloud - spring-cloud-tencent-pushgateway-plugin - ${revision} - - com.google.guava 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 647254fa..fed05b95 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 @@ -24,10 +24,6 @@ spring-cloud-starter-tencent-polaris-discovery - - com.tencent.cloud - spring-cloud-tencent-pushgateway-plugin - diff --git a/spring-cloud-tencent-plugin-starters/pom.xml b/spring-cloud-tencent-plugin-starters/pom.xml index cba7562e..6487f9a8 100644 --- a/spring-cloud-tencent-plugin-starters/pom.xml +++ b/spring-cloud-tencent-plugin-starters/pom.xml @@ -1,6 +1,6 @@ - spring-cloud-tencent @@ -17,7 +17,6 @@ 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 deleted file mode 100644 index 22a80c0f..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - 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 - 1.10.5 - - - - 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 deleted file mode 100644 index 1ef1c385..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfiguration.java +++ /dev/null @@ -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); - } -} 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 deleted file mode 100644 index 67c33661..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfiguration.java +++ /dev/null @@ -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 { -} 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 deleted file mode 100644 index b680c53c..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayModifier.java +++ /dev/null @@ -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; - } -} 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 deleted file mode 100644 index 7b6f681c..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayProperties.java +++ /dev/null @@ -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; - } -} 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 deleted file mode 100644 index 2976dc70..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ /dev/null @@ -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" - } - ] -} 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 deleted file mode 100644 index b6d09d6f..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayAutoConfigurationTest.java +++ /dev/null @@ -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); - }); - } -} 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 deleted file mode 100644 index bb332d94..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayBootstrapConfigurationTest.java +++ /dev/null @@ -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); - }); - } -} 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 deleted file mode 100644 index 52d14ad8..00000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/src/test/java/com/tencent/cloud/plugin/pushgateway/PolarisStatPushGatewayPropertiesTest.java +++ /dev/null @@ -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"); - }); - } -} 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 5905eb4d..2ad8e5de 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 @@ -17,6 +17,7 @@ package com.tencent.cloud.rpc.enhancement.stat.config; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -47,6 +48,25 @@ public class PolarisStatProperties { */ 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() { return enabled; } @@ -78,4 +98,28 @@ public class PolarisStatProperties { public void setPath(String 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; + } } 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 8496f70b..0f06c965 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 @@ -47,28 +47,35 @@ public class StatConfigModifier implements PolarisConfigModifier { public void modify(ConfigurationImpl configuration) { // Turn on stat reporter configuration. configuration.getGlobal().getStatReporter().setEnable(polarisStatProperties.isEnabled()); - + PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter() + .getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class); // Set prometheus plugin. if (polarisStatProperties.isEnabled()) { - PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter() - .getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class); - if (!StringUtils.hasText(polarisStatProperties.getHost())) { - polarisStatProperties.setHost(environment.getProperty("spring.cloud.client.ip-address")); + + if (polarisStatProperties.isPushGatewayEnabled()) { + // push gateway + prometheusHandlerConfig.setType("push"); + prometheusHandlerConfig.setAddress(polarisStatProperties.getPushGatewayAddress()); + prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval()); } - prometheusHandlerConfig.setHost(polarisStatProperties.getHost()); - prometheusHandlerConfig.setPort(polarisStatProperties.getPort()); - prometheusHandlerConfig.setPath(polarisStatProperties.getPath()); - configuration.getGlobal().getStatReporter() - .setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig); + else { + // pull metrics + prometheusHandlerConfig.setType("pull"); + if (!StringUtils.hasText(polarisStatProperties.getHost())) { + polarisStatProperties.setHost(environment.getProperty("spring.cloud.client.ip-address")); + } + prometheusHandlerConfig.setHost(polarisStatProperties.getHost()); + prometheusHandlerConfig.setPort(polarisStatProperties.getPort()); + prometheusHandlerConfig.setPath(polarisStatProperties.getPath()); + } + } 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); } + configuration.getGlobal().getStatReporter() + .setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig); } @Override diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 1f618241..a2a449b3 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -52,6 +52,25 @@ "type": "java.lang.String", "defaultValue": "/metrics", "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" } ] } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementReporterPropertiesTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementReporterPropertiesTest.java index e483cf59..48db4807 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementReporterPropertiesTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementReporterPropertiesTest.java @@ -39,7 +39,7 @@ import static org.springframework.http.HttpStatus.Series.SERVER_ERROR; */ @ExtendWith(SpringExtension.class) @SpringBootTest(classes = RpcEnhancementReporterPropertiesTest.TestApplication.class) -@ActiveProfiles("disable") +@ActiveProfiles("test") public class RpcEnhancementReporterPropertiesTest { @Autowired diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java deleted file mode 100644 index fbcb0855..00000000 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesDisableTest.java +++ /dev/null @@ -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 { - - } -} diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java index 88ac826c..7074a39b 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java @@ -18,13 +18,9 @@ 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 org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import static org.assertj.core.api.Assertions.assertThat; @@ -33,25 +29,49 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Haotian Zhang */ -@ExtendWith(SpringExtension.class) -@SpringBootTest(classes = PolarisStatPropertiesTest.TestApplication.class) -@ActiveProfiles("test") public class PolarisStatPropertiesTest { - @Autowired - private PolarisStatProperties polarisStatProperties; + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .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 public void testDefaultInitialization() { - assertThat(polarisStatProperties).isNotNull(); - assertThat(polarisStatProperties.isEnabled()).isTrue(); - assertThat(polarisStatProperties.getHost()).isNotBlank(); - assertThat(polarisStatProperties.getPort()).isEqualTo(20000); - assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx"); + contextRunner.run(context -> { + PolarisStatProperties polarisStatProperties = context.getBean(PolarisStatProperties.class); + + assertThat(polarisStatProperties).isNotNull(); + 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 - protected static class TestApplication { + @Test + 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"); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java new file mode 100644 index 00000000..ba55b6f2 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java @@ -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 { + + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties deleted file mode 100644 index a6af090d..00000000 --- a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-disable.properties +++ /dev/null @@ -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 diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties index 96d94d0d..56881cd7 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties +++ b/spring-cloud-tencent-rpc-enhancement/src/test/resources/application-test.properties @@ -1,3 +1,5 @@ -spring.cloud.polaris.stat.enabled=true -spring.cloud.polaris.stat.port=20000 -spring.cloud.polaris.stat.path=/xxx +spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error=false +spring.cloud.tencent.rpc-enhancement.reporter.series[0]=CLIENT_ERROR +spring.cloud.tencent.rpc-enhancement.reporter.series[1]=SERVER_ERROR +spring.cloud.tencent.rpc-enhancement.reporter.statuses[0]=MULTIPLE_CHOICES +spring.cloud.tencent.rpc-enhancement.reporter.statuses[1]=MOVED_PERMANENTLY