Support Jedis' Transaction and fix duplicated enhancement (#57)

This commit is contained in:
Jiajing LU 2021-10-28 09:33:41 +08:00 committed by GitHub
parent ec1b7a19a4
commit c17acbe552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 225 additions and 2 deletions

View File

@ -5,7 +5,7 @@ Release Notes.
8.9.0
------------------
* Support `Transaction` and fix duplicated methods enhancements for `jedis-2.x` plugin.
#### Documentation

View File

@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.jedis.v2;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import redis.clients.jedis.Client;
public class TransactionConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
Client client = (Client) allArguments[0];
objInst.setSkyWalkingDynamicField(client.getHost() + ":" + client.getPort());
}
}

View File

@ -21,6 +21,7 @@ package org.apache.skywalking.apm.plugin.jedis.v2.define;
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.DeclaredInstanceMethodsInterceptPoint;
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.match.ClassMatch;
@ -46,7 +47,7 @@ public class MultiKeyPipelineBaseInstrumentation extends ClassInstanceMethodsEnh
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
new DeclaredInstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return RedisMethodMatch.INSTANCE.getJedisMethodMatcher();

View File

@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.jedis.v2.define;
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.match.ClassMatch;
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 TransactionConstructorInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "redis.clients.jedis.Transaction";
private static final String TRANSACTION_CONSTRUCTION_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jedis.v2.TransactionConstructorInterceptor";
private static final String ARGUMENT_TYPE_NAME = "redis.clients.jedis.Client";
@Override
protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[]{
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return takesArgumentWithType(0, ARGUMENT_TYPE_NAME);
}
@Override
public String getConstructorInterceptor() {
return TRANSACTION_CONSTRUCTION_INTERCEPT_CLASS;
}
}
};
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[0];
}
}

View File

@ -19,5 +19,6 @@ jedis-2.x=org.apache.skywalking.apm.plugin.jedis.v2.define.JedisInstrumentation
jedis-2.x=org.apache.skywalking.apm.plugin.jedis.v2.define.PipelineInstrumentation
jedis-2.x=org.apache.skywalking.apm.plugin.jedis.v2.define.PipelineBaseInstrumentation
jedis-2.x=org.apache.skywalking.apm.plugin.jedis.v2.define.MultiKeyPipelineBaseInstrumentation
jedis-2.x=org.apache.skywalking.apm.plugin.jedis.v2.define.TransactionConstructorInstrumentation

View File

@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.jedis.v2;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import redis.clients.jedis.Client;
import static org.mockito.Mockito.verify;
@RunWith(PowerMockRunner.class)
public class TransactionConstructorInterceptorTest {
private TransactionConstructorInterceptor interceptor;
@Mock
private EnhancedInstance enhancedInstance;
@Before
public void setUp() throws Exception {
interceptor = new TransactionConstructorInterceptor();
}
@Test
public void onConstruct() throws Throwable {
interceptor.onConstruct(enhancedInstance, new Object[]{
new Client("127.0.0.1", 6379)
});
verify(enhancedInstance).setSkyWalkingDynamicField("127.0.0.1:6379");
}
}

View File

@ -117,6 +117,34 @@ segmentItems:
- {key: db.type, value: Redis}
- {key: db.statement, value: hdel a}
skipAnalysis: 'false'
- operationName: Jedis/set
parentSpanId: 0
spanId: 8
spanLayer: Cache
startTime: nq 0
endTime: nq 0
componentId: 30
isError: false
spanType: Exit
peer: redis-server:6379
tags:
- { key: db.type, value: Redis }
- { key: db.statement, value: set key }
skipAnalysis: 'false'
- operationName: Jedis/expire
parentSpanId: 0
spanId: 9
spanLayer: Cache
startTime: nq 0
endTime: nq 0
componentId: 30
isError: false
spanType: Exit
peer: redis-server:6379
tags:
- { key: db.type, value: Redis }
- { key: db.statement, value: expire key }
skipAnalysis: 'false'
- operationName: GET:/jedis-scenario/case/jedis-scenario
parentSpanId: -1
spanId: 0

View File

@ -47,6 +47,11 @@ public class CaseController {
try (RedisPipelineCommandExecutor command = new RedisPipelineCommandExecutor(redisHost, redisPort)) {
command.pipelineExecute();
}
try (RedisTransactionCommandExecutor command = new RedisTransactionCommandExecutor(redisHost, redisPort)) {
command.multiExecute();
}
return SUCCESS;
}

View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.skywalking.apm.testcase.jedis.controller;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class RedisTransactionCommandExecutor implements AutoCloseable {
private Jedis jedis;
public RedisTransactionCommandExecutor(String host, Integer port) {
jedis = new Jedis(host, port);
}
public void multiExecute() {
Transaction pipeline = jedis.multi();
pipeline.set("key", "a");
pipeline.expire("key", 5);
pipeline.exec();
}
public void close() throws Exception {
jedis.close();
}
}