Skip to content

Commit 6e86f5c

Browse files
committed
Register uncategorized ObservationHandlers after categorized ones
Closes gh-34399
1 parent 5bad242 commit 6e86f5c

File tree

3 files changed

+146
-17
lines changed

3 files changed

+146
-17
lines changed

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationHandlerGrouping.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.observation;
1818

19+
import java.util.ArrayList;
1920
import java.util.List;
2021

2122
import io.micrometer.observation.ObservationHandler;
@@ -30,6 +31,7 @@
3031
* Groups {@link ObservationHandler ObservationHandlers} by type.
3132
*
3233
* @author Andy Wilkinson
34+
* @author Moritz Halbritter
3335
*/
3436
@SuppressWarnings("rawtypes")
3537
class ObservationHandlerGrouping {
@@ -46,13 +48,14 @@ class ObservationHandlerGrouping {
4648

4749
void apply(List<ObservationHandler<?>> handlers, ObservationConfig config) {
4850
MultiValueMap<Class<? extends ObservationHandler>, ObservationHandler<?>> groupings = new LinkedMultiValueMap<>();
51+
List<ObservationHandler<?>> handlersWithoutCategory = new ArrayList<>();
4952
for (ObservationHandler<?> handler : handlers) {
5053
Class<? extends ObservationHandler> category = findCategory(handler);
5154
if (category != null) {
5255
groupings.add(category, handler);
5356
}
5457
else {
55-
config.observationHandler(handler);
58+
handlersWithoutCategory.add(handler);
5659
}
5760
}
5861
for (Class<? extends ObservationHandler> category : this.categories) {
@@ -61,6 +64,9 @@ void apply(List<ObservationHandler<?>> handlers, ObservationConfig config) {
6164
config.observationHandler(new FirstMatchingCompositeObservationHandler(handlerGroup));
6265
}
6366
}
67+
for (ObservationHandler<?> observationHandler : handlersWithoutCategory) {
68+
config.observationHandler(observationHandler);
69+
}
6470
}
6571

6672
private Class<? extends ObservationHandler> findCategory(ObservationHandler<?> handler) {

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,13 @@ void autoConfiguresObservationHandlers() {
223223
Observation.start("test-observation", observationRegistry).stop();
224224
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
225225
assertThat(handlers).hasSize(2);
226-
// Regular handlers are registered first
227-
assertThat(handlers.get(0)).isInstanceOf(CustomObservationHandler.class);
228226
// Multiple MeterObservationHandler are wrapped in
229-
// FirstMatchingCompositeObservationHandler, which calls only the first
230-
// one
231-
assertThat(handlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class);
232-
assertThat(((CustomMeterObservationHandler) handlers.get(1)).getName())
227+
// FirstMatchingCompositeObservationHandler, which calls only the first one
228+
assertThat(handlers.get(0)).isInstanceOf(CustomMeterObservationHandler.class);
229+
assertThat(((CustomMeterObservationHandler) handlers.get(0)).getName())
233230
.isEqualTo("customMeterObservationHandler1");
231+
// Regular handlers are registered last
232+
assertThat(handlers.get(1)).isInstanceOf(CustomObservationHandler.class);
234233
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
235234
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
236235
});
@@ -273,20 +272,18 @@ void autoConfiguresObservationHandlerWhenTracingIsActive() {
273272
List<ObservationHandler<?>> handlers = context.getBean(CalledHandlers.class).getCalledHandlers();
274273
Observation.start("test-observation", observationRegistry).stop();
275274
assertThat(handlers).hasSize(3);
276-
// Regular handlers are registered first
277-
assertThat(handlers.get(0)).isInstanceOf(CustomObservationHandler.class);
278275
// Multiple TracingObservationHandler are wrapped in
279-
// FirstMatchingCompositeObservationHandler, which calls only the first
280-
// one
281-
assertThat(handlers.get(1)).isInstanceOf(CustomTracingObservationHandler.class);
282-
assertThat(((CustomTracingObservationHandler) handlers.get(1)).getName())
276+
// FirstMatchingCompositeObservationHandler, which calls only the first one
277+
assertThat(handlers.get(0)).isInstanceOf(CustomTracingObservationHandler.class);
278+
assertThat(((CustomTracingObservationHandler) handlers.get(0)).getName())
283279
.isEqualTo("customTracingHandler1");
284280
// Multiple MeterObservationHandler are wrapped in
285-
// FirstMatchingCompositeObservationHandler, which calls only the first
286-
// one
287-
assertThat(handlers.get(2)).isInstanceOf(CustomMeterObservationHandler.class);
288-
assertThat(((CustomMeterObservationHandler) handlers.get(2)).getName())
281+
// FirstMatchingCompositeObservationHandler, which calls only the first one
282+
assertThat(handlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class);
283+
assertThat(((CustomMeterObservationHandler) handlers.get(1)).getName())
289284
.isEqualTo("customMeterObservationHandler1");
285+
// Regular handlers are registered last
286+
assertThat(handlers.get(2)).isInstanceOf(CustomObservationHandler.class);
290287
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
291288
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
292289
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.List;
21+
22+
import io.micrometer.observation.Observation;
23+
import io.micrometer.observation.Observation.Context;
24+
import io.micrometer.observation.ObservationHandler;
25+
import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler;
26+
import io.micrometer.observation.ObservationRegistry.ObservationConfig;
27+
import org.junit.jupiter.api.Test;
28+
29+
import org.springframework.util.ReflectionUtils;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link ObservationHandlerGrouping}.
35+
*
36+
* @author Moritz Halbritter
37+
*/
38+
class ObservationHandlerGroupingTests {
39+
40+
@Test
41+
void shouldGroupCategoriesIntoFirstMatchingHandlerAndRespectsCategoryOrder() {
42+
ObservationHandlerGrouping grouping = new ObservationHandlerGrouping(
43+
List.of(ObservationHandlerA.class, ObservationHandlerB.class));
44+
ObservationConfig config = new ObservationConfig();
45+
ObservationHandlerA handlerA1 = new ObservationHandlerA("a1");
46+
ObservationHandlerA handlerA2 = new ObservationHandlerA("a2");
47+
ObservationHandlerB handlerB1 = new ObservationHandlerB("b1");
48+
ObservationHandlerB handlerB2 = new ObservationHandlerB("b2");
49+
grouping.apply(List.of(handlerB1, handlerB2, handlerA1, handlerA2), config);
50+
List<ObservationHandler<?>> handlers = getObservationHandlers(config);
51+
assertThat(handlers).hasSize(2);
52+
// Category A is first
53+
assertThat(handlers.get(0)).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
54+
FirstMatchingCompositeObservationHandler firstMatching0 = (FirstMatchingCompositeObservationHandler) handlers
55+
.get(0);
56+
assertThat(firstMatching0.getHandlers()).containsExactly(handlerA1, handlerA2);
57+
// Category B is second
58+
assertThat(handlers.get(1)).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
59+
FirstMatchingCompositeObservationHandler firstMatching1 = (FirstMatchingCompositeObservationHandler) handlers
60+
.get(1);
61+
assertThat(firstMatching1.getHandlers()).containsExactly(handlerB1, handlerB2);
62+
}
63+
64+
@Test
65+
void uncategorizedHandlersShouldBeOrderedAfterCategories() {
66+
ObservationHandlerGrouping grouping = new ObservationHandlerGrouping(ObservationHandlerA.class);
67+
ObservationConfig config = new ObservationConfig();
68+
ObservationHandlerA handlerA1 = new ObservationHandlerA("a1");
69+
ObservationHandlerA handlerA2 = new ObservationHandlerA("a2");
70+
ObservationHandlerB handlerB1 = new ObservationHandlerB("b1");
71+
grouping.apply(List.of(handlerB1, handlerA1, handlerA2), config);
72+
List<ObservationHandler<?>> handlers = getObservationHandlers(config);
73+
assertThat(handlers).hasSize(2);
74+
// Category A is first
75+
assertThat(handlers.get(0)).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
76+
FirstMatchingCompositeObservationHandler firstMatching0 = (FirstMatchingCompositeObservationHandler) handlers
77+
.get(0);
78+
// Uncategorized handlers follow
79+
assertThat(firstMatching0.getHandlers()).containsExactly(handlerA1, handlerA2);
80+
assertThat(handlers.get(1)).isEqualTo(handlerB1);
81+
}
82+
83+
@SuppressWarnings("unchecked")
84+
private static List<ObservationHandler<?>> getObservationHandlers(ObservationConfig config) {
85+
Method method = ReflectionUtils.findMethod(ObservationConfig.class, "getObservationHandlers");
86+
ReflectionUtils.makeAccessible(method);
87+
return (List<ObservationHandler<?>>) ReflectionUtils.invokeMethod(method, config);
88+
}
89+
90+
private static class NamedObservationHandler implements ObservationHandler<Observation.Context> {
91+
92+
private final String name;
93+
94+
NamedObservationHandler(String name) {
95+
this.name = name;
96+
}
97+
98+
@Override
99+
public boolean supportsContext(Context context) {
100+
return true;
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return getClass().getSimpleName() + "{name='" + this.name + "'}";
106+
}
107+
108+
}
109+
110+
private static class ObservationHandlerA extends NamedObservationHandler {
111+
112+
ObservationHandlerA(String name) {
113+
super(name);
114+
}
115+
116+
}
117+
118+
private static class ObservationHandlerB extends NamedObservationHandler {
119+
120+
ObservationHandlerB(String name) {
121+
super(name);
122+
}
123+
124+
}
125+
126+
}

0 commit comments

Comments
 (0)