Add auto-configuration for exporting metrics to Wavefront

See gh-12068
pull/12098/head
Jon Schneider 7 years ago committed by Andy Wilkinson
parent 46eb88c299
commit 142dbb2213

@ -137,6 +137,11 @@
<artifactId>micrometer-registry-statsd</artifactId> <artifactId>micrometer-registry-statsd</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-wavefront</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>io.projectreactor.ipc</groupId> <groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId> <artifactId>reactor-netty</artifactId>

@ -0,0 +1,63 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
import io.micrometer.core.instrument.Clock;
import io.micrometer.wavefront.WavefrontConfig;
import io.micrometer.wavefront.WavefrontMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;
/**
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to Wavefront.
*
* @author Jon Schneider
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(WavefrontMeterRegistry.class)
@ConditionalOnProperty(prefix = "management.metrics.export.wavefront", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(WavefrontProperties.class)
public class WavefrontMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean(WavefrontConfig.class)
public WavefrontConfig wavefrontConfig(WavefrontProperties props) {
return new WavefrontPropertiesConfigAdapter(props);
}
@Bean(destroyMethod = "stop")
@ConditionalOnMissingBean
public WavefrontMeterRegistry wavefrontMeterRegistry(WavefrontConfig config, Clock clock) {
return new WavefrontMeterRegistry(config, clock);
}
}

@ -0,0 +1,86 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* {@link ConfigurationProperties} for configuring Wavefront metrics export.
*
* @author Jon Schneider
* @since 2.0.0
*/
@ConfigurationProperties("management.metrics.export.wavefront")
public class WavefrontProperties extends StepRegistryProperties {
/**
* The URI to publish metrics to. The URI could represent a Wavefront sidecar or the
* Wavefront API host. This host could also represent an internal proxy set up in your environment
* that forwards metrics data to the Wavefront API host.
*
* If publishing metrics to a Wavefront proxy (as described in https://docs.wavefront.com/proxies_installing.html),
* the host must be in the proxy://HOST:PORT format.
*/
private String uri;
/**
* Uniquely identifies the app instance that is publishing metrics to Wavefront. Defaults to the local host name.
*/
private String source;
/**
* Required when publishing directly to the Wavefront API host, otherwise does nothing.
*/
private String apiToken;
/**
* Global prefix to separate metrics originating from this app's white box instrumentation from those originating from other Wavefront integrations when viewed in the Wavefront UI.
*/
private String globalPrefix;
public String getUri() {
return this.uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getSource() {
return this.source;
}
public void setSource(String source) {
this.source = source;
}
public String getApiToken() {
return this.apiToken;
}
public void setApiToken(String apiToken) {
this.apiToken = apiToken;
}
public String getGlobalPrefix() {
return this.globalPrefix;
}
public void setGlobalPrefix(String globalPrefix) {
this.globalPrefix = globalPrefix;
}
}

@ -0,0 +1,59 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
import io.micrometer.wavefront.WavefrontConfig;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter;
/**
* Adapter to convert {@link WavefrontProperties} to a {@link WavefrontConfig}.
*
* @author Jon Schneider
* @since 2.0.0
*/
public class WavefrontPropertiesConfigAdapter extends PropertiesConfigAdapter<WavefrontProperties> implements WavefrontConfig {
public WavefrontPropertiesConfigAdapter(WavefrontProperties properties) {
super(properties);
}
@Override
public String get(String k) {
return null;
}
@Override
public String uri() {
return get(WavefrontProperties::getUri, WavefrontConfig.DEFAULT_DIRECT::uri);
}
@Override
public String source() {
return get(WavefrontProperties::getSource, WavefrontConfig.super::source);
}
@Override
public String apiToken() {
return get(WavefrontProperties::getApiToken, WavefrontConfig.super::apiToken);
}
@Override
public String globalPrefix() {
return get(WavefrontProperties::getGlobalPrefix, WavefrontConfig.super::globalPrefix);
}
}

@ -0,0 +1,20 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
/**
* Support for exporting actuator metrics to StatsD.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;

@ -47,6 +47,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.Prometh
org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.export.signalfx.SignalFxMetricsExportAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.export.signalfx.SignalFxMetricsExportAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront.WavefrontMetricsExportAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.integration.MetricsIntegrationAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.integration.MetricsIntegrationAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.client.RestTemplateMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.web.client.RestTemplateMetricsAutoConfiguration,\

@ -0,0 +1,156 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
import java.util.Map;
import io.micrometer.core.instrument.Clock;
import io.micrometer.wavefront.WavefrontConfig;
import io.micrometer.wavefront.WavefrontMeterRegistry;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link WavefrontMetricsExportAutoConfiguration}.
*
* @author Jon Schneider
*/
public class WavefrontMetricsExportAutoConfigurationTests {
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(WavefrontMetricsExportAutoConfiguration.class));
@Test
public void backsOffWithoutAClock() {
this.runner.run((context) -> assertThat(context)
.doesNotHaveBean(WavefrontMeterRegistry.class));
}
@Test
public void autoConfigurationCanBeDisabled() {
this.runner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues(
"management.metrics.export.wavefront.enabled=false")
.run((context) -> assertThat(context)
.doesNotHaveBean(WavefrontMeterRegistry.class)
.doesNotHaveBean(WavefrontConfig.class));
}
@Test
public void allowsConfigToBeCustomized() {
this.runner
.withUserConfiguration(CustomConfigConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(Clock.class)
.hasSingleBean(WavefrontMeterRegistry.class)
.hasSingleBean(WavefrontConfig.class).hasBean("customConfig"));
}
@Test
public void allowsRegistryToBeCustomized() {
this.runner
.withUserConfiguration(CustomRegistryConfiguration.class)
.withPropertyValues(
"management.metrics.export.wavefront.api-token=abcde")
.run((context) -> assertThat(context).hasSingleBean(Clock.class)
.hasSingleBean(WavefrontConfig.class)
.hasSingleBean(WavefrontMeterRegistry.class)
.hasBean("customRegistry"));
}
@Test
public void stopsMeterRegistryWhenContextIsClosed() {
this.runner
.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues(
"management.metrics.export.wavefront.api-token=abcde")
.run((context) -> {
WavefrontMeterRegistry registry = spyOnDisposableBean(
WavefrontMeterRegistry.class, context);
context.close();
verify(registry).stop();
});
}
@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}
@Configuration
static class BaseConfiguration {
@Bean
public Clock customClock() {
return Clock.SYSTEM;
}
}
@Configuration
@Import(BaseConfiguration.class)
static class CustomConfigConfiguration {
@Bean
public WavefrontConfig customConfig() {
return new WavefrontConfig() {
@Override
public String get(String key) {
return null;
}
@Override
public String uri() {
return WavefrontConfig.DEFAULT_PROXY.uri();
}
};
}
}
@Configuration
@Import(BaseConfiguration.class)
static class CustomRegistryConfiguration {
@Bean(destroyMethod = "stop")
public WavefrontMeterRegistry customRegistry(WavefrontConfig config, Clock clock) {
return new WavefrontMeterRegistry(config, clock);
}
}
}

@ -902,6 +902,11 @@
<artifactId>micrometer-registry-statsd</artifactId> <artifactId>micrometer-registry-statsd</artifactId>
<version>${micrometer.version}</version> <version>${micrometer.version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-wavefront</artifactId>
<version>${micrometer.version}</version>
</dependency>
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-bom</artifactId> <artifactId>netty-bom</artifactId>

@ -1391,6 +1391,10 @@ content into your application. Rather, pick only the properties that you need.
management.metrics.export.statsd.polling-frequency=10s # How often gauges will be polled. When a gauge is polled, its value is recalculated and if the value has changed, it is sent to the StatsD server. management.metrics.export.statsd.polling-frequency=10s # How often gauges will be polled. When a gauge is polled, its value is recalculated and if the value has changed, it is sent to the StatsD server.
management.metrics.export.statsd.port=8125 # Port of the StatsD server to receive exported metrics. management.metrics.export.statsd.port=8125 # Port of the StatsD server to receive exported metrics.
management.metrics.export.statsd.queue-size=2147483647 # Maximum size of the queue of items waiting to be sent to the StatsD server. management.metrics.export.statsd.queue-size=2147483647 # Maximum size of the queue of items waiting to be sent to the StatsD server.
management.metrics.export.wavefront.uri= # Optional custom URI for the Wavefront API or proxy.
management.metrics.export.wavefront.source= # Uniquely identifies the app instance that is publishing metrics to Wavefront. Defaults to the local host name.
management.metrics.export.wavefront.api-token= # Required when publishing directly to the Wavefront API host, otherwise does nothing.
management.metrics.export.wavefront.global-prefix= # Setting a global prefix separates metrics originating from this app's whitebox instrumentation from those originating from other Wavefront integrations.
management.metrics.use-global-registry=true # Whether auto-configured MeterRegistry implementations should be bound to the global static registry on Metrics. management.metrics.use-global-registry=true # Whether auto-configured MeterRegistry implementations should be bound to the global static registry on Metrics.
management.metrics.web.client.max-uri-tags=100 # Maximum number of unique URI tag values allowed. After the max number of tag values is reached, metrics with additional tag values are denied by filter. management.metrics.web.client.max-uri-tags=100 # Maximum number of unique URI tag values allowed. After the max number of tag values is reached, metrics with additional tag values are denied by filter.
management.metrics.web.client.record-request-percentiles=false # Whether instrumented requests record percentiles histogram buckets by default. management.metrics.web.client.record-request-percentiles=false # Whether instrumented requests record percentiles histogram buckets by default.

Loading…
Cancel
Save