Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to trace redisson lock #567

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Callable {
* Add WebSphere Liberty 23.x plugin
* Add Plugin to support aerospike Java client
* Add ClickHouse parsing to the jdbc-common plugin.
* Support to trace redisson lock

#### Documentation

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.redisson.v3;

import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
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.network.trace.component.ComponentsDefine;
import org.redisson.api.RLock;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

public class RedissonLockInterceptor implements InstanceMethodsAroundInterceptor {

private static final AbstractTag<String> TAG_LOCK_NAME = Tags.ofKey("lock_name");

private static final AbstractTag<String> TAG_LEASE_TIME = Tags.ofKey("lease_time");

private static final AbstractTag<String> TAG_THREAD_ID = Tags.ofKey("thead_id");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these duplicated? If so, please move them into a Tags class to avoid the duplication.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Others are good.


@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
AbstractSpan span = ContextManager.createLocalSpan("Redisson/LOCK");
span.setComponent(ComponentsDefine.REDISSON);
SpanLayer.asCache(span);
RLock rLock = (RLock) objInst;
span.tag(TAG_LOCK_NAME, rLock.getName());
Tags.CACHE_TYPE.set(span, "Redis");
TimeUnit unit;
if (allArguments[1] instanceof TimeUnit) {
wu-sheng marked this conversation as resolved.
Show resolved Hide resolved
unit = (TimeUnit) allArguments[1];
span.tag(TAG_LEASE_TIME, unit.toMillis((Long) allArguments[0]) + "ms");
wu-sheng marked this conversation as resolved.
Show resolved Hide resolved
span.tag(TAG_THREAD_ID, String.valueOf(allArguments[2]));
} else if (allArguments[2] instanceof TimeUnit) {
unit = (TimeUnit) allArguments[2];
span.tag(TAG_LEASE_TIME, unit.toMillis((Long) allArguments[1]) + "ms");
wu-sheng marked this conversation as resolved.
Show resolved Hide resolved
span.tag(TAG_THREAD_ID, String.valueOf(allArguments[3]));
}
}

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

@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.redisson.v3.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 org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.logical.LogicalMatchOperation;

import static net.bytebuddy.matcher.ElementMatchers.named;

public class RedissonLockInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {

private static final String REDISSON_LOCK_CLASS = "org.redisson.RedissonLock";

private static final String REDISSON_SPIN_LOCK_CLASS = "org.redisson.RedissonSpinLock";

private static final String REDISSON_LOCK_INTERCEPTOR = "org.apache.skywalking.apm.plugin.redisson.v3.RedissonLockInterceptor";

@Override
protected ClassMatch enhanceClass() {
return LogicalMatchOperation.or(HierarchyMatch.byHierarchyMatch(REDISSON_LOCK_CLASS), MultiClassNameMatch.byMultiClassMatch(REDISSON_LOCK_CLASS, REDISSON_SPIN_LOCK_CLASS));
}

@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}

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

@Override
public String getMethodsInterceptor() {
return REDISSON_LOCK_INTERCEPTOR;
}

@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

redisson-3.x=org.apache.skywalking.apm.plugin.redisson.v3.define.ConnectionManagerInstrumentation
redisson-3.x=org.apache.skywalking.apm.plugin.redisson.v3.define.RedisConnectionInstrumentation
redisson-3.x=org.apache.skywalking.apm.plugin.redisson.v3.define.RedisClientInstrumentation
redisson-3.x=org.apache.skywalking.apm.plugin.redisson.v3.define.RedisClientInstrumentation
redisson-3.x=org.apache.skywalking.apm.plugin.redisson.v3.define.RedissonLockInstrumentation
64 changes: 64 additions & 0 deletions test/plugin/scenarios/redisson-scenario/config/expectedData.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,70 @@ segmentItems:
- {key: cache.instance, value: not null}
- {key: cache.cmd, value: BATCH_EXECUTE}
skipAnalysis: 'false'
- operationName: Redisson/EVAL
parentSpanId: 3
spanId: nq 0
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 56
isError: false
spanType: Exit
peer: not null
tags:
- { key: cache.type, value: Redis }
- { key: cache.instance, value: not null }
- { key: cache.cmd, value: EVAL }
- { key: cache.key, value: not null }
skipAnalysis: 'false'
- operationName: Redisson/LOCK
parentSpanId: 0
spanId: nq 0
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 56
isError: false
spanType: Local
peer: ''
tags:
- { key: lock_name, value: lock_a }
- { key: cache.type, value: Redis }
- { key: lease_time, value: 10000ms }
- { key: thead_id, value: not null }
skipAnalysis: false
- operationName: Redisson/EVAL
parentSpanId: 5
spanId: nq 0
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 56
isError: false
spanType: Exit
peer: not null
tags:
- { key: cache.type, value: Redis }
- { key: cache.instance, value: not null }
- { key: cache.cmd, value: EVAL }
- { key: cache.key, value: not null }
skipAnalysis: 'false'
- operationName: Redisson/LOCK
parentSpanId: 0
spanId: nq 0
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 56
isError: false
spanType: Local
peer: ''
tags:
- { key: lock_name, value: lock_b }
- { key: cache.type, value: Redis }
- { key: lease_time, value: 20ms }
- { key: thead_id, value: not null }
skipAnalysis: false
- operationName: GET:/case/redisson-case
parentSpanId: -1
spanId: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.redisson.Redisson;
import org.redisson.api.RBatch;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -58,6 +59,16 @@ public String redissonCase() {
batch.getBucket("batch_k_b").setAsync("batch_v_b");
batch.getBucket("batch_k_b").expireAsync(20, TimeUnit.SECONDS);
batch.execute();

RLock lockA = client.getLock("lock_a");
lockA.lock(10L,TimeUnit.SECONDS);
RLock lockB = client.getLock("lock_b");
try {
lockB.tryLock(10L,20L,TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}

return "Success";
}

Expand Down