|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 VMware Inc. or its affiliates, All Rights Reserved. |
| 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 reactor.core.publisher; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | +import java.util.function.Predicate; |
| 21 | + |
| 22 | +import io.micrometer.context.ContextRegistry; |
| 23 | +import io.micrometer.context.ContextSnapshot; |
| 24 | + |
| 25 | +import reactor.util.annotation.Nullable; |
| 26 | +import reactor.util.context.Context; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utility private class to detect if the <a href="https://github.com/micrometer-metrics/context-propagation">context-propagation library</a> is on the classpath and to offer |
| 30 | + * ContextSnapshot support to {@link Flux} and {@link Mono}. |
| 31 | + * |
| 32 | + * @author Simon Baslé |
| 33 | + */ |
| 34 | +final class ContextPropagation { |
| 35 | + |
| 36 | + static final boolean isContextPropagationAvailable; |
| 37 | + |
| 38 | + static final Predicate<Object> PREDICATE_TRUE = v -> true; |
| 39 | + static final Function<Context, Context> NO_OP = c -> c; |
| 40 | + static final Function<Context, Context> WITH_GLOBAL_REGISTRY_NO_PREDICATE; |
| 41 | + |
| 42 | + static { |
| 43 | + boolean contextPropagation; |
| 44 | + try { |
| 45 | + Class.forName("io.micrometer.context.ContextRegistry", false, ContextPropagation.class.getClassLoader()); |
| 46 | + contextPropagation = true; |
| 47 | + } |
| 48 | + catch (Throwable t) { |
| 49 | + contextPropagation = false; |
| 50 | + } |
| 51 | + isContextPropagationAvailable = contextPropagation; |
| 52 | + if (contextPropagation) { |
| 53 | + WITH_GLOBAL_REGISTRY_NO_PREDICATE = new ContextCaptureFunction(PREDICATE_TRUE, ContextRegistry.getInstance()); |
| 54 | + } |
| 55 | + else { |
| 56 | + WITH_GLOBAL_REGISTRY_NO_PREDICATE = NO_OP; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Is Micrometer {@code context-propagation} API on the classpath? |
| 62 | + * |
| 63 | + * @return true if context-propagation is available at runtime, false otherwise |
| 64 | + */ |
| 65 | + static boolean isContextPropagationAvailable() { |
| 66 | + return isContextPropagationAvailable; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Create a support function that takes a snapshot of thread locals and merges them with the |
| 71 | + * provided {@link Context}, resulting in a new {@link Context} which includes entries |
| 72 | + * captured from threadLocals by the Context-Propagation API. |
| 73 | + * |
| 74 | + * @return the {@link Context} augmented with captured entries |
| 75 | + */ |
| 76 | + public static Function<Context, Context> contextCapture() { |
| 77 | + if (!isContextPropagationAvailable) { |
| 78 | + return NO_OP; |
| 79 | + } |
| 80 | + return WITH_GLOBAL_REGISTRY_NO_PREDICATE; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create a support function that takes a snapshot of thread locals and merges them with the |
| 85 | + * provided {@link Context}, resulting in a new {@link Context} which includes entries |
| 86 | + * captured from threadLocals by the Context-Propagation API. |
| 87 | + * <p> |
| 88 | + * The provided {@link Predicate} is used on keys associated to said thread locals |
| 89 | + * by the Context-Propagation API to filter which entries should be captured in the |
| 90 | + * first place. |
| 91 | + * |
| 92 | + * @param captureKeyPredicate a {@link Predicate} used on keys to determine if each entry |
| 93 | + * should be injected into the new {@link Context} |
| 94 | + * @return a {@link Function} augmenting {@link Context} with captured entries |
| 95 | + */ |
| 96 | + public static Function<Context, Context> contextCapture(Predicate<Object> captureKeyPredicate) { |
| 97 | + if (!isContextPropagationAvailable) { |
| 98 | + return NO_OP; |
| 99 | + } |
| 100 | + return new ContextCaptureFunction(captureKeyPredicate, null); |
| 101 | + } |
| 102 | + |
| 103 | + //the Function indirection allows tests to directly assert code in this class rather than static methods |
| 104 | + static final class ContextCaptureFunction implements Function<Context, Context> { |
| 105 | + |
| 106 | + final Predicate<Object> capturePredicate; |
| 107 | + final ContextRegistry registry; |
| 108 | + |
| 109 | + ContextCaptureFunction(Predicate<Object> capturePredicate, @Nullable ContextRegistry registry) { |
| 110 | + this.capturePredicate = capturePredicate; |
| 111 | + this.registry = registry != null ? registry : ContextRegistry.getInstance(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Context apply(Context target) { |
| 116 | + return ContextSnapshot.captureAllUsing(capturePredicate, this.registry).updateContext(target); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments