feat: support config event and monitor address list.

pull/1670/head
fishtailfu 1 month ago
parent 3f839fde3b
commit d490aeb311

@ -41,43 +41,6 @@ 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 List<String> pushGatewayAddress;
/**
* PushGateway namespace.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.namespace:Polaris}")
private String statNamespace = "Polaris";
/**
* PushGateway service.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.service:polaris.pushgateway}")
private String statService = "polaris.pushgateway";
/**
* Push metrics interval.
* unit: milliseconds default 60s.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.push-interval:#{60000}}")
private Long pushGatewayPushInterval = 60 * 1000L;
/**
* If push gateway gzip open. default false.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.open-gzip:#{false}}")
private Boolean openGzip = false;
/**
* The path regex list for stat for aggregation.
*/
@ -99,53 +62,6 @@ public class PolarisStatProperties {
this.path = path;
}
public boolean isPushGatewayEnabled() {
return pushGatewayEnabled;
}
public void setPushGatewayEnabled(boolean pushGatewayEnabled) {
this.pushGatewayEnabled = pushGatewayEnabled;
}
public List<String> getPushGatewayAddress() {
return pushGatewayAddress;
}
public void setPushGatewayAddress(List<String> pushGatewayAddress) {
this.pushGatewayAddress = pushGatewayAddress;
}
public String getStatNamespace() {
return statNamespace;
}
public void setStatNamespace(String statNamespace) {
this.statNamespace = statNamespace;
}
public String getStatService() {
return statService;
}
public void setStatService(String statService) {
this.statService = statService;
}
public Long getPushGatewayPushInterval() {
return pushGatewayPushInterval;
}
public void setPushGatewayPushInterval(Long pushGatewayPushInterval) {
this.pushGatewayPushInterval = pushGatewayPushInterval;
}
public Boolean getOpenGzip() {
return openGzip;
}
public void setOpenGzip(Boolean openGzip) {
this.openGzip = openGzip;
}
public List<String> getPathRegexList() {
return pathRegexList;
@ -160,12 +76,6 @@ public class PolarisStatProperties {
return "PolarisStatProperties{" +
"enabled=" + enabled +
", path='" + path + '\'' +
", pushGatewayEnabled=" + pushGatewayEnabled +
", pushGatewayAddress='" + pushGatewayAddress + '\'' +
", statNamespace='" + statNamespace + '\'' +
", statService='" + statService + '\'' +
", pushGatewayPushInterval=" + pushGatewayPushInterval +
", openGzip=" + openGzip +
", pathRegexList=" + pathRegexList +
'}';
}

@ -31,12 +31,12 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@EnableConfigurationProperties(PolarisStatProperties.class)
@EnableConfigurationProperties({PolarisStatProperties.class, PolarisStatPushGatewayProperties.class})
public class PolarisStatPropertiesAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public StatConfigModifier statReporterConfigModifier(PolarisStatProperties polarisStatProperties) {
return new StatConfigModifier(polarisStatProperties);
public StatConfigModifier statReporterConfigModifier(PolarisStatProperties polarisStatProperties, PolarisStatPushGatewayProperties polarisStatPushGatewayProperties) {
return new StatConfigModifier(polarisStatProperties, polarisStatPushGatewayProperties);
}
}

@ -0,0 +1,95 @@
package com.tencent.cloud.rpc.enhancement.stat.config;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* The properties for stat reporter pushgateway.
*
* @author Haotian Zhang
*/
@ConfigurationProperties("spring.cloud.polaris.stat.pushgateway")
public class PolarisStatPushGatewayProperties {
/**
* If state pushGateway reporter enabled.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.enabled:#{false}}")
private boolean pushGatewayEnabled = false;
/**
* PushGateway namespace.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.namespace:Polaris}")
private String statNamespace = "Polaris";
/**
* PushGateway service.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.service:polaris.pushgateway}")
private String statService = "polaris.pushgateway";
/**
* Push metrics interval.
* unit: milliseconds default 60s.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.push-interval:#{60000}}")
private Long pushGatewayPushInterval = 60 * 1000L;
/**
* If push gateway gzip open. default false.
*/
@Value("${spring.cloud.polaris.stat.pushgateway.open-gzip:#{false}}")
private Boolean openGzip = false;
private List<String> address;
boolean isPushGatewayEnabled() {
return pushGatewayEnabled;
}
void setPushGatewayEnabled(boolean pushGatewayEnabled) {
this.pushGatewayEnabled = pushGatewayEnabled;
}
String getStatNamespace() {
return statNamespace;
}
void setStatNamespace(String statNamespace) {
this.statNamespace = statNamespace;
}
String getStatService() {
return statService;
}
void setStatService(String statService) {
this.statService = statService;
}
Long getPushGatewayPushInterval() {
return pushGatewayPushInterval;
}
void setPushGatewayPushInterval(Long pushGatewayPushInterval) {
this.pushGatewayPushInterval = pushGatewayPushInterval;
}
Boolean getOpenGzip() {
return openGzip;
}
void setOpenGzip(Boolean openGzip) {
this.openGzip = openGzip;
}
List<String> getAddress() {
return address;
}
void setAddress(List<String> address) {
this.address = address;
}
}

@ -34,8 +34,11 @@ public class StatConfigModifier implements PolarisConfigModifier {
private final PolarisStatProperties polarisStatProperties;
public StatConfigModifier(PolarisStatProperties polarisStatProperties) {
private final PolarisStatPushGatewayProperties polarisStatPushGatewayProperties;
public StatConfigModifier(PolarisStatProperties polarisStatProperties, PolarisStatPushGatewayProperties polarisStatPushGatewayProperties) {
this.polarisStatProperties = polarisStatProperties;
this.polarisStatPushGatewayProperties = polarisStatPushGatewayProperties;
}
@Override
@ -48,14 +51,14 @@ public class StatConfigModifier implements PolarisConfigModifier {
prometheusHandlerConfig.setPathRegexList(polarisStatProperties.getPathRegexList());
// Set prometheus plugin.
if (polarisStatProperties.isEnabled()) {
if (polarisStatProperties.isPushGatewayEnabled()) {
if (polarisStatPushGatewayProperties.isPushGatewayEnabled()) {
// push gateway
prometheusHandlerConfig.setType("push");
prometheusHandlerConfig.setAddress(polarisStatProperties.getPushGatewayAddress());
prometheusHandlerConfig.setNamespace(polarisStatProperties.getStatNamespace());
prometheusHandlerConfig.setService(polarisStatProperties.getStatService());
prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval());
prometheusHandlerConfig.setOpenGzip(polarisStatProperties.getOpenGzip());
prometheusHandlerConfig.setAddress(polarisStatPushGatewayProperties.getAddress());
prometheusHandlerConfig.setNamespace(polarisStatPushGatewayProperties.getStatNamespace());
prometheusHandlerConfig.setService(polarisStatPushGatewayProperties.getStatService());
prometheusHandlerConfig.setPushInterval(polarisStatPushGatewayProperties.getPushGatewayPushInterval());
prometheusHandlerConfig.setOpenGzip(polarisStatPushGatewayProperties.getOpenGzip());
}
else {
// pull metrics

@ -55,7 +55,7 @@
},
{
"name": "spring.cloud.polaris.stat.pushgateway.address",
"type": "java.lang.String",
"type": "java.util.List<java.lang.String>",
"description": "PushGateway address.",
"sourceType": "com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties"
},

@ -39,7 +39,7 @@ public class PolarisStatPropertiesTest {
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.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,127.0.0.1:9092")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.address=127.0.0.1:9091, 127.0.0.1:9092")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.namespace=test-namespace")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.service=test-service")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000")
@ -49,35 +49,35 @@ public class PolarisStatPropertiesTest {
public void testDefaultInitialization() {
contextRunner.run(context -> {
PolarisStatProperties polarisStatProperties = context.getBean(PolarisStatProperties.class);
PolarisStatPushGatewayProperties polarisStatPushGatewayProperties = context.getBean(PolarisStatPushGatewayProperties.class);
assertThat(polarisStatProperties).isNotNull();
assertThat(polarisStatProperties.isEnabled()).isTrue();
assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx");
assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue();
assertThat(polarisStatProperties.getPushGatewayAddress().get(0)).isEqualTo("127.0.0.1:9091");
assertThat(polarisStatProperties.getPushGatewayAddress().get(1)).isEqualTo("127.0.0.1:9092");
assertThat(polarisStatProperties.getStatNamespace()).isEqualTo("test-namespace");
assertThat(polarisStatProperties.getStatService()).isEqualTo("test-service");
assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
assertThat(polarisStatPushGatewayProperties.isPushGatewayEnabled()).isTrue();
assertThat(polarisStatPushGatewayProperties.getAddress().get(0)).isEqualTo("127.0.0.1:9091");
assertThat(polarisStatPushGatewayProperties.getAddress().get(1)).isEqualTo("127.0.0.1:9092");
assertThat(polarisStatPushGatewayProperties.getStatNamespace()).isEqualTo("test-namespace");
assertThat(polarisStatPushGatewayProperties.getStatService()).isEqualTo("test-service");
assertThat(polarisStatPushGatewayProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
});
}
@Test
void testGetAndSet() {
PolarisStatProperties polarisStatProperties = new PolarisStatProperties();
PolarisStatPushGatewayProperties polarisStatPushGatewayProperties = new PolarisStatPushGatewayProperties();
// PushGatewayEnabled
polarisStatProperties.setPushGatewayEnabled(true);
assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue();
polarisStatPushGatewayProperties.setPushGatewayEnabled(true);
assertThat(polarisStatPushGatewayProperties.isPushGatewayEnabled()).isTrue();
// PushGatewayAddress
List<String> pushGatewayAddress = List.of("127.0.0.1:9091", "127.0.0.1:9092");
polarisStatProperties.setPushGatewayAddress(pushGatewayAddress);
assertThat(polarisStatProperties.getPushGatewayAddress().get(0)).isEqualTo("127.0.0.1:9091");
assertThat(polarisStatProperties.getPushGatewayAddress().get(1)).isEqualTo("127.0.0.1:9092");
polarisStatPushGatewayProperties.setAddress(pushGatewayAddress);
assertThat(polarisStatPushGatewayProperties.getAddress().get(0)).isEqualTo("127.0.0.1:9091");
assertThat(polarisStatPushGatewayProperties.getAddress().get(1)).isEqualTo("127.0.0.1:9092");
// PushGatewayPushInterval
polarisStatProperties.setPushGatewayPushInterval(1000L);
assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
polarisStatPushGatewayProperties.setPushGatewayPushInterval(1000L);
assertThat(polarisStatPushGatewayProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
}
}

@ -52,7 +52,7 @@ public class StatConfigModifierTest {
.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.address=127.0.0.1:9091, 127.0.0.1:9092")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.open-gzip=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.namespace=test-namespace")
@ -92,7 +92,8 @@ public class StatConfigModifierTest {
.getGlobal().getStatReporter()
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
assertThat(prometheusHandlerConfig.getType()).isEqualTo("push");
assertThat(prometheusHandlerConfig.getAddress()).isEqualTo("127.0.0.1:9091");
assertThat(prometheusHandlerConfig.getAddress().get(0)).isEqualTo("127.0.0.1:9091");
assertThat(prometheusHandlerConfig.getAddress().get(1)).isEqualTo("127.0.0.1:9092");
assertThat(prometheusHandlerConfig.getPushInterval()).isEqualTo(1000);
assertThat(prometheusHandlerConfig.isOpenGzip()).isTrue();
assertThat(prometheusHandlerConfig.getNamespace()).isEqualTo("test-namespace");

Loading…
Cancel
Save