Skip to content

Commit 492fb6e

Browse files
authored
refactor pipeline in jedis-plugin (#445)
1 parent 4012dc4 commit 492fb6e

22 files changed

Lines changed: 259 additions & 223 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Release Notes.
1111
* Move the baseline to JDK 17 for development, the runtime baseline is still Java 8 compatible.
1212
* Remove Powermock entirely from the test cases.
1313
* Fix H2 instrumentation point
14+
* Refactor pipeline in jedis-plugin.
1415

1516
#### Documentation
1617
* Update docs of Tracing APIs, reorganize the API docs into six parts

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-2.x-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v3/JedisMethodInterceptor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
2727
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
2828
import org.apache.skywalking.apm.util.StringUtil;
29+
import redis.clients.jedis.Pipeline;
30+
import redis.clients.jedis.Transaction;
2931

3032
import java.lang.reflect.Method;
3133
import java.util.Optional;
@@ -41,9 +43,13 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
4143
SpanLayer.asCache(span);
4244
String methodName = method.getName();
4345
Tags.CACHE_TYPE.set(span, "Redis");
44-
Tags.CACHE_CMD.set(span, methodName);
45-
getKey(allArguments).ifPresent(key -> Tags.CACHE_KEY.set(span, key));
46-
parseOperation(methodName).ifPresent(op -> Tags.CACHE_OP.set(span, op));
46+
if (objInst instanceof Pipeline || objInst instanceof Transaction) {
47+
Tags.CACHE_CMD.set(span, "BATCH_EXECUTE");
48+
} else {
49+
Tags.CACHE_CMD.set(span, methodName);
50+
getKey(allArguments).ifPresent(key -> Tags.CACHE_KEY.set(span, key));
51+
parseOperation(methodName).ifPresent(op -> Tags.CACHE_OP.set(span, op));
52+
}
4753
}
4854

4955
private Optional<String> getKey(Object[] allArguments) {

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-2.x-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v3/define/PipelineInstrumentation.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PipelineInstrumentation extends AbstractWitnessInstrumentation {
3030

3131
private static final String ENHANCE_CLASS = "redis.clients.jedis.Pipeline";
3232
private static final String PIPELINE_SET_CLIENT_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v3.PipelineSetClientMethodInterceptor";
33+
private static final String JEDIS_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v3.JedisMethodInterceptor";
3334

3435
@Override
3536
public ClassMatch enhanceClass() {
@@ -56,6 +57,23 @@ public String getMethodsInterceptor() {
5657
return PIPELINE_SET_CLIENT_METHOD_INTERCEPT_CLASS;
5758
}
5859

60+
@Override
61+
public boolean isOverrideArgs() {
62+
return false;
63+
}
64+
},
65+
66+
new InstanceMethodsInterceptPoint() {
67+
@Override
68+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
69+
return named("sync").or(named("syncAndReturnAll")).or(named("discard"));
70+
}
71+
72+
@Override
73+
public String getMethodsInterceptor() {
74+
return JEDIS_METHOD_INTERCEPT_CLASS;
75+
}
76+
5977
@Override
6078
public boolean isOverrideArgs() {
6179
return false;

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-2.x-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v3/define/TransactionConstructorInstrumentation.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
2424
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
2525

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

2930
public class TransactionConstructorInstrumentation extends AbstractWitnessInstrumentation {
3031

3132
private static final String ENHANCE_CLASS = "redis.clients.jedis.Transaction";
3233
private static final String TRANSACTION_CONSTRUCTION_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v3.TransactionConstructorInterceptor";
34+
private static final String JEDIS_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v3.JedisMethodInterceptor";
3335
private static final String ARGUMENT_TYPE_NAME = "redis.clients.jedis.Client";
3436

3537
@Override
@@ -56,6 +58,23 @@ public String getConstructorInterceptor() {
5658

5759
@Override
5860
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
59-
return new InstanceMethodsInterceptPoint[0];
61+
return new InstanceMethodsInterceptPoint[]{
62+
new InstanceMethodsInterceptPoint() {
63+
@Override
64+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
65+
return named("exec").or(named("execGetResponse")).or(named("discard"));
66+
}
67+
68+
@Override
69+
public String getMethodsInterceptor() {
70+
return JEDIS_METHOD_INTERCEPT_CLASS;
71+
}
72+
73+
@Override
74+
public boolean isOverrideArgs() {
75+
return false;
76+
}
77+
}
78+
};
6079
}
6180
}

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-2.x-3.x-plugin/src/main/resources/skywalking-plugin.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
jedis-2.x-3.x=org.apache.skywalking.apm.plugin.jedis.v3.define.JedisClusterInstrumentation
1818
jedis-2.x-3.x=org.apache.skywalking.apm.plugin.jedis.v3.define.JedisInstrumentation
1919
jedis-2.x-3.x=org.apache.skywalking.apm.plugin.jedis.v3.define.PipelineInstrumentation
20-
jedis-2.x-3.x=org.apache.skywalking.apm.plugin.jedis.v3.define.PipelineBaseInstrumentation
21-
jedis-2.x-3.x=org.apache.skywalking.apm.plugin.jedis.v3.define.MultiKeyPipelineBaseInstrumentation
2220
jedis-2.x-3.x=org.apache.skywalking.apm.plugin.jedis.v3.define.TransactionConstructorInstrumentation
2321

2422

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v4/ConnectionSendCmdInterceptor.java renamed to apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v4/JedisConstructorInterceptor.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717

1818
package org.apache.skywalking.apm.plugin.jedis.v4;
1919

20-
import redis.clients.jedis.CommandArguments;
21-
import redis.clients.jedis.args.Rawable;
22-
23-
import java.util.Iterator;
24-
25-
public class ConnectionSendCmdInterceptor extends AbstractConnectionInterceptor {
20+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
22+
import redis.clients.jedis.Connection;
23+
import redis.clients.jedis.Jedis;
2624

25+
public class JedisConstructorInterceptor implements InstanceConstructorInterceptor {
2726
@Override
28-
protected Iterator<Rawable> getCommands(Object[] allArguments) {
29-
CommandArguments commandArguments = (CommandArguments) allArguments[0];
30-
return commandArguments.iterator();
27+
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
28+
Connection connection = null;
29+
if (allArguments[0] instanceof Jedis) {
30+
connection = ((Jedis) allArguments[0]).getConnection();
31+
} else if (allArguments[0] instanceof Connection) {
32+
connection = (Connection) allArguments[0];
33+
}
34+
if (connection instanceof EnhancedInstance) {
35+
objInst.setSkyWalkingDynamicField(((EnhancedInstance) connection).getSkyWalkingDynamicField());
36+
}
3137
}
32-
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
package org.apache.skywalking.apm.plugin.jedis.v4;
19+
20+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
21+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
22+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
23+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
27+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
28+
29+
import java.lang.reflect.Method;
30+
31+
public class JedisMethodInterceptor implements InstanceMethodsAroundInterceptor {
32+
33+
@Override
34+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
35+
MethodInterceptResult result) throws Throwable {
36+
String peer = String.valueOf(objInst.getSkyWalkingDynamicField());
37+
AbstractSpan span = ContextManager.createExitSpan("Jedis/" + method.getName(), peer);
38+
span.setComponent(ComponentsDefine.JEDIS);
39+
SpanLayer.asCache(span);
40+
Tags.CACHE_TYPE.set(span, "Redis");
41+
Tags.CACHE_CMD.set(span, "BATCH_EXECUTE");
42+
}
43+
44+
@Override
45+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
46+
Object ret) throws Throwable {
47+
ContextManager.stopSpan();
48+
return ret;
49+
}
50+
51+
@Override
52+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
53+
Class<?>[] argumentsTypes, Throwable t) {
54+
AbstractSpan span = ContextManager.activeSpan();
55+
span.log(t);
56+
}
57+
}

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v4/define/ConnectionInstrumentation.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class ConnectionInstrumentation extends AbstractWitnessInstrumentation {
3333
private static final String ENHANCE_CLASS = "redis.clients.jedis.Connection";
3434
private static final String CONNECTION_CONSTRUCTOR_INTERCEPTOR = "org.apache.skywalking.apm.plugin.jedis.v4.ConnectionConstructorInterceptor";
3535
private static final String CONNECTION_EXECUTE_INTERCEPTOR = "org.apache.skywalking.apm.plugin.jedis.v4.ConnectionExecuteInterceptor";
36-
private static final String CONNECTION_SEND_INTERCEPTOR = "org.apache.skywalking.apm.plugin.jedis.v4.ConnectionSendCmdInterceptor";
3736

3837
@Override
3938
protected ClassMatch enhanceClass() {
@@ -72,22 +71,6 @@ public String getMethodsInterceptor() {
7271
return CONNECTION_EXECUTE_INTERCEPTOR;
7372
}
7473

75-
@Override
76-
public boolean isOverrideArgs() {
77-
return false;
78-
}
79-
},
80-
new InstanceMethodsInterceptPoint() {
81-
@Override
82-
public ElementMatcher<MethodDescription> getMethodsMatcher() {
83-
return named("sendCommand").and(takesArgumentWithType(0, "redis.clients.jedis.CommandArguments"));
84-
}
85-
86-
@Override
87-
public String getMethodsInterceptor() {
88-
return CONNECTION_SEND_INTERCEPTOR;
89-
}
90-
9174
@Override
9275
public boolean isOverrideArgs() {
9376
return false;

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-2.x-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v3/define/PipelineBaseInstrumentation.java renamed to apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v4/define/PipelineInstrumentation.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.apm.plugin.jedis.v3.define;
18+
package org.apache.skywalking.apm.plugin.jedis.v4.define;
1919

2020
import net.bytebuddy.description.method.MethodDescription;
2121
import net.bytebuddy.matcher.ElementMatcher;
22+
import net.bytebuddy.matcher.ElementMatchers;
2223
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
2324
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
2425
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
25-
import org.apache.skywalking.apm.plugin.jedis.v3.RedisMethodMatch;
2626

27+
import static net.bytebuddy.matcher.ElementMatchers.named;
2728
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
2829

29-
public class PipelineBaseInstrumentation extends AbstractWitnessInstrumentation {
30+
public class PipelineInstrumentation extends AbstractWitnessInstrumentation {
3031

31-
private static final String ENHANCE_CLASS = "redis.clients.jedis.PipelineBase";
32-
private static final String JEDIS_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v3.JedisMethodInterceptor";
32+
private static final String ENHANCE_CLASS = "redis.clients.jedis.Pipeline";
33+
private static final String JEDIS_CONSTRUCTION_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v4.JedisConstructorInterceptor";
34+
private static final String JEDIS_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v4.JedisMethodInterceptor";
3335

3436
@Override
3537
public ClassMatch enhanceClass() {
@@ -38,7 +40,20 @@ public ClassMatch enhanceClass() {
3840

3941
@Override
4042
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
41-
return new ConstructorInterceptPoint[0];
43+
return new ConstructorInterceptPoint[]{
44+
new ConstructorInterceptPoint() {
45+
@Override
46+
public ElementMatcher<MethodDescription> getConstructorMatcher() {
47+
return ElementMatchers.takesArgument(0, named("redis.clients.jedis.Jedis"))
48+
.or(ElementMatchers.takesArgument(0, named("redis.clients.jedis.Connection")));
49+
}
50+
51+
@Override
52+
public String getConstructorInterceptor() {
53+
return JEDIS_CONSTRUCTION_INTERCEPT_CLASS;
54+
}
55+
}
56+
};
4257
}
4358

4459
@Override
@@ -47,7 +62,7 @@ public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
4762
new InstanceMethodsInterceptPoint() {
4863
@Override
4964
public ElementMatcher<MethodDescription> getMethodsMatcher() {
50-
return RedisMethodMatch.INSTANCE.getJedisMethodMatcher();
65+
return named("sync").or(named("syncAndReturnAll"));
5166
}
5267

5368
@Override

apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-2.x-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v3/define/MultiKeyPipelineBaseInstrumentation.java renamed to apm-sniffer/apm-sdk-plugin/jedis-plugins/jedis-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jedis/v4/define/TransactionConstructorInstrumentation.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,54 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.apm.plugin.jedis.v3.define;
18+
package org.apache.skywalking.apm.plugin.jedis.v4.define;
1919

2020
import net.bytebuddy.description.method.MethodDescription;
2121
import net.bytebuddy.matcher.ElementMatcher;
22+
import net.bytebuddy.matcher.ElementMatchers;
2223
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
23-
import org.apache.skywalking.apm.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint;
2424
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
2525
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
26-
import org.apache.skywalking.apm.plugin.jedis.v3.RedisMethodMatch;
2726

27+
import static net.bytebuddy.matcher.ElementMatchers.named;
2828
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
2929

30-
public class MultiKeyPipelineBaseInstrumentation extends AbstractWitnessInstrumentation {
30+
public class TransactionConstructorInstrumentation extends AbstractWitnessInstrumentation {
3131

32-
private static final String ENHANCE_CLASS = "redis.clients.jedis.MultiKeyPipelineBase";
33-
private static final String JEDIS_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v3.JedisMethodInterceptor";
32+
private static final String ENHANCE_CLASS = "redis.clients.jedis.Transaction";
33+
private static final String JEDIS_CONSTRUCTION_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v4.JedisConstructorInterceptor";
34+
private static final String JEDIS_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v4.JedisMethodInterceptor";
3435

3536
@Override
36-
public ClassMatch enhanceClass() {
37+
protected ClassMatch enhanceClass() {
3738
return byName(ENHANCE_CLASS);
3839
}
3940

4041
@Override
4142
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
42-
return new ConstructorInterceptPoint[0];
43+
return new ConstructorInterceptPoint[]{
44+
new ConstructorInterceptPoint() {
45+
@Override
46+
public ElementMatcher<MethodDescription> getConstructorMatcher() {
47+
return ElementMatchers.takesArgument(0, named("redis.clients.jedis.Jedis"))
48+
.or(ElementMatchers.takesArgument(0, named("redis.clients.jedis.Connection")));
49+
}
50+
51+
@Override
52+
public String getConstructorInterceptor() {
53+
return JEDIS_CONSTRUCTION_INTERCEPT_CLASS;
54+
}
55+
}
56+
};
4357
}
4458

4559
@Override
4660
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
47-
return new InstanceMethodsInterceptPoint[] {
48-
new DeclaredInstanceMethodsInterceptPoint() {
61+
return new InstanceMethodsInterceptPoint[]{
62+
new InstanceMethodsInterceptPoint() {
4963
@Override
5064
public ElementMatcher<MethodDescription> getMethodsMatcher() {
51-
return RedisMethodMatch.INSTANCE.getJedisMethodMatcher();
65+
return named("exec").or(named("discard"));
5266
}
5367

5468
@Override

0 commit comments

Comments
 (0)