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"; 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. * The path regex list for stat for aggregation.
*/ */
@ -99,53 +62,6 @@ public class PolarisStatProperties {
this.path = path; 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() { public List<String> getPathRegexList() {
return pathRegexList; return pathRegexList;
@ -160,12 +76,6 @@ public class PolarisStatProperties {
return "PolarisStatProperties{" + return "PolarisStatProperties{" +
"enabled=" + enabled + "enabled=" + enabled +
", path='" + path + '\'' + ", path='" + path + '\'' +
", pushGatewayEnabled=" + pushGatewayEnabled +
", pushGatewayAddress='" + pushGatewayAddress + '\'' +
", statNamespace='" + statNamespace + '\'' +
", statService='" + statService + '\'' +
", pushGatewayPushInterval=" + pushGatewayPushInterval +
", openGzip=" + openGzip +
", pathRegexList=" + pathRegexList + ", pathRegexList=" + pathRegexList +
'}'; '}';
} }

@ -31,12 +31,12 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled @ConditionalOnPolarisEnabled
@EnableConfigurationProperties(PolarisStatProperties.class) @EnableConfigurationProperties({PolarisStatProperties.class, PolarisStatPushGatewayProperties.class})
public class PolarisStatPropertiesAutoConfiguration { public class PolarisStatPropertiesAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public StatConfigModifier statReporterConfigModifier(PolarisStatProperties polarisStatProperties) { public StatConfigModifier statReporterConfigModifier(PolarisStatProperties polarisStatProperties, PolarisStatPushGatewayProperties polarisStatPushGatewayProperties) {
return new StatConfigModifier(polarisStatProperties); 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; private final PolarisStatProperties polarisStatProperties;
public StatConfigModifier(PolarisStatProperties polarisStatProperties) { private final PolarisStatPushGatewayProperties polarisStatPushGatewayProperties;
public StatConfigModifier(PolarisStatProperties polarisStatProperties, PolarisStatPushGatewayProperties polarisStatPushGatewayProperties) {
this.polarisStatProperties = polarisStatProperties; this.polarisStatProperties = polarisStatProperties;
this.polarisStatPushGatewayProperties = polarisStatPushGatewayProperties;
} }
@Override @Override
@ -48,14 +51,14 @@ public class StatConfigModifier implements PolarisConfigModifier {
prometheusHandlerConfig.setPathRegexList(polarisStatProperties.getPathRegexList()); prometheusHandlerConfig.setPathRegexList(polarisStatProperties.getPathRegexList());
// Set prometheus plugin. // Set prometheus plugin.
if (polarisStatProperties.isEnabled()) { if (polarisStatProperties.isEnabled()) {
if (polarisStatProperties.isPushGatewayEnabled()) { if (polarisStatPushGatewayProperties.isPushGatewayEnabled()) {
// push gateway // push gateway
prometheusHandlerConfig.setType("push"); prometheusHandlerConfig.setType("push");
prometheusHandlerConfig.setAddress(polarisStatProperties.getPushGatewayAddress()); prometheusHandlerConfig.setAddress(polarisStatPushGatewayProperties.getAddress());
prometheusHandlerConfig.setNamespace(polarisStatProperties.getStatNamespace()); prometheusHandlerConfig.setNamespace(polarisStatPushGatewayProperties.getStatNamespace());
prometheusHandlerConfig.setService(polarisStatProperties.getStatService()); prometheusHandlerConfig.setService(polarisStatPushGatewayProperties.getStatService());
prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval()); prometheusHandlerConfig.setPushInterval(polarisStatPushGatewayProperties.getPushGatewayPushInterval());
prometheusHandlerConfig.setOpenGzip(polarisStatProperties.getOpenGzip()); prometheusHandlerConfig.setOpenGzip(polarisStatPushGatewayProperties.getOpenGzip());
} }
else { else {
// pull metrics // pull metrics

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

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

Loading…
Cancel
Save