19 changed files with 1531 additions and 139 deletions
@ -0,0 +1,117 @@
@@ -0,0 +1,117 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry; |
||||
import io.micrometer.core.instrument.MockClock; |
||||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; |
||||
import io.micrometer.core.instrument.simple.SimpleConfig; |
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
||||
import io.micrometer.graphite.GraphiteMeterRegistry; |
||||
import io.micrometer.jmx.JmxMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteMetricsExportAutoConfiguration; |
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.jmx.JmxMetricsExportAutoConfiguration; |
||||
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; |
||||
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.Primary; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Integration tests for metrics auto-configuration. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public class MetricsAutoConfigurationIntegrationTests { |
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
||||
.with(MetricsRun.simple()); |
||||
|
||||
@Test |
||||
public void propertyBasedMeterFilteringIsAutoConfigured() { |
||||
this.contextRunner.withPropertyValues("management.metrics.enable.my.org=false") |
||||
.run((context) -> { |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
registry.timer("my.org.timer"); |
||||
assertThat(registry.find("my.org.timer").timer()).isNull(); |
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void simpleMeterRegistryIsUsedAsAFallback() { |
||||
this.contextRunner |
||||
.run((context) -> assertThat(context.getBean(MeterRegistry.class)) |
||||
.isInstanceOf(SimpleMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void emptyCompositeIsCreatedWhenNoMeterRegistriesAreAutoConfigured() { |
||||
new ApplicationContextRunner().with(MetricsRun.limitedTo()).run((context) -> { |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
assertThat(registry).isInstanceOf(CompositeMeterRegistry.class); |
||||
assertThat(((CompositeMeterRegistry) registry).getRegistries()).isEmpty(); |
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void noCompositeIsCreatedWhenASingleMeterRegistryIsAutoConfigured() { |
||||
new ApplicationContextRunner() |
||||
.with(MetricsRun.limitedTo(GraphiteMetricsExportAutoConfiguration.class)) |
||||
.run((context) -> assertThat(context.getBean(MeterRegistry.class)) |
||||
.isInstanceOf(GraphiteMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void noCompositeIsCreatedWithMultipleRegistriesAndOneThatIsPrimary() { |
||||
new ApplicationContextRunner() |
||||
.with(MetricsRun.limitedTo(GraphiteMetricsExportAutoConfiguration.class, |
||||
JmxMetricsExportAutoConfiguration.class)) |
||||
.withUserConfiguration(PrimaryMeterRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context.getBean(MeterRegistry.class)) |
||||
.isInstanceOf(SimpleMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void compositeCreatedWithMultipleRegistries() { |
||||
new ApplicationContextRunner() |
||||
.with(MetricsRun.limitedTo(GraphiteMetricsExportAutoConfiguration.class, |
||||
JmxMetricsExportAutoConfiguration.class)) |
||||
.run((context) -> { |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
assertThat(registry).isInstanceOf(CompositeMeterRegistry.class); |
||||
assertThat(((CompositeMeterRegistry) registry).getRegistries()) |
||||
.hasAtLeastOneElementOfType(GraphiteMeterRegistry.class) |
||||
.hasAtLeastOneElementOfType(JmxMeterRegistry.class); |
||||
}); |
||||
} |
||||
|
||||
@Configuration |
||||
static class PrimaryMeterRegistryConfiguration { |
||||
|
||||
@Primary |
||||
@Bean |
||||
public MeterRegistry simpleMeterRegistry() { |
||||
return new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* 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.atlas; |
||||
|
||||
import com.netflix.spectator.atlas.AtlasConfig; |
||||
import io.micrometer.atlas.AtlasMeterRegistry; |
||||
import io.micrometer.core.instrument.Clock; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link AtlasMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class AtlasMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(AtlasMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(AtlasMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigAndMeterRegistry() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(AtlasMeterRegistry.class) |
||||
.hasSingleBean(AtlasConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(AtlasMeterRegistry.class) |
||||
.hasSingleBean(AtlasConfig.class).hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(AtlasMeterRegistry.class).hasBean("customRegistry") |
||||
.hasSingleBean(AtlasConfig.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public AtlasConfig customConfig() { |
||||
return new AtlasConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public AtlasMeterRegistry customRegistry(AtlasConfig config, Clock clock) { |
||||
return new AtlasMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* 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.ganglia; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.ganglia.GangliaConfig; |
||||
import io.micrometer.ganglia.GangliaMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link GangliaMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class GangliaMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(GangliaMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(GangliaMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigAndMeterRegistry() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(GangliaMeterRegistry.class) |
||||
.hasSingleBean(GangliaConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(GangliaMeterRegistry.class) |
||||
.hasSingleBean(GangliaConfig.class).hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(GangliaMeterRegistry.class) |
||||
.hasBean("customRegistry").hasSingleBean(GangliaConfig.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public GangliaConfig customConfig() { |
||||
return new GangliaConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public GangliaMeterRegistry customRegistry(GangliaConfig config, Clock clock) { |
||||
return new GangliaMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,115 @@
@@ -0,0 +1,115 @@
|
||||
/* |
||||
* 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.graphite; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.graphite.GraphiteConfig; |
||||
import io.micrometer.graphite.GraphiteMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link GraphiteMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class GraphiteMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(GraphiteMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(GraphiteMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigAndMeterRegistry() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(GraphiteMeterRegistry.class) |
||||
.hasSingleBean(GraphiteConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(GraphiteMeterRegistry.class) |
||||
.hasSingleBean(GraphiteConfig.class).hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(GraphiteMeterRegistry.class) |
||||
.hasBean("customRegistry").hasSingleBean(GraphiteConfig.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public GraphiteConfig customConfig() { |
||||
return new GraphiteConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
if ("Graphite.apiKey".equals(k)) { |
||||
return "12345"; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public GraphiteMeterRegistry customRegistry(GraphiteConfig config, Clock clock) { |
||||
return new GraphiteMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* 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.influx; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.influx.InfluxConfig; |
||||
import io.micrometer.influx.InfluxMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link InfluxMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class InfluxMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(InfluxMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(InfluxMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigAndMeterRegistry() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(InfluxMeterRegistry.class) |
||||
.hasSingleBean(InfluxConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(InfluxMeterRegistry.class) |
||||
.hasSingleBean(InfluxConfig.class).hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(InfluxMeterRegistry.class) |
||||
.hasBean("customRegistry").hasSingleBean(InfluxConfig.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public InfluxConfig customConfig() { |
||||
return new InfluxConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public InfluxMeterRegistry customRegistry(InfluxConfig config, Clock clock) { |
||||
return new InfluxMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,145 @@
@@ -0,0 +1,145 @@
|
||||
/* |
||||
* 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.jmx; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.core.instrument.Meter.Id; |
||||
import io.micrometer.core.instrument.config.NamingConvention; |
||||
import io.micrometer.core.instrument.util.HierarchicalNameMapper; |
||||
import io.micrometer.jmx.JmxConfig; |
||||
import io.micrometer.jmx.JmxMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link JmxMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class JmxMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(JmxMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run( |
||||
(context) -> assertThat(context).doesNotHaveBean(JmxMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigMeterRegistryAndNameMapper() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(JmxMeterRegistry.class) |
||||
.hasSingleBean(JmxConfig.class) |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(JmxMeterRegistry.class) |
||||
.hasSingleBean(JmxConfig.class).hasBean("customConfig") |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(JmxMeterRegistry.class).hasBean("customRegistry") |
||||
.hasSingleBean(JmxConfig.class) |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomHierarchicalNameMapperToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomNameMapperConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(JmxMeterRegistry.class) |
||||
.hasSingleBean(JmxConfig.class).hasBean("customNameMapper") |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public JmxConfig customConfig() { |
||||
return new JmxConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public JmxMeterRegistry customRegistry(JmxConfig config, Clock clock) { |
||||
return new JmxMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomNameMapperConfiguration { |
||||
|
||||
@Bean |
||||
public HierarchicalNameMapper customNameMapper() { |
||||
return new HierarchicalNameMapper() { |
||||
|
||||
@Override |
||||
public String toHierarchicalName(Id id, NamingConvention convention) { |
||||
return "test"; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,183 @@
@@ -0,0 +1,183 @@
|
||||
/* |
||||
* 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.prometheus; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.prometheus.PrometheusConfig; |
||||
import io.micrometer.prometheus.PrometheusMeterRegistry; |
||||
import io.prometheus.client.CollectorRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration; |
||||
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint; |
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link PrometheusMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class PrometheusMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration(AutoConfigurations |
||||
.of(PrometheusMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(PrometheusMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigCollectorRegistryAndMeterRegistry() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(PrometheusMeterRegistry.class) |
||||
.hasSingleBean(CollectorRegistry.class) |
||||
.hasSingleBean(PrometheusConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(PrometheusMeterRegistry.class) |
||||
.hasSingleBean(CollectorRegistry.class) |
||||
.hasSingleBean(PrometheusConfig.class).hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(PrometheusMeterRegistry.class) |
||||
.hasBean("customRegistry").hasSingleBean(CollectorRegistry.class) |
||||
.hasSingleBean(PrometheusConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomCollectorRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomCollectorRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(PrometheusMeterRegistry.class) |
||||
.hasBean("customCollectorRegistry") |
||||
.hasSingleBean(CollectorRegistry.class) |
||||
.hasSingleBean(PrometheusConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void addsScrapeEndpointToManagementContext() { |
||||
this.runner |
||||
.withConfiguration( |
||||
AutoConfigurations.of(ManagementContextAutoConfiguration.class)) |
||||
.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(PrometheusScrapeEndpoint.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void scrapeEndpointCanBeDisabled() { |
||||
this.runner |
||||
.withConfiguration( |
||||
AutoConfigurations.of(ManagementContextAutoConfiguration.class)) |
||||
.withPropertyValues("management.endpoint.prometheus.enabled=false") |
||||
.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(PrometheusScrapeEndpoint.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomScrapeEndpointToBeUsed() { |
||||
this.runner |
||||
.withConfiguration( |
||||
AutoConfigurations.of(ManagementContextAutoConfiguration.class)) |
||||
.withUserConfiguration(CustomEndpointConfiguration.class) |
||||
.run((context) -> assertThat(context).hasBean("customEndpoint") |
||||
.hasSingleBean(PrometheusScrapeEndpoint.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public PrometheusConfig customConfig() { |
||||
return new PrometheusConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public PrometheusMeterRegistry customRegistry(PrometheusConfig config, |
||||
CollectorRegistry collectorRegistry, Clock clock) { |
||||
return new PrometheusMeterRegistry(config, collectorRegistry, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomCollectorRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public CollectorRegistry customCollectorRegistry() { |
||||
return new CollectorRegistry(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomEndpointConfiguration { |
||||
|
||||
@Bean |
||||
public PrometheusScrapeEndpoint customEndpoint( |
||||
CollectorRegistry collectorRegistry) { |
||||
return new PrometheusScrapeEndpoint(collectorRegistry); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,107 @@
@@ -0,0 +1,107 @@
|
||||
/* |
||||
* 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.simple; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.core.instrument.MeterRegistry; |
||||
import io.micrometer.core.instrument.simple.SimpleConfig; |
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* |
||||
* Tests for {@link SimpleMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class SimplMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(SimpleMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void autoConfiguresConfigAndMeterRegistry() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(SimpleMeterRegistry.class) |
||||
.hasSingleBean(Clock.class).hasSingleBean(SimpleConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsConfigToBeCustomized() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context).hasSingleBean(SimpleConfig.class) |
||||
.hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
public void backsOffEntirelyWithCustomMeterRegistry() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context).hasSingleBean(MeterRegistry.class) |
||||
.hasBean("customRegistry").doesNotHaveBean(SimpleConfig.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public SimpleConfig customConfig() { |
||||
return new SimpleConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public MeterRegistry customRegistry() { |
||||
return mock(MeterRegistry.class); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,145 @@
@@ -0,0 +1,145 @@
|
||||
/* |
||||
* 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.statsd; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.core.instrument.Meter.Id; |
||||
import io.micrometer.core.instrument.config.NamingConvention; |
||||
import io.micrometer.core.instrument.util.HierarchicalNameMapper; |
||||
import io.micrometer.statsd.StatsdConfig; |
||||
import io.micrometer.statsd.StatsdMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link StatsdMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class StatsdMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(StatsdMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void backsOffWithoutAClock() { |
||||
this.runner.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(StatsdMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguresItsConfigMeterRegistryAndNameMapper() { |
||||
this.runner.withUserConfiguration(BaseConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(StatsdMeterRegistry.class) |
||||
.hasSingleBean(StatsdConfig.class) |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomConfigToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomConfigConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(StatsdMeterRegistry.class) |
||||
.hasSingleBean(StatsdConfig.class).hasBean("customConfig") |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomRegistryToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(StatsdMeterRegistry.class) |
||||
.hasBean("customRegistry").hasSingleBean(StatsdConfig.class) |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void allowsCustomHierarchicalNameMapperToBeUsed() { |
||||
this.runner.withUserConfiguration(CustomNameMapperConfiguration.class) |
||||
.run((context) -> assertThat(context) |
||||
.hasSingleBean(StatsdMeterRegistry.class) |
||||
.hasSingleBean(StatsdConfig.class).hasBean("customNameMapper") |
||||
.hasSingleBean(HierarchicalNameMapper.class)); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public Clock clock() { |
||||
return Clock.SYSTEM; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomConfigConfiguration { |
||||
|
||||
@Bean |
||||
public StatsdConfig customConfig() { |
||||
return new StatsdConfig() { |
||||
|
||||
@Override |
||||
public String get(String k) { |
||||
return null; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
public StatsdMeterRegistry customRegistry(StatsdConfig config, Clock clock) { |
||||
return new StatsdMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomNameMapperConfiguration { |
||||
|
||||
@Bean |
||||
public HierarchicalNameMapper customNameMapper() { |
||||
return new HierarchicalNameMapper() { |
||||
|
||||
@Override |
||||
public String toHierarchicalName(Id id, NamingConvention convention) { |
||||
return "test"; |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,141 @@
@@ -0,0 +1,141 @@
|
||||
/* |
||||
* 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.jdbc; |
||||
|
||||
import java.util.UUID; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry; |
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
||||
import org.springframework.boot.jdbc.DataSourceBuilder; |
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link DataSourcePoolMetricsAutoConfiguration}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
public class DataSourcePoolMetricsAutoConfigurationTests { |
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
||||
.withConfiguration( |
||||
AutoConfigurations.of(DataSourcePoolMetricsAutoConfiguration.class)) |
||||
.withUserConfiguration(BaseConfiguration.class); |
||||
|
||||
@Test |
||||
public void autoConfiguredDataSourceIsInstrumented() { |
||||
this.contextRunner |
||||
.withConfiguration( |
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class)) |
||||
.withPropertyValues("spring.datasource.generate-unique-name=true") |
||||
.run((context) -> { |
||||
context.getBean(DataSource.class).getConnection().getMetaData(); |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
registry.get("data.source.max.connections").tags("name", "dataSource") |
||||
.meter(); |
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void autoConfiguredDataSourceWithCustomMetricName() { |
||||
this.contextRunner |
||||
.withConfiguration( |
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class)) |
||||
.withPropertyValues("spring.datasource.generate-unique-name=true", |
||||
"management.metrics.jdbc.metric-name=custom.name") |
||||
.run((context) -> { |
||||
context.getBean(DataSource.class).getConnection().getMetaData(); |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
registry.get("custom.name.max.connections").tags("name", "dataSource") |
||||
.meter(); |
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void dataSourceInstrumentationCanBeDisabled() { |
||||
this.contextRunner |
||||
.withConfiguration( |
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class)) |
||||
.withPropertyValues("spring.datasource.generate-unique-name=true", |
||||
"management.metrics.jdbc.instrument=false") |
||||
.run((context) -> { |
||||
context.getBean(DataSource.class).getConnection().getMetaData(); |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
assertThat(registry.find("data.source.max.connections") |
||||
.tags("name", "dataSource").meter()).isNull(); |
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void allDataSourcesCanBeInstrumented() { |
||||
this.contextRunner.withUserConfiguration(TwoDataSourcesConfiguration.class) |
||||
.withConfiguration( |
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class)) |
||||
.run((context) -> { |
||||
context.getBean("firstDataSource", DataSource.class).getConnection() |
||||
.getMetaData(); |
||||
context.getBean("secondOne", DataSource.class).getConnection() |
||||
.getMetaData(); |
||||
MeterRegistry registry = context.getBean(MeterRegistry.class); |
||||
registry.get("data.source.max.connections").tags("name", "first") |
||||
.meter(); |
||||
registry.get("data.source.max.connections").tags("name", "secondOne") |
||||
.meter(); |
||||
}); |
||||
} |
||||
|
||||
@Configuration |
||||
static class BaseConfiguration { |
||||
|
||||
@Bean |
||||
public SimpleMeterRegistry simpleMeterRegistry() { |
||||
return new SimpleMeterRegistry(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
static class TwoDataSourcesConfiguration { |
||||
|
||||
@Bean |
||||
public DataSource firstDataSource() { |
||||
return createDataSource(); |
||||
} |
||||
|
||||
@Bean |
||||
public DataSource secondOne() { |
||||
return createDataSource(); |
||||
} |
||||
|
||||
private DataSource createDataSource() { |
||||
String url = "jdbc:hsqldb:mem:test-" + UUID.randomUUID(); |
||||
return DataSourceBuilder.create().url(url).build(); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue