|
| 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.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import brave.internal.propagation.StringPropagationAdapter; |
| 25 | +import brave.propagation.B3Propagation; |
| 26 | +import brave.propagation.Propagation; |
| 27 | +import brave.propagation.TraceContext; |
| 28 | +import brave.propagation.TraceContextOrSamplingFlags; |
| 29 | +import io.micrometer.tracing.BaggageManager; |
| 30 | +import io.micrometer.tracing.brave.bridge.W3CPropagation; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link Factory} which supports multiple tracing formats. It is able to configure |
| 34 | + * different formats for injecting and for extracting. |
| 35 | + * |
| 36 | + * @author Marcin Grzejszczak |
| 37 | + * @author Moritz Halbritter |
| 38 | + */ |
| 39 | +class CompositePropagationFactory extends Propagation.Factory implements Propagation<String> { |
| 40 | + |
| 41 | + private final Collection<Propagation.Factory> injectorFactories; |
| 42 | + |
| 43 | + private final Collection<Propagation.Factory> extractorFactories; |
| 44 | + |
| 45 | + private final List<Propagation<String>> injectors; |
| 46 | + |
| 47 | + private final List<Propagation<String>> extractors; |
| 48 | + |
| 49 | + private final boolean supportsJoin; |
| 50 | + |
| 51 | + private final boolean requires128BitTraceId; |
| 52 | + |
| 53 | + private final List<String> keys; |
| 54 | + |
| 55 | + CompositePropagationFactory(Collection<Factory> injectorFactories, Collection<Factory> extractorFactories) { |
| 56 | + this.injectorFactories = injectorFactories; |
| 57 | + this.extractorFactories = extractorFactories; |
| 58 | + this.injectors = this.injectorFactories.stream().map(Factory::get).toList(); |
| 59 | + this.extractors = this.extractorFactories.stream().map(Factory::get).toList(); |
| 60 | + this.supportsJoin = Stream.concat(this.injectorFactories.stream(), this.extractorFactories.stream()) |
| 61 | + .allMatch(Factory::supportsJoin); |
| 62 | + this.requires128BitTraceId = Stream.concat(this.injectorFactories.stream(), this.extractorFactories.stream()) |
| 63 | + .anyMatch(Factory::requires128BitTraceId); |
| 64 | + this.keys = Stream.concat(this.injectors.stream(), this.extractors.stream()) |
| 65 | + .flatMap((entry) -> entry.keys().stream()) |
| 66 | + .distinct() |
| 67 | + .toList(); |
| 68 | + } |
| 69 | + |
| 70 | + Collection<Factory> getInjectorFactories() { |
| 71 | + return this.injectorFactories; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public List<String> keys() { |
| 76 | + return this.keys; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) { |
| 81 | + return (traceContext, request) -> { |
| 82 | + for (Propagation<String> injector : this.injectors) { |
| 83 | + injector.injector(setter).inject(traceContext, request); |
| 84 | + } |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) { |
| 90 | + return (request) -> { |
| 91 | + for (Propagation<String> extractor : this.extractors) { |
| 92 | + TraceContextOrSamplingFlags extract = extractor.extractor(getter).extract(request); |
| 93 | + if (extract != TraceContextOrSamplingFlags.EMPTY) { |
| 94 | + return extract; |
| 95 | + } |
| 96 | + } |
| 97 | + return TraceContextOrSamplingFlags.EMPTY; |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + @SuppressWarnings("deprecation") |
| 103 | + public <K> Propagation<K> create(KeyFactory<K> keyFactory) { |
| 104 | + return StringPropagationAdapter.create(this, keyFactory); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean supportsJoin() { |
| 109 | + return this.supportsJoin; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean requires128BitTraceId() { |
| 114 | + return this.requires128BitTraceId; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public TraceContext decorate(TraceContext context) { |
| 119 | + for (Factory injectorFactory : this.injectorFactories) { |
| 120 | + TraceContext decorated = injectorFactory.decorate(context); |
| 121 | + if (decorated != context) { |
| 122 | + return decorated; |
| 123 | + } |
| 124 | + } |
| 125 | + for (Factory extractorFactory : this.extractorFactories) { |
| 126 | + TraceContext decorated = extractorFactory.decorate(context); |
| 127 | + if (decorated != context) { |
| 128 | + return decorated; |
| 129 | + } |
| 130 | + } |
| 131 | + return super.decorate(context); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Creates a new {@link CompositePropagationFactory}, which uses the given |
| 136 | + * {@code injectionTypes} for injection and {@code extractionTypes} for extraction. |
| 137 | + * @param baggageManager the baggage manager to use, or {@code null} |
| 138 | + * @param injectionTypes the propagation types for injection |
| 139 | + * @param extractionTypes the propagation types for extraction |
| 140 | + * @return the {@link CompositePropagationFactory} |
| 141 | + */ |
| 142 | + static CompositePropagationFactory create(BaggageManager baggageManager, |
| 143 | + Collection<TracingProperties.Propagation.PropagationType> injectionTypes, |
| 144 | + Collection<TracingProperties.Propagation.PropagationType> extractionTypes) { |
| 145 | + List<Factory> injectors = injectionTypes.stream() |
| 146 | + .map((injection) -> factoryForType(baggageManager, injection)) |
| 147 | + .toList(); |
| 148 | + List<Factory> extractors = extractionTypes.stream() |
| 149 | + .map((extraction) -> factoryForType(baggageManager, extraction)) |
| 150 | + .toList(); |
| 151 | + return new CompositePropagationFactory(injectors, extractors); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Creates a new {@link CompositePropagationFactory}, which uses the given |
| 156 | + * {@code injectionTypes} for injection and {@code extractionTypes} for extraction. |
| 157 | + * @param injectionTypes the propagation types for injection |
| 158 | + * @param extractionTypes the propagation types for extraction |
| 159 | + * @return the {@link CompositePropagationFactory} |
| 160 | + */ |
| 161 | + static CompositePropagationFactory create(Collection<TracingProperties.Propagation.PropagationType> injectionTypes, |
| 162 | + Collection<TracingProperties.Propagation.PropagationType> extractionTypes) { |
| 163 | + return create(null, injectionTypes, extractionTypes); |
| 164 | + } |
| 165 | + |
| 166 | + private static Factory factoryForType(BaggageManager baggageManager, |
| 167 | + TracingProperties.Propagation.PropagationType type) { |
| 168 | + return switch (type) { |
| 169 | + case B3 -> b3Single(); |
| 170 | + case B3_MULTI -> b3Multi(); |
| 171 | + case W3C -> w3c(baggageManager); |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Creates a new B3 propagation factory using a single B3 header. |
| 177 | + * @return the B3 propagation factory |
| 178 | + */ |
| 179 | + private static Factory b3Single() { |
| 180 | + return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build(); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Creates a new B3 propagation factory using multiple B3 headers. |
| 185 | + * @return the B3 propagation factory |
| 186 | + */ |
| 187 | + private static Factory b3Multi() { |
| 188 | + return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Creates a new W3C propagation factory. |
| 193 | + * @param baggageManager baggage manager to use, or {@code null} |
| 194 | + * @return the W3C propagation factory |
| 195 | + */ |
| 196 | + private static W3CPropagation w3c(BaggageManager baggageManager) { |
| 197 | + return (baggageManager != null) ? new W3CPropagation(baggageManager, Collections.emptyList()) |
| 198 | + : new W3CPropagation(); |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments