|
| 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.plugin.lettuce.v65; |
| 20 | + |
| 21 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 25 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 26 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext; |
| 28 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 29 | +import org.reactivestreams.Publisher; |
| 30 | +import reactor.core.publisher.Flux; |
| 31 | +import reactor.core.publisher.Mono; |
| 32 | + |
| 33 | +import java.lang.reflect.Method; |
| 34 | + |
| 35 | +/** |
| 36 | + * Intercepts reactive publisher factory methods (createMono/createFlux) |
| 37 | + * to ensure the SkyWalking context snapshot is propagated via Reactor Context. |
| 38 | + * |
| 39 | + * <p>If the Reactor Context does not already contain a snapshot, this interceptor |
| 40 | + * captures the current active context and writes it into the subscriber context |
| 41 | + * as a fallback propagation mechanism.</p> |
| 42 | + */ |
| 43 | +public class RedisReactiveCreatePublisherMethodInterceptorV65 implements InstanceMethodsAroundInterceptorV2 { |
| 44 | + |
| 45 | + private static final String SNAPSHOT_KEY = "SKYWALKING_CONTEXT_SNAPSHOT"; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 49 | + MethodInvocationContext context) { |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret, MethodInvocationContext context) { |
| 54 | + |
| 55 | + if (!(ret instanceof Mono) && !(ret instanceof Flux)) { |
| 56 | + return ret; |
| 57 | + } |
| 58 | + |
| 59 | + final AbstractSpan localSpan = ContextManager.createLocalSpan("Lettuce/Reactive/" + method.getName()); |
| 60 | + localSpan.setComponent(ComponentsDefine.LETTUCE); |
| 61 | + SpanLayer.asCache(localSpan); |
| 62 | + |
| 63 | + try { |
| 64 | + final ContextSnapshot snapshot = ContextManager.capture(); |
| 65 | + |
| 66 | + return wrapPublisher((Publisher<?>) ret, snapshot); |
| 67 | + } finally { |
| 68 | + ContextManager.stopSpan(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private <T> Publisher<T> wrapPublisher(Publisher<T> original, ContextSnapshot snapshot) { |
| 73 | + if (original instanceof Mono) { |
| 74 | + return Mono.deferContextual(ctxView -> { |
| 75 | + if (ctxView.hasKey(SNAPSHOT_KEY)) { |
| 76 | + return (Mono<T>) original; |
| 77 | + } |
| 78 | + return ((Mono<T>) original).contextWrite(c -> c.put(SNAPSHOT_KEY, snapshot)); |
| 79 | + }); |
| 80 | + } else { |
| 81 | + return Flux.deferContextual(ctxView -> { |
| 82 | + if (ctxView.hasKey(SNAPSHOT_KEY)) { |
| 83 | + return original; |
| 84 | + } |
| 85 | + return ((Flux<T>) original).contextWrite(c -> c.put(SNAPSHOT_KEY, snapshot)); |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t, MethodInvocationContext context) { |
| 92 | + } |
| 93 | +} |
0 commit comments