|
| 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