|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 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.metrics; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import io.micrometer.core.instrument.observation.MeterObservationHandler; |
| 24 | +import io.micrometer.core.instrument.observation.Observation.Context; |
| 25 | +import io.micrometer.core.instrument.observation.ObservationHandler; |
| 26 | +import io.micrometer.core.instrument.observation.ObservationHandler.AllMatchingCompositeObservationHandler; |
| 27 | +import io.micrometer.core.instrument.observation.ObservationHandler.CompositeObservationHandler; |
| 28 | +import io.micrometer.core.instrument.observation.ObservationHandler.FirstMatchingCompositeObservationHandler; |
| 29 | +import io.micrometer.core.instrument.observation.ObservationRegistry; |
| 30 | +import io.micrometer.core.instrument.observation.ObservationRegistry.ObservationConfig; |
| 31 | +import io.micrometer.core.instrument.observation.TimerObservationHandler; |
| 32 | +import io.micrometer.tracing.Tracer; |
| 33 | +import io.micrometer.tracing.handler.DefaultTracingObservationHandler; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; |
| 38 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 39 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.context.annotation.Configuration; |
| 42 | +import org.springframework.core.annotation.Order; |
| 43 | +import org.springframework.util.ReflectionUtils; |
| 44 | + |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests for {@link TracingAutoConfiguration}. |
| 49 | + * |
| 50 | + * @author Moritz Halbritter |
| 51 | + */ |
| 52 | +class TracingAutoConfigurationTests { |
| 53 | + |
| 54 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().with(MetricsRun.simple()) |
| 55 | + .withConfiguration(AutoConfigurations.of(TracingAutoConfiguration.class)); |
| 56 | + |
| 57 | + @Test |
| 58 | + @SuppressWarnings("rawtypes") |
| 59 | + void autoConfiguresObservationHandler() { |
| 60 | + this.contextRunner.withUserConfiguration(ObservationHandlers.class).run((context) -> { |
| 61 | + ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class); |
| 62 | + List<ObservationHandler<?>> handlers = getObservationHandlers(observationRegistry); |
| 63 | + assertThat(handlers).hasSize(6); |
| 64 | + assertThat(handlers.get(0)).isInstanceOf(TimerObservationHandler.class); |
| 65 | + assertThat(handlers.get(1)).isInstanceOf(CustomObservationHandler.class); |
| 66 | + assertThat(handlers.get(2)).isInstanceOf(FirstMatchingCompositeObservationHandler.class); |
| 67 | + assertThat(handlers.get(3)).isInstanceOf(AllMatchingCompositeObservationHandler.class); |
| 68 | + // CustomMeterObservationHandlers get wrapped into a |
| 69 | + // FirstMatchingCompositeObservationHandler |
| 70 | + assertThat(handlers.get(4)).isInstanceOf(FirstMatchingCompositeObservationHandler.class); |
| 71 | + List<ObservationHandler> containedHandlers = ((CompositeObservationHandler) handlers.get(4)).getHandlers(); |
| 72 | + assertThat(containedHandlers).hasSize(2); |
| 73 | + assertThat(containedHandlers.get(0)).isInstanceOf(CustomMeterObservationHandler.class); |
| 74 | + assertThat(containedHandlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class); |
| 75 | + assertThat(handlers.get(4)).isInstanceOf(FirstMatchingCompositeObservationHandler.class); |
| 76 | + // DefaultTracingObservationHandler get wrapped into a |
| 77 | + // FirstMatchingCompositeObservationHandler |
| 78 | + containedHandlers = ((CompositeObservationHandler) handlers.get(5)).getHandlers(); |
| 79 | + assertThat(containedHandlers).hasSize(2); |
| 80 | + assertThat(containedHandlers.get(0)).isInstanceOf(DefaultTracingObservationHandler.class); |
| 81 | + assertThat(containedHandlers.get(1)).isInstanceOf(DefaultTracingObservationHandler.class); |
| 82 | + |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + private List<ObservationHandler<?>> getObservationHandlers(ObservationRegistry registry) { |
| 88 | + Method method = ReflectionUtils.findMethod(ObservationConfig.class, "getObservationHandlers"); |
| 89 | + ReflectionUtils.makeAccessible(method); |
| 90 | + return List.copyOf( |
| 91 | + (Collection<ObservationHandler<?>>) ReflectionUtils.invokeMethod(method, registry.observationConfig())); |
| 92 | + } |
| 93 | + |
| 94 | + @Configuration(proxyBeanMethods = false) |
| 95 | + static class ObservationHandlers { |
| 96 | + |
| 97 | + @Bean |
| 98 | + @Order(6) |
| 99 | + DefaultTracingObservationHandler customTracingHandler2() { |
| 100 | + return new DefaultTracingObservationHandler(Mockito.mock(Tracer.class)); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + @Order(5) |
| 105 | + DefaultTracingObservationHandler customTracingHandler1() { |
| 106 | + return new DefaultTracingObservationHandler(Mockito.mock(Tracer.class)); |
| 107 | + } |
| 108 | + |
| 109 | + @Bean |
| 110 | + @Order(4) |
| 111 | + AllMatchingCompositeObservationHandler customAllMatchingCompositeObservationHandler() { |
| 112 | + return new AllMatchingCompositeObservationHandler(); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + @Order(3) |
| 117 | + FirstMatchingCompositeObservationHandler customFirstMatchingCompositeObservationHandler() { |
| 118 | + return new FirstMatchingCompositeObservationHandler(); |
| 119 | + } |
| 120 | + |
| 121 | + @Bean |
| 122 | + @Order(2) |
| 123 | + ObservationHandler<Context> customObservationHandler() { |
| 124 | + return new CustomObservationHandler(); |
| 125 | + } |
| 126 | + |
| 127 | + @Bean |
| 128 | + @Order(1) |
| 129 | + MeterObservationHandler<Context> customMeterObservationHandler2() { |
| 130 | + return new CustomMeterObservationHandler(); |
| 131 | + } |
| 132 | + |
| 133 | + @Bean |
| 134 | + @Order(0) |
| 135 | + MeterObservationHandler<Context> customMeterObservationHandler1() { |
| 136 | + return new CustomMeterObservationHandler(); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + static class CustomObservationHandler implements ObservationHandler<Context> { |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean supportsContext(Context context) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + static class CustomMeterObservationHandler implements MeterObservationHandler<Context> { |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean supportsContext(Context context) { |
| 154 | + return true; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments