diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java index 07eba89a7..e2c991d97 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java @@ -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 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 getPushGatewayAddress() { - return pushGatewayAddress; - } - - public void setPushGatewayAddress(List 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 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 + '}'; } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesAutoConfiguration.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesAutoConfiguration.java index 536aa22c4..9d07c6bb3 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesAutoConfiguration.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesAutoConfiguration.java @@ -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); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPushGatewayProperties.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPushGatewayProperties.java new file mode 100644 index 000000000..14dcc3aa8 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPushGatewayProperties.java @@ -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 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 getAddress() { + return address; + } + + void setAddress(List address) { + this.address = address; + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java index 066e8361e..4b36cc14a 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java @@ -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 diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 8b283025d..db442a87f 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -55,7 +55,7 @@ }, { "name": "spring.cloud.polaris.stat.pushgateway.address", - "type": "java.lang.String", + "type": "java.util.List", "description": "PushGateway address.", "sourceType": "com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties" }, diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java index cfe09e8f9..4fd4c4491 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java @@ -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 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"); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java index fddc26f22..22b1fe408 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifierTest.java @@ -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");