Skip to content

Commit

Permalink
Fix problem which lead redisson plugin to throw unnecessary NullPoint…
Browse files Browse the repository at this point in the history
…erException (#700)
  • Loading branch information
Ricehomesky authored Jun 24, 2024
1 parent 8200cf2 commit 01248a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.skywalking.apm.plugin.redisson.v3;

import io.netty.channel.Channel;

import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
Expand All @@ -28,8 +30,8 @@
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.redisson.v3.util.ClassUtil;
import org.apache.skywalking.apm.util.StringUtil;
Expand All @@ -42,17 +44,17 @@
import java.net.InetSocketAddress;
import java.util.Optional;

public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundInterceptorV2, InstanceConstructorInterceptor {

private static final ILog LOGGER = LogManager.getLogger(RedisConnectionMethodInterceptor.class);

private static final String ABBR = "...";
private static final String QUESTION_MARK = "?";
private static final String DELIMITER_SPACE = " ";
public static final Object STOP_SPAN_FLAG = new Object();

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInvocationContext context) throws Throwable {
String peer = (String) objInst.getSkyWalkingDynamicField();

RedisConnection connection = (RedisConnection) objInst;
Expand Down Expand Up @@ -82,6 +84,7 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
}

AbstractSpan span = ContextManager.createExitSpan(operationName, peer);
context.setContext(STOP_SPAN_FLAG);
span.setComponent(ComponentsDefine.REDISSON);
Tags.CACHE_TYPE.set(span, "Redis");
Tags.CACHE_INSTANCE.set(span, dbInstance);
Expand All @@ -93,17 +96,19 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
}

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret, MethodInvocationContext context) throws Throwable {
if (Objects.nonNull(context.getContext())) {
ContextManager.stopSpan();
}
return ret;
}

@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
AbstractSpan span = ContextManager.activeSpan();
span.log(t);
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t, MethodInvocationContext context) {
if (Objects.nonNull(context.getContext())) {
AbstractSpan span = ContextManager.activeSpan();
span.log(t);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.ClassInstanceMethodsEnhancePluginDefineV2;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.v2.InstanceMethodsInterceptV2Point;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;

import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;

public class RedisConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public class RedisConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefineV2 {

private static final String ENHANCE_CLASS = "org.redisson.client.RedisConnection";

Expand All @@ -53,16 +53,16 @@ public String getConstructorInterceptor() {
}

@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
public InstanceMethodsInterceptV2Point[] getInstanceMethodsInterceptV2Points() {
return new InstanceMethodsInterceptV2Point[] {
new InstanceMethodsInterceptV2Point() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("send");
}

@Override
public String getMethodsInterceptor() {
public String getMethodsInterceptorV2() {
return REDISSON_METHOD_INTERCEPTOR_CLASS;
}

Expand Down

0 comments on commit 01248a2

Please sign in to comment.