|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 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.tracing; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import io.micrometer.core.instrument.observation.MeterObservationHandler; |
| 23 | +import io.micrometer.observation.ObservationHandler; |
| 24 | +import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler; |
| 25 | +import io.micrometer.observation.ObservationRegistry.ObservationConfig; |
| 26 | +import io.micrometer.tracing.Tracer; |
| 27 | +import io.micrometer.tracing.handler.TracingAwareMeterObservationHandler; |
| 28 | +import io.micrometer.tracing.handler.TracingObservationHandler; |
| 29 | + |
| 30 | +import org.springframework.boot.observation.autoconfigure.ObservationHandlerGroup; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link ObservationHandlerGroup} that considers both {@link TracingObservationHandler} |
| 34 | + * and {@link MeterObservationHandler} types as members. This group takes precedence over |
| 35 | + * any regular {@link MeterObservationHandler} group in order to use ensure |
| 36 | + * {@link TracingAwareMeterObservationHandler} wrapping is applied during registration. |
| 37 | + * |
| 38 | + * @author Phillip Webb |
| 39 | + */ |
| 40 | +class TracingAndMeterObservationHandlerGroup implements ObservationHandlerGroup { |
| 41 | + |
| 42 | + private final Tracer tracer; |
| 43 | + |
| 44 | + TracingAndMeterObservationHandlerGroup(Tracer tracer) { |
| 45 | + this.tracer = tracer; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isMember(ObservationHandler<?> handler) { |
| 50 | + return MeterObservationHandler.class.isInstance(handler) || TracingObservationHandler.class.isInstance(handler); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public int compareTo(ObservationHandlerGroup other) { |
| 55 | + if (other instanceof TracingAndMeterObservationHandlerGroup) { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + return MeterObservationHandler.class.isAssignableFrom(other.handlerType()) ? -1 : 1; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void registerMembers(ObservationConfig config, List<ObservationHandler<?>> members) { |
| 63 | + List<ObservationHandler<?>> tracingHandlers = new ArrayList<>(members.size()); |
| 64 | + List<ObservationHandler<?>> metricsHandlers = new ArrayList<>(members.size()); |
| 65 | + for (ObservationHandler<?> member : members) { |
| 66 | + if (member instanceof MeterObservationHandler<?> meterObservationHandler |
| 67 | + && !(member instanceof TracingAwareMeterObservationHandler<?>)) { |
| 68 | + metricsHandlers.add(new TracingAwareMeterObservationHandler<>(meterObservationHandler, this.tracer)); |
| 69 | + } |
| 70 | + else { |
| 71 | + tracingHandlers.add(member); |
| 72 | + } |
| 73 | + } |
| 74 | + registerHandlers(config, tracingHandlers); |
| 75 | + registerHandlers(config, metricsHandlers); |
| 76 | + } |
| 77 | + |
| 78 | + private void registerHandlers(ObservationConfig config, List<ObservationHandler<?>> handlers) { |
| 79 | + if (handlers.size() == 1) { |
| 80 | + config.observationHandler(handlers.get(0)); |
| 81 | + } |
| 82 | + else if (!handlers.isEmpty()) { |
| 83 | + config.observationHandler(new FirstMatchingCompositeObservationHandler(handlers)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Class<?> handlerType() { |
| 89 | + return TracingObservationHandler.class; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments