|
19 | 19 | import java.util.Collection; |
20 | 20 | import java.util.Collections; |
21 | 21 | import java.util.List; |
22 | | -import java.util.function.Predicate; |
23 | 22 | import java.util.stream.Stream; |
24 | 23 |
|
25 | 24 | import brave.propagation.B3Propagation; |
@@ -76,11 +75,19 @@ public Propagation<String> get() { |
76 | 75 |
|
77 | 76 | @Override |
78 | 77 | public TraceContext decorate(TraceContext context) { |
79 | | -return Stream.concat(this.injectors.stream(), this.extractors.stream()) |
80 | | -.map((factory) -> factory.decorate(context)) |
81 | | -.filter((decorated) -> decorated != context) |
82 | | -.findFirst() |
83 | | -.orElse(context); |
| 78 | +for (Propagation.Factory factory : this.injectors.factories) { |
| 79 | +TraceContext decorated = factory.decorate(context); |
| 80 | +if (decorated != context) { |
| 81 | +return decorated; |
| 82 | +} |
| 83 | +} |
| 84 | +for (Propagation.Factory factory : this.extractors.factories) { |
| 85 | +TraceContext decorated = factory.decorate(context); |
| 86 | +if (decorated != context) { |
| 87 | +return decorated; |
| 88 | +} |
| 89 | +} |
| 90 | +return context; |
84 | 91 | } |
85 | 92 |
|
86 | 93 | /** |
@@ -179,11 +186,21 @@ private static class PropagationFactories { |
179 | 186 | } |
180 | 187 |
|
181 | 188 | boolean requires128BitTraceId() { |
182 | | -return stream().anyMatch(Propagation.Factory::requires128BitTraceId); |
| 189 | +for (Propagation.Factory factory : this.factories) { |
| 190 | +if (factory.requires128BitTraceId()) { |
| 191 | +return true; |
| 192 | +} |
| 193 | +} |
| 194 | +return false; |
183 | 195 | } |
184 | 196 |
|
185 | 197 | boolean supportsJoin() { |
186 | | -return stream().allMatch(Propagation.Factory::supportsJoin); |
| 198 | +for (Propagation.Factory factory : this.factories) { |
| 199 | +if (!factory.supportsJoin()) { |
| 200 | +return false; |
| 201 | +} |
| 202 | +} |
| 203 | +return true; |
187 | 204 | } |
188 | 205 |
|
189 | 206 | List<Propagation<String>> get() { |
@@ -224,19 +241,24 @@ public List<String> keys() { |
224 | 241 |
|
225 | 242 | @Override |
226 | 243 | public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) { |
227 | | -return (traceContext, request) -> this.injectors.stream() |
228 | | -.map((propagation) -> propagation.injector(setter)) |
229 | | -.forEach((injector) -> injector.inject(traceContext, request)); |
| 244 | +return (traceContext, request) -> { |
| 245 | +for (Propagation<String> propagation : this.injectors) { |
| 246 | +propagation.injector(setter).inject(traceContext, request); |
| 247 | +} |
| 248 | +}; |
230 | 249 | } |
231 | 250 |
|
232 | 251 | @Override |
233 | 252 | public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) { |
234 | | -return (request) -> this.extractors.stream() |
235 | | -.map((propagation) -> propagation.extractor(getter)) |
236 | | -.map((extractor) -> extractor.extract(request)) |
237 | | -.filter(Predicate.not(TraceContextOrSamplingFlags.EMPTY::equals)) |
238 | | -.findFirst() |
239 | | -.orElse(TraceContextOrSamplingFlags.EMPTY); |
| 253 | +return (request) -> { |
| 254 | +for (Propagation<String> propagation : this.extractors) { |
| 255 | +TraceContextOrSamplingFlags extracted = propagation.extractor(getter).extract(request); |
| 256 | +if (!TraceContextOrSamplingFlags.EMPTY.equals(extracted)) { |
| 257 | +return extracted; |
| 258 | +} |
| 259 | +} |
| 260 | +return TraceContextOrSamplingFlags.EMPTY; |
| 261 | +}; |
240 | 262 | } |
241 | 263 |
|
242 | 264 | } |
|
0 commit comments