feature:support pushGateway push metrics

pull/504/head
wulingxiao 3 years ago
parent b009f03819
commit 462f528e77

@ -26,9 +26,9 @@ import com.tencent.cloud.rpc.enhancement.stat.config.plugin.PrometheusPushGatewa
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
/** /**
@ -38,7 +38,7 @@ import org.springframework.core.env.Environment;
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled @ConditionalOnPolarisEnabled
@Import({PolarisStatProperties.class}) @EnableConfigurationProperties(PolarisStatProperties.class)
public class PolarisStatPropertiesAutoConfiguration { public class PolarisStatPropertiesAutoConfiguration {
@Bean @Bean

@ -20,50 +20,83 @@ package com.tencent.cloud.rpc.enhancement.stat.plugin;
import java.util.Objects; import java.util.Objects;
import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties; import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties;
import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.stat.config.plugin.PrometheusPushGatewayContainer; import com.tencent.cloud.rpc.enhancement.stat.config.plugin.PrometheusPushGatewayContainer;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Test for {@link PrometheusPushGatewayContainer}. * Test for {@link PrometheusPushGatewayContainer}.
* *
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = PrometheusPushGatewayContainerTest.TestApplication.class,
properties = {"spring.cloud.polaris.stat.pushgateway.enabled=true",
"spring.cloud.polaris.stat.enabled=true",
"spring.cloud.polaris.stat.pushgateway.shut-down-strategy=DELETE",
"spring.cloud.polaris.stat.pushgateway.push-rate=1m",
"spring.cloud.polaris.stat.pushgateway.job=test",
"spring.cloud.polaris.stat.pushgateway.grouping-keys.instance=test"})
public class PrometheusPushGatewayContainerTest { public class PrometheusPushGatewayContainerTest {
@Autowired @Test
private ApplicationContext applicationContext; public void testWithPushGatewayAndPushGatewayEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class));
contextRunner.run(context -> {
assertThat(context).hasSingleBean(PrometheusPushGatewayContainer.class);
});
}
@Test @Test
public void prometheusPushGatewayContainerTest() { public void testWithoutPushGatewayEnabled() {
PolarisStatProperties polarisStatProperties = applicationContext.getBean(PolarisStatProperties.class); ApplicationContextRunner contextRunner = new ApplicationContextRunner()
PolarisStatProperties.PushGatewayProperties pushgateway = polarisStatProperties.getPushgateway(); .withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=false")
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class));
contextRunner.run(context -> {
assertThat(context).doesNotHaveBean(PrometheusPushGatewayContainer.class);
});
}
@Test
public void testWithoutStatEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=false")
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class));
contextRunner.run(context -> {
assertThat(context).doesNotHaveBean(PrometheusPushGatewayContainer.class);
});
}
@Test
public void testWithoutPushGatewayAndStatEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=false")
.withPropertyValues("spring.cloud.polaris.stat.enabled=false")
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class));
contextRunner.run(context -> {
assertThat(context).doesNotHaveBean(PrometheusPushGatewayContainer.class);
});
}
@Test
public void testPushGatewayProperties() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.job=test")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.grouping-keys.instance=test")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-rate=1m")
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class));
contextRunner.run(context -> {
assertThat(context).hasSingleBean(PrometheusPushGatewayContainer.class);
PolarisStatProperties properties = context.getBean(PolarisStatProperties.class);
PolarisStatProperties.PushGatewayProperties pushgateway = properties.getPushgateway();
Assertions.assertFalse(Objects.isNull(pushgateway)); Assertions.assertFalse(Objects.isNull(pushgateway));
Assertions.assertEquals(pushgateway.getJob(), "test"); Assertions.assertEquals(pushgateway.getJob(), "test");
Assertions.assertEquals(pushgateway.getPushRate().toMillis(), 60000); Assertions.assertEquals(pushgateway.getPushRate().toMillis(), 60000);
Assertions.assertEquals(pushgateway.getShutDownStrategy(), PolarisStatProperties.ShutDownStrategy.DELETE); });
applicationContext.getBean(PrometheusPushGatewayContainer.class);
}
@SpringBootApplication
protected static class TestApplication {
} }
} }

Loading…
Cancel
Save