提交 b2ad6343 编写于 作者: L lsyf

Fix ehcache: performance issue

上级 e57176d0
......@@ -18,7 +18,6 @@
package org.apache.skywalking.apm.plugin.ehcache.v2;
import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
......@@ -26,20 +25,11 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceC
public class EhcacheConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
try {
CacheConfiguration cacheConfiguration = (CacheConfiguration) allArguments[0];
CacheConfiguration cacheConfiguration = (CacheConfiguration) allArguments[0];
// get cache name
if (cacheConfiguration != null) {
objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName()));
}
} catch (ClassCastException e) {
Cache cache = (Cache) allArguments[0];
// get cache name
if (cache != null && cache.getCacheConfiguration() != null) {
objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cache.getCacheConfiguration().getName()));
}
// get cache name
if (cacheConfiguration != null) {
objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName()));
}
}
}
/*
* 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.ehcache.v2;
import net.sf.ehcache.Cache;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
public class EhcachePrivateConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
Cache cache = (Cache) allArguments[0];
// get cache name
if (cache != null && cache.getCacheConfiguration() != null) {
objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cache.getCacheConfiguration().getName()));
}
}
}
......@@ -38,6 +38,7 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu
public static final String INTERCEPT_CLASS = "net.sf.ehcache.Cache";
public static final String CONSTRUCTOR_CLASS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcacheConstructorInterceptor";
public static final String PRIVATE_CONSTRUCTOR_CLASS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcachePrivateConstructorInterceptor";
// get and put value
public static final String PUT_CACHE_ENHANCE_METHOD = "put";
......@@ -80,14 +81,24 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return takesArgument(0, named("net.sf.ehcache.config.CacheConfiguration"))
.or(isPrivate().and(takesArgument(0, named("net.sf.ehcache.Cache"))));
return takesArgument(0, named("net.sf.ehcache.config.CacheConfiguration"));
}
@Override
public String getConstructorInterceptor() {
return CONSTRUCTOR_CLASS_INTERCEPT_CLASS;
}
},
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return isPrivate().and(takesArgument(0, named("net.sf.ehcache.Cache")));
}
@Override
public String getConstructorInterceptor() {
return PRIVATE_CONSTRUCTOR_CLASS_INTERCEPT_CLASS;
}
}
};
}
......
......@@ -66,6 +66,7 @@ public class EhcacheInterceptorTest {
private EhcacheOperateAllInterceptor operateAllInterceptor;
private EhcacheLockInterceptor lockInterceptor;
private EhcacheConstructorInterceptor constructorInterceptor;
private EhcachePrivateConstructorInterceptor privateConstructorInterceptor;
private EhcacheCacheNameInterceptor cacheNameInterceptor;
private Object[] operateObjectArguments;
private Object[] operateElementArguments;
......@@ -106,6 +107,7 @@ public class EhcacheInterceptorTest {
operateElementInterceptor = new EhcacheOperateElementInterceptor();
operateAllInterceptor = new EhcacheOperateAllInterceptor();
constructorInterceptor = new EhcacheConstructorInterceptor();
privateConstructorInterceptor = new EhcachePrivateConstructorInterceptor();
cacheNameInterceptor = new EhcacheCacheNameInterceptor();
lockInterceptor = new EhcacheLockInterceptor();
......@@ -139,6 +141,11 @@ public class EhcacheInterceptorTest {
constructorInterceptor.onConstruct(enhancedInstance, new Object[] {new CacheConfiguration(CACHE_NAME, 20)});
}
@Test
public void assertPrivateConstruct() throws Throwable {
privateConstructorInterceptor.onConstruct(enhancedInstance, new Object[] {new Cache(new CacheConfiguration(CACHE_NAME, 20))});
}
@Test
public void assertSetNameSuccess() throws Throwable {
cacheNameInterceptor.beforeMethod(enhancedInstance, setNameMethod, cacheNameArguments, null, null);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册