16
16
17
17
package org .springframework .web .filter ;
18
18
19
+ import io .micrometer .observation .Observation ;
19
20
import io .micrometer .observation .ObservationRegistry ;
20
21
import io .micrometer .observation .tck .TestObservationRegistry ;
21
22
import io .micrometer .observation .tck .TestObservationRegistryAssert ;
22
23
import jakarta .servlet .RequestDispatcher ;
23
24
import jakarta .servlet .ServletException ;
25
+ import jakarta .servlet .http .HttpServletRequest ;
26
+ import jakarta .servlet .http .HttpServletResponse ;
24
27
import org .junit .jupiter .api .Test ;
25
28
26
29
import org .springframework .http .HttpMethod ;
27
30
import org .springframework .http .server .observation .ServerRequestObservationContext ;
31
+ import org .springframework .util .Assert ;
28
32
import org .springframework .web .testfixture .servlet .MockFilterChain ;
29
33
import org .springframework .web .testfixture .servlet .MockHttpServletRequest ;
30
34
import org .springframework .web .testfixture .servlet .MockHttpServletResponse ;
@@ -41,14 +45,14 @@ class ServerHttpObservationFilterTests {
41
45
42
46
private final TestObservationRegistry observationRegistry = TestObservationRegistry .create ();
43
47
44
- private final ServerHttpObservationFilter filter = new ServerHttpObservationFilter (this .observationRegistry );
45
-
46
48
private final MockFilterChain mockFilterChain = new MockFilterChain ();
47
49
48
50
private final MockHttpServletRequest request = new MockHttpServletRequest (HttpMethod .GET .name (), "/resource/test" );
49
51
50
52
private final MockHttpServletResponse response = new MockHttpServletResponse ();
51
53
54
+ private ServerHttpObservationFilter filter = new ServerHttpObservationFilter (this .observationRegistry );
55
+
52
56
53
57
@ Test
54
58
void filterShouldFillObservationContext () throws Exception {
@@ -65,7 +69,7 @@ void filterShouldFillObservationContext() throws Exception {
65
69
66
70
@ Test
67
71
void filterShouldAcceptNoOpObservationContext () throws Exception {
68
- ServerHttpObservationFilter filter = new ServerHttpObservationFilter (ObservationRegistry .NOOP );
72
+ this . filter = new ServerHttpObservationFilter (ObservationRegistry .NOOP );
69
73
filter .doFilter (this .request , this .response , this .mockFilterChain );
70
74
71
75
ServerRequestObservationContext context = (ServerRequestObservationContext ) this .request
@@ -109,9 +113,32 @@ void filterShouldSetDefaultErrorStatusForBubblingExceptions() {
109
113
.hasLowCardinalityKeyValue ("status" , "500" );
110
114
}
111
115
116
+ @ Test
117
+ void customFilterShouldCallScopeOpened () throws Exception {
118
+ this .filter = new CustomObservationFilter (this .observationRegistry );
119
+ this .filter .doFilter (this .request , this .response , this .mockFilterChain );
120
+
121
+ assertThat (this .response .getHeader ("X-Trace-Id" )).isEqualTo ("badc0ff33" );
122
+ }
123
+
112
124
private TestObservationRegistryAssert .TestObservationRegistryAssertReturningObservationContextAssert assertThatHttpObservation () {
113
125
return TestObservationRegistryAssert .assertThat (this .observationRegistry )
114
126
.hasObservationWithNameEqualTo ("http.server.requests" ).that ();
115
127
}
116
128
129
+ static class CustomObservationFilter extends ServerHttpObservationFilter {
130
+
131
+ public CustomObservationFilter (ObservationRegistry observationRegistry ) {
132
+ super (observationRegistry );
133
+ }
134
+
135
+ @ Override
136
+ protected void onScopeOpened (Observation .Scope scope , HttpServletRequest request , HttpServletResponse response ) {
137
+ Assert .notNull (scope , "scope must not be null" );
138
+ Assert .notNull (request , "request must not be null" );
139
+ Assert .notNull (response , "response must not be null" );
140
+ response .setHeader ("X-Trace-Id" , "badc0ff33" );
141
+ }
142
+ }
143
+
117
144
}
0 commit comments