|
| 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.tracing; |
| 18 | + |
| 19 | +import java.util.AbstractMap; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import brave.internal.propagation.StringPropagationAdapter; |
| 28 | +import brave.propagation.B3Propagation; |
| 29 | +import brave.propagation.Propagation; |
| 30 | +import brave.propagation.TraceContext; |
| 31 | +import brave.propagation.TraceContextOrSamplingFlags; |
| 32 | +import io.micrometer.tracing.brave.bridge.BraveBaggageManager; |
| 33 | +import io.micrometer.tracing.brave.bridge.W3CPropagation; |
| 34 | + |
| 35 | +import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Propagation.PropagationType; |
| 36 | + |
| 37 | +public class CompositePropagationFactory extends Propagation.Factory implements Propagation<String> { |
| 38 | + |
| 39 | + private final Map<PropagationType, Map.Entry<Factory, Propagation<String>>> mapping = new HashMap<>(); |
| 40 | + |
| 41 | + private final Set<PropagationType> types; |
| 42 | + |
| 43 | + private final Iterable<InjectorFactory> injectorFactories; |
| 44 | + |
| 45 | + private final Iterable<ExtractorFactory> extractorFactories; |
| 46 | + |
| 47 | + CompositePropagationFactory(Set<PropagationType> types, BraveBaggageManager braveBaggageManager, Iterable<InjectorFactory> injectorFactories, Iterable<ExtractorFactory> extractorFactories) { |
| 48 | + this.types = types; |
| 49 | + this.injectorFactories = injectorFactories; |
| 50 | + this.extractorFactories = extractorFactories; |
| 51 | + Factory b3Factory = b3Factory(); |
| 52 | + this.mapping.put(PropagationType.B3, new AbstractMap.SimpleEntry<>(b3Factory, b3Factory.get())); |
| 53 | + W3CPropagation w3cFactory = w3cFactory(braveBaggageManager); |
| 54 | + this.mapping.put(PropagationType.W3C, new AbstractMap.SimpleEntry<>(w3cFactory, w3cFactory.get())); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<String> keys() { |
| 59 | + return this.types.stream().map(this.mapping::get).flatMap(p -> p.getValue().keys().stream()) |
| 60 | + .collect(Collectors.toList()); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) { |
| 65 | + return (traceContext, request) -> this.types.stream().map(this.mapping::get) |
| 66 | + .forEach(p -> p.getValue().injector(setter).inject(traceContext, request)); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) { |
| 71 | + return request -> { |
| 72 | + for (PropagationType type : this.types) { |
| 73 | + Map.Entry<Factory, Propagation<String>> entry = this.mapping.get(type); |
| 74 | + if (entry == null) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + Propagation<String> propagator = entry.getValue(); |
| 78 | + if (propagator == null) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + TraceContextOrSamplingFlags extract = propagator.extractor(getter).extract(request); |
| 82 | + if (extract != TraceContextOrSamplingFlags.EMPTY) { |
| 83 | + return extract; |
| 84 | + } |
| 85 | + } |
| 86 | + return TraceContextOrSamplingFlags.EMPTY; |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + @SuppressWarnings("deprecation") |
| 92 | + public <K> Propagation<K> create(KeyFactory<K> keyFactory) { |
| 93 | + return StringPropagationAdapter.create(this, keyFactory); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean supportsJoin() { |
| 98 | + return this.types.stream().map(this.mapping::get).allMatch(e -> e.getKey().supportsJoin()); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean requires128BitTraceId() { |
| 103 | + return this.types.stream().map(this.mapping::get).allMatch(e -> e.getKey().requires128BitTraceId()); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public TraceContext decorate(TraceContext context) { |
| 108 | + for (PropagationType type : this.types) { |
| 109 | + Map.Entry<Factory, Propagation<String>> entry = this.mapping.get(type); |
| 110 | + if (entry == null) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + TraceContext decorate = entry.getKey().decorate(context); |
| 114 | + if (decorate != context) { |
| 115 | + return decorate; |
| 116 | + } |
| 117 | + } |
| 118 | + return super.decorate(context); |
| 119 | + } |
| 120 | + |
| 121 | + private static Factory b3Factory() { |
| 122 | + return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build(); |
| 123 | + } |
| 124 | + |
| 125 | + private static W3CPropagation w3cFactory(BraveBaggageManager braveBaggageManager) { |
| 126 | + return (braveBaggageManager != null) ? new W3CPropagation(braveBaggageManager, Collections.emptyList()) : new W3CPropagation(); |
| 127 | + } |
| 128 | + |
| 129 | + interface InjectorFactory { |
| 130 | + <R> TraceContext.Injector<R> injector(Setter<R, String> setter); |
| 131 | + } |
| 132 | + |
| 133 | + interface ExtractorFactory { |
| 134 | + <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments