@ -16,12 +16,12 @@
package org.springframework.boot.actuate.autoconfigure.observation.web.reactive ;
import java.util.List ;
import java.time.Duration ;
import java.time.temporal.ChronoUnit ;
import io.micrometer.core.instrument.MeterRegistry ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.extension.ExtendWith ;
import reactor.core.publisher.Mono ;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration ;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun ;
@ -33,16 +33,6 @@ import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplic
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner ;
import org.springframework.boot.test.system.CapturedOutput ;
import org.springframework.boot.test.system.OutputCaptureExtension ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.Ordered ;
import org.springframework.core.annotation.Order ;
import org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention ;
import org.springframework.test.web.reactive.server.WebTestClient ;
import org.springframework.web.filter.reactive.ServerHttpObservationFilter ;
import org.springframework.web.server.ServerWebExchange ;
import org.springframework.web.server.WebFilter ;
import org.springframework.web.server.WebFilterChain ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -54,7 +44,6 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Madhura Bhave
* /
@ExtendWith ( OutputCaptureExtension . class )
@SuppressWarnings ( "removal" )
class WebFluxObservationAutoConfigurationTests {
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner ( )
@ -62,31 +51,6 @@ class WebFluxObservationAutoConfigurationTests {
. withConfiguration (
AutoConfigurations . of ( ObservationAutoConfiguration . class , WebFluxObservationAutoConfiguration . class ) ) ;
@Test
void shouldProvideWebFluxObservationFilter ( ) {
this . contextRunner . run ( ( context ) - > assertThat ( context ) . hasSingleBean ( ServerHttpObservationFilter . class ) ) ;
}
@Test
void shouldProvideWebFluxObservationFilterOrdered ( ) {
this . contextRunner . withBean ( FirstWebFilter . class ) . withBean ( ThirdWebFilter . class ) . run ( ( context ) - > {
List < WebFilter > webFilters = context . getBeanProvider ( WebFilter . class ) . orderedStream ( ) . toList ( ) ;
assertThat ( webFilters . get ( 0 ) ) . isInstanceOf ( FirstWebFilter . class ) ;
assertThat ( webFilters . get ( 1 ) ) . isInstanceOf ( ServerHttpObservationFilter . class ) ;
assertThat ( webFilters . get ( 2 ) ) . isInstanceOf ( ThirdWebFilter . class ) ;
} ) ;
}
@Test
void shouldUseCustomConventionWhenAvailable ( ) {
this . contextRunner . withUserConfiguration ( CustomConventionConfiguration . class ) . run ( ( context ) - > {
assertThat ( context ) . hasSingleBean ( ServerHttpObservationFilter . class ) ;
assertThat ( context ) . getBean ( ServerHttpObservationFilter . class )
. extracting ( "observationConvention" )
. isInstanceOf ( CustomConvention . class ) ;
} ) ;
}
@Test
void afterMaxUrisReachedFurtherUrisAreDenied ( CapturedOutput output ) {
this . contextRunner . withUserConfiguration ( TestController . class )
@ -108,7 +72,7 @@ class WebFluxObservationAutoConfigurationTests {
. withPropertyValues ( "management.metrics.web.server.max-uri-tags=2" ,
"management.observations.http.server.requests.name=my.http.server.requests" )
. run ( ( context ) - > {
MeterRegistry registry = getInitializedMeterRegistry ( context );
MeterRegistry registry = getInitializedMeterRegistry ( context , "my.http.server.requests" );
assertThat ( registry . get ( "my.http.server.requests" ) . meters ( ) ) . hasSizeLessThanOrEqualTo ( 2 ) ;
assertThat ( output ) . contains ( "Reached the maximum number of URI tags for 'my.http.server.requests'" ) ;
} ) ;
@ -127,53 +91,17 @@ class WebFluxObservationAutoConfigurationTests {
} ) ;
}
private MeterRegistry getInitializedMeterRegistry ( AssertableReactiveWebApplicationContext context )
throws Exception {
return getInitializedMeterRegistry ( context , "/test0" , "/test1" , "/test2" ) ;
private MeterRegistry getInitializedMeterRegistry ( AssertableReactiveWebApplicationContext context ) {
return getInitializedMeterRegistry ( context , "http.server.requests" ) ;
}
private MeterRegistry getInitializedMeterRegistry ( AssertableReactiveWebApplicationContext context , String . . . urls )
throws Exception {
assertThat ( context ) . hasSingleBean ( ServerHttpObservationFilter . class ) ;
WebTestClient client = WebTestClient . bindToApplicationContext ( context ) . build ( ) ;
for ( String url : urls ) {
client . get ( ) . uri ( url ) . exchange ( ) . expectStatus ( ) . isOk ( ) ;
}
return context . getBean ( MeterRegistry . class ) ;
}
@Configuration ( proxyBeanMethods = false )
static class CustomConventionConfiguration {
@Bean
CustomConvention customConvention ( ) {
return new CustomConvention ( ) ;
}
}
static class CustomConvention extends DefaultServerRequestObservationConvention {
}
@Order ( Ordered . HIGHEST_PRECEDENCE )
static class FirstWebFilter implements WebFilter {
@Override
public Mono < Void > filter ( ServerWebExchange exchange , WebFilterChain chain ) {
return chain . filter ( exchange ) ;
}
}
@Order ( Ordered . HIGHEST_PRECEDENCE + 2 )
static class ThirdWebFilter implements WebFilter {
@Override
public Mono < Void > filter ( ServerWebExchange exchange , WebFilterChain chain ) {
return chain . filter ( exchange ) ;
}
private MeterRegistry getInitializedMeterRegistry ( AssertableReactiveWebApplicationContext context ,
String metricName ) {
MeterRegistry meterRegistry = context . getBean ( MeterRegistry . class ) ;
meterRegistry . timer ( metricName , "uri" , "/test0" ) . record ( Duration . of ( 500 , ChronoUnit . SECONDS ) ) ;
meterRegistry . timer ( metricName , "uri" , "/test1" ) . record ( Duration . of ( 500 , ChronoUnit . SECONDS ) ) ;
meterRegistry . timer ( metricName , "uri" , "/test2" ) . record ( Duration . of ( 500 , ChronoUnit . SECONDS ) ) ;
return meterRegistry ;
}
}