16
16
17
17
package org .springframework .boot .actuate .autoconfigure .observation .web .reactive ;
18
18
19
- import java .util .List ;
19
+ import java .time .Duration ;
20
+ import java .time .temporal .ChronoUnit ;
20
21
21
22
import io .micrometer .core .instrument .MeterRegistry ;
22
23
import org .junit .jupiter .api .Test ;
23
24
import org .junit .jupiter .api .extension .ExtendWith ;
24
- import reactor .core .publisher .Mono ;
25
25
26
26
import org .springframework .boot .actuate .autoconfigure .metrics .MetricsAutoConfiguration ;
27
27
import org .springframework .boot .actuate .autoconfigure .metrics .test .MetricsRun ;
33
33
import org .springframework .boot .test .context .runner .ReactiveWebApplicationContextRunner ;
34
34
import org .springframework .boot .test .system .CapturedOutput ;
35
35
import org .springframework .boot .test .system .OutputCaptureExtension ;
36
- import org .springframework .context .annotation .Bean ;
37
- import org .springframework .context .annotation .Configuration ;
38
- import org .springframework .core .Ordered ;
39
- import org .springframework .core .annotation .Order ;
40
- import org .springframework .http .server .reactive .observation .DefaultServerRequestObservationConvention ;
41
- import org .springframework .test .web .reactive .server .WebTestClient ;
42
- import org .springframework .web .filter .reactive .ServerHttpObservationFilter ;
43
- import org .springframework .web .server .ServerWebExchange ;
44
- import org .springframework .web .server .WebFilter ;
45
- import org .springframework .web .server .WebFilterChain ;
46
36
47
37
import static org .assertj .core .api .Assertions .assertThat ;
48
38
54
44
* @author Madhura Bhave
55
45
*/
56
46
@ ExtendWith (OutputCaptureExtension .class )
57
- @ SuppressWarnings ("removal" )
58
47
class WebFluxObservationAutoConfigurationTests {
59
48
60
49
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner ()
61
50
.with (MetricsRun .simple ())
62
51
.withConfiguration (
63
52
AutoConfigurations .of (ObservationAutoConfiguration .class , WebFluxObservationAutoConfiguration .class ));
64
53
65
- @ Test
66
- void shouldProvideWebFluxObservationFilter () {
67
- this .contextRunner .run ((context ) -> assertThat (context ).hasSingleBean (ServerHttpObservationFilter .class ));
68
- }
69
-
70
- @ Test
71
- void shouldProvideWebFluxObservationFilterOrdered () {
72
- this .contextRunner .withBean (FirstWebFilter .class ).withBean (ThirdWebFilter .class ).run ((context ) -> {
73
- List <WebFilter > webFilters = context .getBeanProvider (WebFilter .class ).orderedStream ().toList ();
74
- assertThat (webFilters .get (0 )).isInstanceOf (FirstWebFilter .class );
75
- assertThat (webFilters .get (1 )).isInstanceOf (ServerHttpObservationFilter .class );
76
- assertThat (webFilters .get (2 )).isInstanceOf (ThirdWebFilter .class );
77
- });
78
- }
79
-
80
- @ Test
81
- void shouldUseCustomConventionWhenAvailable () {
82
- this .contextRunner .withUserConfiguration (CustomConventionConfiguration .class ).run ((context ) -> {
83
- assertThat (context ).hasSingleBean (ServerHttpObservationFilter .class );
84
- assertThat (context ).getBean (ServerHttpObservationFilter .class )
85
- .extracting ("observationConvention" )
86
- .isInstanceOf (CustomConvention .class );
87
- });
88
- }
89
-
90
54
@ Test
91
55
void afterMaxUrisReachedFurtherUrisAreDenied (CapturedOutput output ) {
92
56
this .contextRunner .withUserConfiguration (TestController .class )
@@ -108,7 +72,7 @@ void afterMaxUrisReachedFurtherUrisAreDeniedWhenUsingCustomObservationName(Captu
108
72
.withPropertyValues ("management.metrics.web.server.max-uri-tags=2" ,
109
73
"management.observations.http.server.requests.name=my.http.server.requests" )
110
74
.run ((context ) -> {
111
- MeterRegistry registry = getInitializedMeterRegistry (context );
75
+ MeterRegistry registry = getInitializedMeterRegistry (context , "my.http.server.requests" );
112
76
assertThat (registry .get ("my.http.server.requests" ).meters ()).hasSizeLessThanOrEqualTo (2 );
113
77
assertThat (output ).contains ("Reached the maximum number of URI tags for 'my.http.server.requests'" );
114
78
});
@@ -127,53 +91,17 @@ void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput output) {
127
91
});
128
92
}
129
93
130
- private MeterRegistry getInitializedMeterRegistry (AssertableReactiveWebApplicationContext context )
131
- throws Exception {
132
- return getInitializedMeterRegistry (context , "/test0" , "/test1" , "/test2" );
94
+ private MeterRegistry getInitializedMeterRegistry (AssertableReactiveWebApplicationContext context ) {
95
+ return getInitializedMeterRegistry (context , "http.server.requests" );
133
96
}
134
97
135
- private MeterRegistry getInitializedMeterRegistry (AssertableReactiveWebApplicationContext context , String ... urls )
136
- throws Exception {
137
- assertThat (context ).hasSingleBean (ServerHttpObservationFilter .class );
138
- WebTestClient client = WebTestClient .bindToApplicationContext (context ).build ();
139
- for (String url : urls ) {
140
- client .get ().uri (url ).exchange ().expectStatus ().isOk ();
141
- }
142
- return context .getBean (MeterRegistry .class );
143
- }
144
-
145
- @ Configuration (proxyBeanMethods = false )
146
- static class CustomConventionConfiguration {
147
-
148
- @ Bean
149
- CustomConvention customConvention () {
150
- return new CustomConvention ();
151
- }
152
-
153
- }
154
-
155
- static class CustomConvention extends DefaultServerRequestObservationConvention {
156
-
157
- }
158
-
159
- @ Order (Ordered .HIGHEST_PRECEDENCE )
160
- static class FirstWebFilter implements WebFilter {
161
-
162
- @ Override
163
- public Mono <Void > filter (ServerWebExchange exchange , WebFilterChain chain ) {
164
- return chain .filter (exchange );
165
- }
166
-
167
- }
168
-
169
- @ Order (Ordered .HIGHEST_PRECEDENCE + 2 )
170
- static class ThirdWebFilter implements WebFilter {
171
-
172
- @ Override
173
- public Mono <Void > filter (ServerWebExchange exchange , WebFilterChain chain ) {
174
- return chain .filter (exchange );
175
- }
176
-
98
+ private MeterRegistry getInitializedMeterRegistry (AssertableReactiveWebApplicationContext context ,
99
+ String metricName ) {
100
+ MeterRegistry meterRegistry = context .getBean (MeterRegistry .class );
101
+ meterRegistry .timer (metricName , "uri" , "/test0" ).record (Duration .of (500 , ChronoUnit .SECONDS ));
102
+ meterRegistry .timer (metricName , "uri" , "/test1" ).record (Duration .of (500 , ChronoUnit .SECONDS ));
103
+ meterRegistry .timer (metricName , "uri" , "/test2" ).record (Duration .of (500 , ChronoUnit .SECONDS ));
104
+ return meterRegistry ;
177
105
}
178
106
179
107
}
0 commit comments