feature:support pushGateway push metrics

pull/504/head
wulingxiao 3 years ago
parent 148105d184
commit d37d18c533

@ -171,6 +171,12 @@
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-pushgateway-plugin</artifactId>
<version>${revision}</version>
</dependency>
<!-- third part framework dependencies --> <!-- third part framework dependencies -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
@ -255,9 +261,9 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.prometheus</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>simpleclient_pushgateway</artifactId> <artifactId>stat-pushgateway</artifactId>
<version>${pushgateway.version}</version> <version>${polaris.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

@ -17,6 +17,7 @@
<modules> <modules>
<module>spring-cloud-tencent-featureenv-plugin</module> <module>spring-cloud-tencent-featureenv-plugin</module>
<module>spring-cloud-tencent-gateway-plugin</module> <module>spring-cloud-tencent-gateway-plugin</module>
<module>spring-cloud-tencent-pushgateway-plugin</module>
</modules> </modules>
</project> </project>

@ -0,0 +1,26 @@
<?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-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.polaris</groupId>
<artifactId>stat-pushgateway</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
</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 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);
}
}

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

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

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

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

@ -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
Loading…
Cancel
Save