Rename and refactor SimapleObjectFirstInvokeInterceptor to NoCocurrencyAceessObject
This commit is contained in:
parent
7e640311ce
commit
04638c5177
|
|
@ -0,0 +1,46 @@
|
|||
package com.a.eye.skywalking.plugin.interceptor.assist;
|
||||
|
||||
import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
import com.a.eye.skywalking.plugin.interceptor.InterceptorException;
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
|
||||
|
||||
/**
|
||||
* {@link NoCocurrencyAceessObject} is an abstract class,
|
||||
* works for class's methods call each others, which these methods should be intercepted.
|
||||
*
|
||||
* At this scenario, only the first access should be intercepted.
|
||||
*
|
||||
* @author wusheng
|
||||
*/
|
||||
public abstract class NoCocurrencyAceessObject implements InstanceMethodsAroundInterceptor {
|
||||
protected String invokeCounterKey = "__$invokeCounterKey";
|
||||
|
||||
protected Object invokeCounterInstLock = new Object();
|
||||
|
||||
public void whenEnter(EnhancedClassInstanceContext context, Runnable runnable) {
|
||||
if (!context.isContain(invokeCounterKey)) {
|
||||
synchronized (invokeCounterInstLock) {
|
||||
if (!context.isContain(invokeCounterKey)) {
|
||||
context.set(invokeCounterKey, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
int counter = context.get(invokeCounterKey,
|
||||
Integer.class);
|
||||
if(++counter == 1){
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
public void whenExist(EnhancedClassInstanceContext context, Runnable runnable) {
|
||||
if (!context.isContain(invokeCounterKey)) {
|
||||
throw new InterceptorException(
|
||||
"key=invokeCounterKey not found is context. unexpected situation.");
|
||||
}
|
||||
int counter = context.get(invokeCounterKey,
|
||||
Integer.class);
|
||||
if(--counter == 0){
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
package com.a.eye.skywalking.plugin.interceptor.assist;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
import com.a.eye.skywalking.plugin.interceptor.InterceptorException;
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
|
||||
|
||||
/**
|
||||
* 用于首次拦截方法调用,避免方法内部的方法调用被多次拦截。
|
||||
*
|
||||
* @author wusheng
|
||||
*
|
||||
*/
|
||||
public abstract class SimpleObjectFirstInvokeInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
protected String invokeCounterKey = "__$invokeCounterKey";
|
||||
|
||||
protected Object invokeCounterInstLock = new Object();
|
||||
|
||||
public boolean isFirstBeforeMethod(EnhancedClassInstanceContext context) {
|
||||
if (!context.isContain(invokeCounterKey)) {
|
||||
synchronized (invokeCounterInstLock) {
|
||||
if (!context.isContain(invokeCounterKey)) {
|
||||
context.set(invokeCounterKey, new AtomicInteger(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
AtomicInteger counter = context.get(invokeCounterKey,
|
||||
AtomicInteger.class);
|
||||
return counter.incrementAndGet() == 1;
|
||||
}
|
||||
|
||||
public boolean isLastAfterMethod(EnhancedClassInstanceContext context) {
|
||||
if (!context.isContain(invokeCounterKey)) {
|
||||
throw new InterceptorException(
|
||||
"key=invokeCounterKey not found is context. unexpected failue.");
|
||||
}
|
||||
AtomicInteger counter = context.get(invokeCounterKey,
|
||||
AtomicInteger.class);
|
||||
return counter.decrementAndGet() == 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package test.a.eye.cloud.api;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.a.eye.skywalking.invoke.monitor.RPCClientInvokeMonitor;
|
||||
import com.a.eye.skywalking.model.Identification;
|
||||
import com.a.eye.skywalking.model.Identification.IdentificationBuilder;
|
||||
|
||||
public class TimeTest {
|
||||
@Test
|
||||
public void test(){
|
||||
RPCClientInvokeMonitor sender = new RPCClientInvokeMonitor();
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
IdentificationBuilder builder = Identification
|
||||
.newBuilder()
|
||||
.viewPoint("1111");
|
||||
sender.beforeInvoke(builder.build());
|
||||
sender.afterInvoke();
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println(end - start + "ms");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,44 +1,47 @@
|
|||
package com.a.eye.skywalking.plugin.jedis.v2;
|
||||
|
||||
import com.a.eye.skywalking.invoke.monitor.RPCClientInvokeMonitor;
|
||||
import com.a.eye.skywalking.model.Identification;
|
||||
import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
import com.a.eye.skywalking.plugin.interceptor.assist.SimpleObjectFirstInvokeInterceptor;
|
||||
import com.a.eye.skywalking.plugin.interceptor.assist.NoCocurrencyAceessObject;
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext;
|
||||
import com.a.eye.skywalking.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
|
||||
public class JedisMethodInterceptor extends SimpleObjectFirstInvokeInterceptor {
|
||||
public class JedisMethodInterceptor extends NoCocurrencyAceessObject {
|
||||
protected static final String REDIS_CONN_INFO_KEY = "redisClusterConnInfo";
|
||||
|
||||
private static RPCClientInvokeMonitor rpcClientInvokeMonitor = new RPCClientInvokeMonitor();
|
||||
|
||||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) {
|
||||
if (this.isFirstBeforeMethod(context)) {
|
||||
/**
|
||||
* redis server wouldn't process rpc context. ignore the
|
||||
* return(ContextData) of sender's beforeSend
|
||||
*/
|
||||
Identification.IdentificationBuilder builder = Identification
|
||||
this.whenEnter(context, new Runnable() {
|
||||
@Override public void run() {
|
||||
/**
|
||||
* redis server wouldn't process rpc context. ignore the
|
||||
* return(ContextData) of sender's beforeSend
|
||||
*/
|
||||
Identification.IdentificationBuilder builder = Identification
|
||||
.newBuilder()
|
||||
.viewPoint(
|
||||
context.get(REDIS_CONN_INFO_KEY, String.class)
|
||||
+ " " + interceptorContext.methodName())
|
||||
context.get(REDIS_CONN_INFO_KEY, String.class)
|
||||
+ " " + interceptorContext.methodName())
|
||||
.spanType(RedisBuriedPointType.INSTANCE);
|
||||
if (interceptorContext.allArguments().length > 0
|
||||
if (interceptorContext.allArguments().length > 0
|
||||
&& interceptorContext.allArguments()[0] instanceof String) {
|
||||
builder.businessKey("key="
|
||||
builder.businessKey("key="
|
||||
+ interceptorContext.allArguments()[0]);
|
||||
}
|
||||
rpcClientInvokeMonitor.beforeInvoke(builder.build());
|
||||
}
|
||||
rpcClientInvokeMonitor.beforeInvoke(builder.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) {
|
||||
if (this.isLastAfterMethod(context)) {
|
||||
rpcClientInvokeMonitor.afterInvoke();
|
||||
}
|
||||
this.whenExist(context, new Runnable(){
|
||||
@Override public void run() {
|
||||
rpcClientInvokeMonitor.afterInvoke();
|
||||
}
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue