From d37d18c533552abb28218c71689076e0f0ff5014 Mon Sep 17 00:00:00 2001 From: wulingxiao <1251605638@qqcom> Date: Tue, 16 Aug 2022 22:37:41 +0800 Subject: [PATCH] feature:support pushGateway push metrics --- spring-cloud-tencent-dependencies/pom.xml | 12 ++- spring-cloud-tencent-plugin-starters/pom.xml | 1 + .../pom.xml | 26 +++++ ...larisStatPushGatewayAutoConfiguration.java | 42 ++++++++ ...StatPushGatewayBootstrapConfiguration.java | 32 +++++++ .../PolarisStatPushGatewayModifier.java | 73 ++++++++++++++ .../PolarisStatPushGatewayProperties.java | 96 +++++++++++++++++++ ...itional-spring-configuration-metadata.json | 35 +++++++ .../main/resources/META-INF/spring.factories | 4 + 9 files changed, 318 insertions(+), 3 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 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 1a9e97889..bfae46a0b 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -171,6 +171,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-tencent-pushgateway-plugin + ${revision} + + com.google.guava @@ -255,9 +261,9 @@ - io.prometheus - simpleclient_pushgateway - ${pushgateway.version} + com.tencent.polaris + stat-pushgateway + ${polaris.version} diff --git a/spring-cloud-tencent-plugin-starters/pom.xml b/spring-cloud-tencent-plugin-starters/pom.xml index 127315097..cba7562ec 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 000000000..9a5e0cb69 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-pushgateway-plugin/pom.xml @@ -0,0 +1,26 @@ + + + + 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 + + + + \ 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..ecf8cdbdb --- /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 org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +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) +@ConditionalOnProperty(value = "spring.cloud.polaris.stat.pushgateway.enabled", havingValue = "true") +@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..9fa4b060e --- /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 000000000..5f69471c8 --- /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 000000000..d9aaa01d5 --- /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. + */ + private String service; + + /** + * Namespace. + */ + 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 000000000..09acc1c36 --- /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.", + "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.", + "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