Skip to content

Commit 7b90fbb

Browse files
committed
Add property to specify the order of ServerHttpObservationFilter
The property is named 'management.observations.http.server.filter.order' Closes gh-35067
1 parent e488e75 commit 7b90fbb

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationProperties.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121

2222
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.core.Ordered;
2324

2425
/**
2526
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Micrometer
@@ -96,10 +97,16 @@ public static class Server {
9697

9798
private final ServerRequests requests = new ServerRequests();
9899

100+
private final Filter filter = new Filter();
101+
99102
public ServerRequests getRequests() {
100103
return this.requests;
101104
}
102105

106+
public Filter getFilter() {
107+
return this.filter;
108+
}
109+
103110
public static class ServerRequests {
104111

105112
/**
@@ -118,6 +125,23 @@ public void setName(String name) {
118125

119126
}
120127

128+
public static class Filter {
129+
130+
/**
131+
* Order of the filter that creates the observations.
132+
*/
133+
private int order = Ordered.HIGHEST_PRECEDENCE + 1;
134+
135+
public int getOrder() {
136+
return this.order;
137+
}
138+
139+
public void setOrder(int order) {
140+
this.order = order;
141+
}
142+
143+
}
144+
121145
}
122146

123147
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation.web.reactive;
18+
19+
import io.micrometer.observation.ObservationRegistry;
20+
21+
import org.springframework.boot.web.reactive.filter.OrderedWebFilter;
22+
import org.springframework.core.Ordered;
23+
import org.springframework.http.server.reactive.observation.ServerRequestObservationConvention;
24+
import org.springframework.web.filter.reactive.ServerHttpObservationFilter;
25+
26+
/**
27+
* {@link ServerHttpObservationFilter} that also implements {@link Ordered}.
28+
*
29+
* @author Moritz Halbritter
30+
*/
31+
class OrderedServerHttpObservationFilter extends ServerHttpObservationFilter implements OrderedWebFilter {
32+
33+
private final int order;
34+
35+
OrderedServerHttpObservationFilter(ObservationRegistry observationRegistry,
36+
ServerRequestObservationConvention observationConvention, int order) {
37+
super(observationRegistry, observationConvention);
38+
this.order = order;
39+
}
40+
41+
@Override
42+
public int getOrder() {
43+
return this.order;
44+
}
45+
46+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/web/reactive/WebFluxObservationAutoConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4343
import org.springframework.context.annotation.Bean;
4444
import org.springframework.context.annotation.Configuration;
45-
import org.springframework.core.Ordered;
4645
import org.springframework.core.annotation.Order;
4746
import org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention;
4847
import org.springframework.http.server.reactive.observation.ServerRequestObservationConvention;
@@ -55,6 +54,7 @@
5554
* @author Brian Clozel
5655
* @author Jon Schneider
5756
* @author Dmytro Nosan
57+
* @author Moritz Halbritter
5858
* @since 3.0.0
5959
*/
6060
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
@@ -77,9 +77,8 @@ public WebFluxObservationAutoConfiguration(MetricsProperties metricsProperties,
7777
}
7878

7979
@Bean
80-
@ConditionalOnMissingBean
81-
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
82-
public ServerHttpObservationFilter webfluxObservationFilter(ObservationRegistry registry,
80+
@ConditionalOnMissingBean(ServerHttpObservationFilter.class)
81+
public OrderedServerHttpObservationFilter webfluxObservationFilter(ObservationRegistry registry,
8382
ObjectProvider<ServerRequestObservationConvention> customConvention,
8483
ObjectProvider<WebFluxTagsProvider> tagConfigurer,
8584
ObjectProvider<WebFluxTagsContributor> contributorsProvider) {
@@ -90,7 +89,8 @@ public ServerHttpObservationFilter webfluxObservationFilter(ObservationRegistry
9089
List<WebFluxTagsContributor> tagsContributors = contributorsProvider.orderedStream().toList();
9190
ServerRequestObservationConvention convention = createConvention(customConvention.getIfAvailable(), name,
9291
tagsProvider, tagsContributors);
93-
return new ServerHttpObservationFilter(registry, convention);
92+
int order = this.observationProperties.getHttp().getServer().getFilter().getOrder();
93+
return new OrderedServerHttpObservationFilter(registry, convention, order);
9494
}
9595

9696
private static ServerRequestObservationConvention createConvention(

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/web/reactive/WebFluxObservationAutoConfigurationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Brian Clozel
5858
* @author Dmytro Nosan
5959
* @author Madhura Bhave
60+
* @author Moritz Halbritter
6061
*/
6162
@ExtendWith(OutputCaptureExtension.class)
6263
@SuppressWarnings("removal")
@@ -169,6 +170,15 @@ void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput output) {
169170
});
170171
}
171172

173+
@Test
174+
void shouldUsePropertyForServerHttpObservationFilterOrder() {
175+
this.contextRunner.withPropertyValues("management.observations.http.server.filter.order=1000")
176+
.run((context) -> {
177+
OrderedServerHttpObservationFilter bean = context.getBean(OrderedServerHttpObservationFilter.class);
178+
assertThat(bean.getOrder()).isEqualTo(1000);
179+
});
180+
}
181+
172182
private MeterRegistry getInitializedMeterRegistry(AssertableReactiveWebApplicationContext context)
173183
throws Exception {
174184
return getInitializedMeterRegistry(context, "/test0", "/test1", "/test2");

0 commit comments

Comments
 (0)