Feature:support pushGateway push metrics (#529)

* Feature:support pushGateway push metrics

* feat:optimize pushgateway plugin.

Co-authored-by: lingxiao,wu <51630311+lingxiao-wu@users.noreply.github.com>
pull/533/head
Haotian Zhang 2 years ago committed by GitHub
parent 85a42f78c0
commit 371d18f8a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,3 +22,4 @@
- [Optimize router label resolver spi](https://github.com/Tencent/spring-cloud-tencent/pull/524)
- [Code optimization for rpc-enhancement module](https://github.com/Tencent/spring-cloud-tencent/pull/526)
- [Feature: Optimized configuration update](https://github.com/Tencent/spring-cloud-tencent/pull/528)
- [Featuresupport pushGateway push metrics](https://github.com/Tencent/spring-cloud-tencent/pull/529)

@ -70,7 +70,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<!--version number-->
<version>${version}</version>
<version>1.6.0-2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>

@ -72,7 +72,7 @@ For example:
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<!--version number-->
<version>${version}</version>
<version>1.6.0-2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>

@ -48,6 +48,7 @@
<module>spring-cloud-starter-tencent-polaris-ratelimit</module>
<module>spring-cloud-starter-tencent-polaris-circuitbreaker</module>
<module>spring-cloud-starter-tencent-polaris-router</module>
<module>spring-cloud-tencent-plugin-starters</module>
<module>spring-cloud-tencent-dependencies</module>
<module>spring-cloud-tencent-examples</module>
<module>spring-cloud-tencent-coverage</module>

@ -68,7 +68,12 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

@ -73,13 +73,14 @@
<revision>1.7.0-2021.0.3-SNAPSHOT</revision>
<!-- Dependencies -->
<polaris.version>1.7.2</polaris.version>
<polaris.version>1.7.3</polaris.version>
<guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version>
<byte-buddy.version>1.12.10</byte-buddy.version>
<protobuf-java.version>3.16.1</protobuf-java.version>
<bcprov-jdk15on.version>1.69</bcprov-jdk15on.version>
<prometheus.version>0.11.0</prometheus.version>
<!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
@ -158,6 +159,12 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
<version>${revision}</version>
</dependency>
<!-- third part framework dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
@ -203,6 +210,18 @@
<version>${bcprov-jdk15on.version}</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byte-buddy.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
@ -216,12 +235,6 @@
<version>${mocktio.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byte-buddy.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

@ -14,6 +14,9 @@ spring:
stat:
enabled: true
port: 28082
# pushgateway:
# enabled: true
# address: 127.0.0.1:9091
tencent:
metadata:
content:

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

@ -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

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
<packaging>pom</packaging>
<name>Spring Cloud Starter Tencent Solution</name>
<modules>
<module>spring-cloud-tencent-pushgateway-plugin</module>
</modules>
</project>

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

@ -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);
}
}

@ -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 {
}

@ -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;
}
}

@ -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;
}
}

@ -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"
}
]
}

@ -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

@ -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);
});
}
}

@ -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);
});
}
}

@ -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());
});
}
}

@ -47,7 +47,6 @@ public class PolarisStatProperties {
*/
private String path = "/metrics";
public boolean isEnabled() {
return enabled;
}

@ -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

Loading…
Cancel
Save