This commit is contained in:
Phillip Webb 2018-02-13 14:40:52 -08:00
parent d238a31d3d
commit 6771cc9674
18 changed files with 50 additions and 58 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.autoconfigure.metrics.config; package org.springframework.boot.actuate.autoconfigure.metrics;
import io.micrometer.core.instrument.config.MissingRequiredConfigurationException; import io.micrometer.core.instrument.config.MissingRequiredConfigurationException;

View File

@ -68,8 +68,8 @@ public class RabbitMetricsAutoConfiguration {
ConnectionFactory rabbitConnectionFactory = connectionFactory ConnectionFactory rabbitConnectionFactory = connectionFactory
.getRabbitConnectionFactory(); .getRabbitConnectionFactory();
String connectionFactoryName = getConnectionFactoryName(beanName); String connectionFactoryName = getConnectionFactoryName(beanName);
new RabbitMetrics(rabbitConnectionFactory, new RabbitMetrics(rabbitConnectionFactory, Tags.of("name", connectionFactoryName))
Tags.of("name", connectionFactoryName)).bindTo(this.registry); .bindTo(this.registry);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -69,8 +69,8 @@ public class DataSourcePoolMetricsAutoConfiguration {
private void bindDataSourceToRegistry(String beanName, DataSource dataSource) { private void bindDataSourceToRegistry(String beanName, DataSource dataSource) {
String dataSourceName = getDataSourceName(beanName); String dataSourceName = getDataSourceName(beanName);
new DataSourcePoolMetrics(dataSource, this.metadataProviders, new DataSourcePoolMetrics(dataSource, this.metadataProviders, dataSourceName,
dataSourceName, Collections.emptyList()).bindTo(this.registry); Collections.emptyList()).bindTo(this.registry);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -41,8 +41,7 @@ class MongoHealthIndicatorConfiguration extends
private final Map<String, MongoTemplate> mongoTemplates; private final Map<String, MongoTemplate> mongoTemplates;
MongoHealthIndicatorConfiguration( MongoHealthIndicatorConfiguration(Map<String, MongoTemplate> mongoTemplates) {
Map<String, MongoTemplate> mongoTemplates) {
this.mongoTemplates = mongoTemplates; this.mongoTemplates = mongoTemplates;
} }

View File

@ -75,5 +75,6 @@ org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyManagementChildC
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration,\ org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration,\ org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration
org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.actuate.autoconfigure.metrics.config.MissingRequiredConfigurationFailureAnalyzer org.springframework.boot.actuate.autoconfigure.metrics.MissingRequiredConfigurationFailureAnalyzer

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.autoconfigure.metrics.config; package org.springframework.boot.actuate.autoconfigure.metrics;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.newrelic.NewRelicConfig; import io.micrometer.newrelic.NewRelicConfig;

View File

@ -47,8 +47,7 @@ public class RabbitMetricsAutoConfigurationTests {
@Test @Test
public void rabbitmqNativeConnectionFactoryInstrumentationCanBeDisabled() { public void rabbitmqNativeConnectionFactoryInstrumentationCanBeDisabled() {
this.contextRunner this.contextRunner.withPropertyValues("management.metrics.enable.rabbitmq=false")
.withPropertyValues("management.metrics.enable.rabbitmq=false")
.run((context) -> { .run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("rabbitmq.connections").meter()).isNull(); assertThat(registry.find("rabbitmq.connections").meter()).isNull();

View File

@ -88,8 +88,7 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
context.getBean("secondOne", DataSource.class).getConnection() context.getBean("secondOne", DataSource.class).getConnection()
.getMetaData(); .getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("jdbc.max.connections").tags("name", "first") registry.get("jdbc.max.connections").tags("name", "first").meter();
.meter();
registry.get("jdbc.max.connections").tags("name", "secondOne") registry.get("jdbc.max.connections").tags("name", "secondOne")
.meter(); .meter();
}); });

View File

@ -39,8 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class MongoReactiveHealthIndicatorConfigurationTests { public class MongoReactiveHealthIndicatorConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class,
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class, MongoDataAutoConfiguration.class,
MongoReactiveAutoConfiguration.class, MongoReactiveAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class,
@ -49,10 +48,10 @@ public class MongoReactiveHealthIndicatorConfigurationTests {
@Test @Test
public void runShouldCreateIndicator() { public void runShouldCreateIndicator() {
this.contextRunner.run( this.contextRunner.run((context) -> assertThat(context)
(context) -> assertThat(context).hasSingleBean(MongoReactiveHealthIndicator.class) .hasSingleBean(MongoReactiveHealthIndicator.class)
.doesNotHaveBean(MongoHealthIndicator.class) .doesNotHaveBean(MongoHealthIndicator.class)
.doesNotHaveBean(ApplicationHealthIndicator.class)); .doesNotHaveBean(ApplicationHealthIndicator.class));
} }
@Test @Test

View File

@ -49,8 +49,8 @@ public class DataSourcePoolMetrics implements MeterBinder {
private final Iterable<Tag> tags; private final Iterable<Tag> tags;
public DataSourcePoolMetrics(DataSource dataSource, public DataSourcePoolMetrics(DataSource dataSource,
Collection<DataSourcePoolMetadataProvider> metadataProviders, String dataSourceName, Collection<DataSourcePoolMetadataProvider> metadataProviders,
Iterable<Tag> tags) { String dataSourceName, Iterable<Tag> tags) {
this(dataSource, new CompositeDataSourcePoolMetadataProvider(metadataProviders), this(dataSource, new CompositeDataSourcePoolMetadataProvider(metadataProviders),
dataSourceName, tags); dataSourceName, tags);
} }
@ -75,9 +75,10 @@ public class DataSourcePoolMetrics implements MeterBinder {
} }
} }
private <N extends Number> void bindPoolMetadata(MeterRegistry registry, String metricName, private <N extends Number> void bindPoolMetadata(MeterRegistry registry,
Function<DataSourcePoolMetadata, N> function) { String metricName, Function<DataSourcePoolMetadata, N> function) {
bindDataSource(registry, metricName, this.metadataProvider.getValueFunction(function)); bindDataSource(registry, metricName,
this.metadataProvider.getValueFunction(function));
} }
private <N extends Number> void bindDataSource(MeterRegistry registry, private <N extends Number> void bindDataSource(MeterRegistry registry,

View File

@ -43,8 +43,7 @@ public class RabbitMetricsTests {
public void connectionFactoryWithTagsIsInstrumented() { public void connectionFactoryWithTagsIsInstrumented() {
ConnectionFactory connectionFactory = mock(ConnectionFactory.class); ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
SimpleMeterRegistry registry = new SimpleMeterRegistry(); SimpleMeterRegistry registry = new SimpleMeterRegistry();
new RabbitMetrics(connectionFactory, Tags.of("env", "prod")) new RabbitMetrics(connectionFactory, Tags.of("env", "prod")).bindTo(registry);
.bindTo(registry);
assertThat(registry.get("rabbitmq.connections").tags("env", "prod").meter()) assertThat(registry.get("rabbitmq.connections").tags("env", "prod").meter())
.isNotNull(); .isNotNull();
assertThat(registry.find("rabbitmq.connections").tags("env", "dev").meter()) assertThat(registry.find("rabbitmq.connections").tags("env", "dev").meter())

View File

@ -50,8 +50,8 @@ public class DataSourcePoolMetricsTests {
"metrics.use-global-registry=false") "metrics.use-global-registry=false")
.run((context) -> { .run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData(); context.getBean(DataSource.class).getConnection().getMetaData();
context.getBean(MeterRegistry.class) context.getBean(MeterRegistry.class).get("jdbc.max.connections")
.get("jdbc.max.connections").meter(); .meter();
}); });
} }

View File

@ -42,10 +42,10 @@ public class MongoReactiveHealthIndicatorTest {
Document buildInfo = mock(Document.class); Document buildInfo = mock(Document.class);
given(buildInfo.getString("version")).willReturn("2.6.4"); given(buildInfo.getString("version")).willReturn("2.6.4");
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class); ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn( given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }"))
Mono.just(buildInfo)); .willReturn(Mono.just(buildInfo));
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(reactiveMongoTemplate); reactiveMongoTemplate);
Mono<Health> health = mongoReactiveHealthIndicator.health(); Mono<Health> health = mongoReactiveHealthIndicator.health();
StepVerifier.create(health).consumeNextWith((h) -> { StepVerifier.create(health).consumeNextWith((h) -> {
assertThat(h.getStatus()).isEqualTo(Status.UP); assertThat(h.getStatus()).isEqualTo(Status.UP);
@ -57,10 +57,10 @@ public class MongoReactiveHealthIndicatorTest {
@Test @Test
public void testMongoIsDown() { public void testMongoIsDown() {
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class); ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow( given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }"))
new MongoException("Connection failed")); .willThrow(new MongoException("Connection failed"));
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(reactiveMongoTemplate); reactiveMongoTemplate);
Mono<Health> health = mongoReactiveHealthIndicator.health(); Mono<Health> health = mongoReactiveHealthIndicator.health();
StepVerifier.create(health).consumeNextWith((h) -> { StepVerifier.create(health).consumeNextWith((h) -> {
assertThat(h.getStatus()).isEqualTo(Status.DOWN); assertThat(h.getStatus()).isEqualTo(Status.DOWN);

View File

@ -20,6 +20,7 @@ import java.util.Collections;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -189,29 +190,23 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
} }
/** /**
* Log the original exception if handling it results in a Server Error or a Bad Request * Log the original exception if handling it results in a Server Error or a Bad
* (Client Error with 400 status code) one. * Request (Client Error with 400 status code) one.
* @param request the source request * @param request the source request
* @param errorStatus the HTTP error status * @param errorStatus the HTTP error status
*/ */
protected void logError(ServerRequest request, HttpStatus errorStatus) { protected void logError(ServerRequest request, HttpStatus errorStatus) {
if (errorStatus.is5xxServerError()) { Throwable ex = getError(request);
Throwable ex = getError(request); log(request, ex, (errorStatus.is5xxServerError() ? logger::error : logger::warn));
if (ex instanceof ResponseStatusException) { }
logger.error(buildMessage(request, ex));
} private void log(ServerRequest request, Throwable ex,
else { BiConsumer<Object, Throwable> logger) {
logger.error(buildMessage(request, null), ex); if (ex instanceof ResponseStatusException) {
} logger.accept(buildMessage(request, ex), null);
} }
else if (errorStatus == HttpStatus.BAD_REQUEST) { else {
Throwable ex = getError(request); logger.accept(buildMessage(request, null), ex);
if (ex instanceof ResponseStatusException) {
logger.warn(buildMessage(request, ex));
}
else {
logger.warn(buildMessage(request, null), ex);
}
} }
} }