|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.toolkit.webflux; |
| 20 | + |
| 21 | +import org.springframework.web.server.ServerWebExchange; |
| 22 | +import reactor.core.publisher.Signal; |
| 23 | +import reactor.core.publisher.SignalType; |
| 24 | +import reactor.util.context.Context; |
| 25 | + |
| 26 | +import java.util.concurrent.Callable; |
| 27 | +import java.util.function.Consumer; |
| 28 | + |
| 29 | +/** |
| 30 | + * WebFlux operators that are capable to reuse tracing context from Reactor's Context. |
| 31 | + */ |
| 32 | +public final class WebFluxSkyWalkingOperators { |
| 33 | + |
| 34 | + private WebFluxSkyWalkingOperators() { |
| 35 | + throw new IllegalStateException("You can't instantiate a utility class"); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Wraps a runnable with a local span and continue tracing context. |
| 40 | + * |
| 41 | + * @param signalType - Reactor's signal type |
| 42 | + * @param runnable - lambda to execute within the tracing context |
| 43 | + * @return consumer of a signal |
| 44 | + */ |
| 45 | + public static Consumer<Signal<?>> continueTracing(SignalType signalType, Runnable runnable) { |
| 46 | + return signal -> { |
| 47 | + if (signalType != signal.getType()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + continueTracing(runnable).accept(signal); |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Wraps a consumer with a local span and continue tracing context. |
| 56 | + * |
| 57 | + * @param signalType - Reactor's signal type |
| 58 | + * @param consumer - lambda to execute within the tracing context |
| 59 | + * @return consumer of a signal |
| 60 | + */ |
| 61 | + public static Consumer<Signal> continueTracing(SignalType signalType, Consumer<Signal> consumer) { |
| 62 | + return signal -> { |
| 63 | + if (signalType != signal.getType()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + continueTracing(signal.getContext(), () -> consumer.accept(signal)); |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Wraps a runnable with a local span and continue tracing context. |
| 72 | + * |
| 73 | + * @param runnable - lambda to execute within the tracing context |
| 74 | + * @return consumer of a signal |
| 75 | + */ |
| 76 | + public static Consumer<Signal> continueTracing(Runnable runnable) { |
| 77 | + return signal -> { |
| 78 | + Context context = signal.getContext(); |
| 79 | + continueTracing(context, runnable); |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Wraps a runnable with a local span and continue tracing context. |
| 85 | + * |
| 86 | + * @param context - Reactor context that contains the tracing context |
| 87 | + * @param runnable - lambda to execute within the tracing context |
| 88 | + */ |
| 89 | + public static void continueTracing(Context context, Runnable runnable) { |
| 90 | + runnable.run(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Wraps a callable with a local span and continue tracing context. |
| 95 | + * |
| 96 | + * @param context - Reactor context that contains the tracing context |
| 97 | + * @param callable - lambda to execute within the tracing context |
| 98 | + * @param <T> callable's return type |
| 99 | + * @return value from the callable |
| 100 | + */ |
| 101 | + public static <T> T continueTracing(Context context, Callable<T> callable) { |
| 102 | + try { |
| 103 | + return callable.call(); |
| 104 | + } catch (Exception e) { |
| 105 | + return sneakyThrow(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Wraps a callable with a local span and continue tracing context. |
| 111 | + * |
| 112 | + * @param serverWebExchange - EnhancedInstance that contains the tracing context |
| 113 | + * @param callable - lambda to execute within the tracing context |
| 114 | + * @param <T> callable's return type |
| 115 | + * @return value from the callable |
| 116 | + */ |
| 117 | + public static <T> T continueTracing(ServerWebExchange serverWebExchange, Callable<T> callable) { |
| 118 | + try { |
| 119 | + return callable.call(); |
| 120 | + } catch (Exception e) { |
| 121 | + return sneakyThrow(e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Wraps a runnable with a local span and continue tracing context. |
| 127 | + * |
| 128 | + * @param serverWebExchange - EnhancedInstance that contains the tracing context |
| 129 | + * @param runnable - lambda to execute within the tracing context |
| 130 | + */ |
| 131 | + public static void continueTracing(ServerWebExchange serverWebExchange, Runnable runnable) { |
| 132 | + runnable.run(); |
| 133 | + } |
| 134 | + |
| 135 | + private static <T extends Throwable, R> R sneakyThrow(Throwable t) throws T { |
| 136 | + throw (T) t; |
| 137 | + } |
| 138 | +} |
0 commit comments