From 54c1cd5cb04f012ff48c8b87c11df11041e3f499 Mon Sep 17 00:00:00 2001 From: lsyf Date: Mon, 17 Feb 2020 23:31:28 +0800 Subject: [PATCH 1/5] =?UTF-8?q?Fix=20ehcache:=C2=A0=20missing=20intercepto?= =?UTF-8?q?r=20of=20private=20constructor=20called=20by=20clone=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ehcache/v2/EhcacheCloneInterceptor.java | 52 +++++++++++++++++++ .../define/EhcachePluginInstrumentation.java | 21 +++++++- .../ehcache/v2/EhcacheInterceptorTest.java | 14 +++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java new file mode 100644 index 000000000..639439c98 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java @@ -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.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.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; + +import java.lang.reflect.Method; + +public class EhcacheCloneInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + if (ret == null) { + return null; + } + CacheConfiguration cacheConfiguration = ((Cache) ret).getCacheConfiguration(); + if (cacheConfiguration != null) { + ((EnhancedInstance) ret).setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName())); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java index c8883cc46..d16f27c92 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java @@ -37,10 +37,12 @@ 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 CLONE_CLASS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcacheCloneInterceptor"; // get and put value public static final String PUT_CACHE_ENHANCE_METHOD = "put"; public static final String GET_CACHE_ENHANCE_METHOD = "get"; + public static final String CLONE_CACHE_ENHANCE_METHOD = "clone"; public static final String GET_QUIET_CACHE_ENHANCE_METHOD = "getQuiet"; public static final String REMOVE_CACHE_ENHANCE_METHOD = "remove"; public static final String REMOVE_AND_RETURN_ELEMENT_CACHE_ENHANCE_METHOD = "removeAndReturnElement"; @@ -89,7 +91,24 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { - new InstanceMethodsInterceptPoint() { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(CLONE_CACHE_ENHANCE_METHOD); + } + + @Override + public String getMethodsInterceptor() { + return CLONE_CLASS_INTERCEPT_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + + }, + new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named(GET_WITH_LOADER_CACHE_ENHANCE_METHOD).or(named(GET_CACHE_ENHANCE_METHOD).and(takesArgument(0, Object.class))) diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java index 555dc4b35..07b02a6a7 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java @@ -38,6 +38,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.powermock.reflect.Whitebox; +import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.CLONE_CACHE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.GET_ALL_CACHE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.GET_CACHE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.PUT_CACHE_ENHANCE_METHOD; @@ -64,6 +65,7 @@ public class EhcacheInterceptorTest { private EhcacheOperateAllInterceptor operateAllInterceptor; private EhcacheLockInterceptor lockInterceptor; private EhcacheConstructorInterceptor constructorInterceptor; + private EhcacheCloneInterceptor cloneInterceptor; private Object[] operateObjectArguments; private Object[] operateElementArguments; private Object[] tryLockArguments; @@ -80,6 +82,8 @@ public class EhcacheInterceptorTest { private Method releaseReadLockMethod; private Method releaseWriteLockMethod; + private Method cloneMethod; + private EnhancedInstance enhancedInstance = new EnhancedInstance() { @Override public Object getSkyWalkingDynamicField() { @@ -97,6 +101,7 @@ public class EhcacheInterceptorTest { operateElementInterceptor = new EhcacheOperateElementInterceptor(); operateAllInterceptor = new EhcacheOperateAllInterceptor(); constructorInterceptor = new EhcacheConstructorInterceptor(); + cloneInterceptor = new EhcacheCloneInterceptor(); lockInterceptor = new EhcacheLockInterceptor(); exception = new Exception(); @@ -117,6 +122,8 @@ public class EhcacheInterceptorTest { tryWriteLockMethod = Whitebox.getMethods(Cache.class, WRITE_LOCK_TRY_ENHANCE_METHOD)[0]; releaseReadLockMethod = Whitebox.getMethods(Cache.class, READ_LOCK_RELEASE_ENHANCE_METHOD)[0]; releaseWriteLockMethod = Whitebox.getMethods(Cache.class, WRITE_LOCK_RELEASE_ENHANCE_METHOD)[0]; + + cloneMethod = Whitebox.getMethods(Cache.class, CLONE_CACHE_ENHANCE_METHOD)[0]; } @Test @@ -124,6 +131,13 @@ public class EhcacheInterceptorTest { constructorInterceptor.onConstruct(enhancedInstance, new Object[] {new CacheConfiguration(CACHE_NAME, 20)}); } + @Test + public void assertClone() throws Throwable { + cloneInterceptor.beforeMethod(enhancedInstance, cloneMethod, null, null, null); + cloneInterceptor.handleMethodException(enhancedInstance, cloneMethod, null, null, exception); + cloneInterceptor.afterMethod(enhancedInstance, cloneMethod, null, null, null); + } + @Test public void assertPutSuccess() throws Throwable { // put arguments From 74c1f7ef73d6fafa24107ae8f9433f8e4c53c6ac Mon Sep 17 00:00:00 2001 From: lsyf Date: Tue, 18 Feb 2020 13:07:16 +0800 Subject: [PATCH 2/5] ehcache-2.x-scenario add EhcacheCloneInterceptor case --- .../skywalking/apm/testcase/ehcache/v2/CaseServlet.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java b/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java index d29fd9054..0d0a78dbd 100644 --- a/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java +++ b/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java @@ -35,7 +35,14 @@ public class CaseServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - Cache cache = cacheManager.getCache("testCache"); + Cache originCache = cacheManager.getCache("testCache"); + + // EhcacheCloneInterceptor + Cache cache = null; + try { + cache = originCache.clone(); + } catch (CloneNotSupportedException e) { + } String objectKey = "dataKey"; From e57176d05e00fa8893be15a206b0508f24cc56c1 Mon Sep 17 00:00:00 2001 From: lsyf Date: Tue, 18 Feb 2020 22:33:33 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Fix=20ehcache:=C2=A0=20add=20interceptor=20?= =?UTF-8?q?for=20Cache's=20private=20constructor=20and=20setName=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....java => EhcacheCacheNameInterceptor.java} | 24 +++++-------- .../v2/EhcacheConstructorInterceptor.java | 18 +++++++--- .../define/EhcachePluginInstrumentation.java | 36 ++++++++++--------- .../ehcache/v2/EhcacheInterceptorTest.java | 30 ++++++++++------ .../config/expectedData.yaml | 15 ++++++++ .../apm/testcase/ehcache/v2/CaseServlet.java | 17 ++++----- 6 files changed, 87 insertions(+), 53 deletions(-) rename apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/{EhcacheCloneInterceptor.java => EhcacheCacheNameInterceptor.java} (66%) diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCacheNameInterceptor.java similarity index 66% rename from apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java rename to apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCacheNameInterceptor.java index 639439c98..39e0a66fe 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCloneInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheCacheNameInterceptor.java @@ -18,35 +18,29 @@ 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.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import java.lang.reflect.Method; -public class EhcacheCloneInterceptor implements InstanceMethodsAroundInterceptor { +public class EhcacheCacheNameInterceptor implements InstanceMethodsAroundInterceptor { @Override - public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { } @Override - public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { - if (ret == null) { - return null; - } - CacheConfiguration cacheConfiguration = ((Cache) ret).getCacheConfiguration(); - if (cacheConfiguration != null) { - ((EnhancedInstance) ret).setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName())); - } + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + String name = (String) allArguments[0]; + objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(name)); return ret; } @Override - public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { - + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { } } diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java index 1d4868aaf..dd848d68a 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java @@ -18,6 +18,7 @@ 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; @@ -25,11 +26,20 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceC public class EhcacheConstructorInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - CacheConfiguration cacheConfiguration = (CacheConfiguration) allArguments[0]; + try { + CacheConfiguration cacheConfiguration = (CacheConfiguration) allArguments[0]; - // get cache name - if (cacheConfiguration != null) { - objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName())); + // 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())); + } } } } diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java index d16f27c92..bb741a175 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java @@ -28,6 +28,7 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.isPrivate; import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; /** @@ -37,12 +38,10 @@ 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 CLONE_CLASS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcacheCloneInterceptor"; // get and put value public static final String PUT_CACHE_ENHANCE_METHOD = "put"; public static final String GET_CACHE_ENHANCE_METHOD = "get"; - public static final String CLONE_CACHE_ENHANCE_METHOD = "clone"; public static final String GET_QUIET_CACHE_ENHANCE_METHOD = "getQuiet"; public static final String REMOVE_CACHE_ENHANCE_METHOD = "remove"; public static final String REMOVE_AND_RETURN_ELEMENT_CACHE_ENHANCE_METHOD = "removeAndReturnElement"; @@ -71,13 +70,18 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu public static final String READ_LOCK_RELEASE_ENHANCE_METHOD = "releaseRead" + LOCK_ENHANCE_METHOD_SUFFIX; public static final String READ_WRITE_LOCK_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcacheLockInterceptor"; + // cache name + public static final String CACHE_NAME_ENHANCE_METHOD = "setName"; + public static final String CACHE_NAME_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcacheCacheNameInterceptor"; + @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[] { new ConstructorInterceptPoint() { @Override public ElementMatcher getConstructorMatcher() { - return takesArgument(0, named("net.sf.ehcache.config.CacheConfiguration")); + return takesArgument(0, named("net.sf.ehcache.config.CacheConfiguration")) + .or(isPrivate().and(takesArgument(0, named("net.sf.ehcache.Cache")))); } @Override @@ -91,24 +95,24 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named(CLONE_CACHE_ENHANCE_METHOD); - } + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(CACHE_NAME_ENHANCE_METHOD).and(takesArgument(0, String.class)); + } - @Override - public String getMethodsInterceptor() { - return CLONE_CLASS_INTERCEPT_CLASS; - } + @Override + public String getMethodsInterceptor() { + return CACHE_NAME_INTERCEPTOR_CLASS; + } - @Override - public boolean isOverrideArgs() { + @Override + public boolean isOverrideArgs() { return false; } - }, - new InstanceMethodsInterceptPoint() { + }, + new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { return named(GET_WITH_LOADER_CACHE_ENHANCE_METHOD).or(named(GET_CACHE_ENHANCE_METHOD).and(takesArgument(0, Object.class))) diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java index 07b02a6a7..dba2c08ed 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java @@ -38,7 +38,6 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.powermock.reflect.Whitebox; -import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.CLONE_CACHE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.GET_ALL_CACHE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.GET_CACHE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.PUT_CACHE_ENHANCE_METHOD; @@ -46,6 +45,8 @@ import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginIn import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.READ_LOCK_TRY_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.WRITE_LOCK_RELEASE_ENHANCE_METHOD; import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.WRITE_LOCK_TRY_ENHANCE_METHOD; +import static org.apache.skywalking.apm.plugin.ehcache.v2.define.EhcachePluginInstrumentation.CACHE_NAME_ENHANCE_METHOD; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; @RunWith(PowerMockRunner.class) @@ -65,11 +66,12 @@ public class EhcacheInterceptorTest { private EhcacheOperateAllInterceptor operateAllInterceptor; private EhcacheLockInterceptor lockInterceptor; private EhcacheConstructorInterceptor constructorInterceptor; - private EhcacheCloneInterceptor cloneInterceptor; + private EhcacheCacheNameInterceptor cacheNameInterceptor; private Object[] operateObjectArguments; private Object[] operateElementArguments; private Object[] tryLockArguments; private Object[] releaseLockArguments; + private Object[] cacheNameArguments; private Exception exception; @@ -82,16 +84,19 @@ public class EhcacheInterceptorTest { private Method releaseReadLockMethod; private Method releaseWriteLockMethod; - private Method cloneMethod; + private Method setNameMethod; private EnhancedInstance enhancedInstance = new EnhancedInstance() { + EhcacheEnhanceInfo ehcacheEnhanceInfo; + @Override public Object getSkyWalkingDynamicField() { - return new EhcacheEnhanceInfo(CACHE_NAME); + return ehcacheEnhanceInfo; } @Override public void setSkyWalkingDynamicField(Object value) { + ehcacheEnhanceInfo = (EhcacheEnhanceInfo) value; } }; @@ -101,7 +106,7 @@ public class EhcacheInterceptorTest { operateElementInterceptor = new EhcacheOperateElementInterceptor(); operateAllInterceptor = new EhcacheOperateAllInterceptor(); constructorInterceptor = new EhcacheConstructorInterceptor(); - cloneInterceptor = new EhcacheCloneInterceptor(); + cacheNameInterceptor = new EhcacheCacheNameInterceptor(); lockInterceptor = new EhcacheLockInterceptor(); exception = new Exception(); @@ -113,6 +118,7 @@ public class EhcacheInterceptorTest { 3000 }; releaseLockArguments = new Object[] {"dataKey"}; + cacheNameArguments = new Object[] {"cacheName"}; putCacheMethod = Whitebox.getMethods(Cache.class, PUT_CACHE_ENHANCE_METHOD)[0]; getCacheMethod = Whitebox.getMethods(Cache.class, GET_CACHE_ENHANCE_METHOD)[0]; @@ -123,7 +129,9 @@ public class EhcacheInterceptorTest { releaseReadLockMethod = Whitebox.getMethods(Cache.class, READ_LOCK_RELEASE_ENHANCE_METHOD)[0]; releaseWriteLockMethod = Whitebox.getMethods(Cache.class, WRITE_LOCK_RELEASE_ENHANCE_METHOD)[0]; - cloneMethod = Whitebox.getMethods(Cache.class, CLONE_CACHE_ENHANCE_METHOD)[0]; + setNameMethod = Whitebox.getMethods(Cache.class, CACHE_NAME_ENHANCE_METHOD)[0]; + + enhancedInstance.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(CACHE_NAME)); } @Test @@ -132,10 +140,12 @@ public class EhcacheInterceptorTest { } @Test - public void assertClone() throws Throwable { - cloneInterceptor.beforeMethod(enhancedInstance, cloneMethod, null, null, null); - cloneInterceptor.handleMethodException(enhancedInstance, cloneMethod, null, null, exception); - cloneInterceptor.afterMethod(enhancedInstance, cloneMethod, null, null, null); + public void assertSetNameSuccess() throws Throwable { + cacheNameInterceptor.beforeMethod(enhancedInstance, setNameMethod, cacheNameArguments, null, null); + cacheNameInterceptor.handleMethodException(enhancedInstance, setNameMethod, null, null, exception); + cacheNameInterceptor.afterMethod(enhancedInstance, setNameMethod, cacheNameArguments, null, null); + + Assert.assertThat(((EhcacheEnhanceInfo) enhancedInstance.getSkyWalkingDynamicField()).getCacheName(), equalTo("cacheName")); } @Test diff --git a/test/plugin/scenarios/ehcache-2.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/ehcache-2.x-scenario/config/expectedData.yaml index b2a077a07..94e7a9e93 100644 --- a/test/plugin/scenarios/ehcache-2.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/ehcache-2.x-scenario/config/expectedData.yaml @@ -100,6 +100,21 @@ segmentItems: peerId: 0 tags: - {key: db.statement, value: dataKey} + - operationName: Ehcache/put/testCache2 + operationId: 0 + parentSpanId: 0 + spanId: 6 + spanLayer: Cache + startTime: nq 0 + endTime: nq 0 + componentId: 75 + componentName: '' + isError: false + spanType: Local + peer: '' + peerId: 0 + tags: + - {key: db.statement, value: dataKey} - operationName: /ehcache-2.x-scenario/case/ehcache operationId: 0 parentSpanId: -1 diff --git a/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java b/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java index 0d0a78dbd..60c998dbe 100644 --- a/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java +++ b/test/plugin/scenarios/ehcache-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/ehcache/v2/CaseServlet.java @@ -35,14 +35,7 @@ public class CaseServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - Cache originCache = cacheManager.getCache("testCache"); - - // EhcacheCloneInterceptor - Cache cache = null; - try { - cache = originCache.clone(); - } catch (CloneNotSupportedException e) { - } + Cache cache = cacheManager.getCache("testCache"); String objectKey = "dataKey"; @@ -65,6 +58,14 @@ public class CaseServlet extends HttpServlet { cache.releaseReadLockOnKey(objectKey); } + // EhcacheCacheNameInterceptor + cacheManager.addCacheIfAbsent("testCache2"); + + Cache cloneCache = cacheManager.getCache("testCache2"); + + // EhcacheOperateElementInterceptor + cloneCache.put(el); + PrintWriter printWriter = resp.getWriter(); printWriter.write("success"); printWriter.flush(); From b2ad63430bf9851e92e4d6bfcbcb7d96a6dcc6d7 Mon Sep 17 00:00:00 2001 From: lsyf Date: Wed, 19 Feb 2020 12:10:00 +0800 Subject: [PATCH 4/5] =?UTF-8?q?Fix=20ehcache:=C2=A0performance=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v2/EhcacheConstructorInterceptor.java | 18 +++------- .../EhcachePrivateConstructorInterceptor.java | 35 +++++++++++++++++++ .../define/EhcachePluginInstrumentation.java | 15 ++++++-- .../ehcache/v2/EhcacheInterceptorTest.java | 7 ++++ 4 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java index dd848d68a..1d4868aaf 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java @@ -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())); } } } diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java new file mode 100644 index 000000000..6610332bf --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java @@ -0,0 +1,35 @@ +/* + * 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())); + } + } +} diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java index bb741a175..1ef702a9a 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java @@ -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 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 getConstructorMatcher() { + return isPrivate().and(takesArgument(0, named("net.sf.ehcache.Cache"))); + } + + @Override + public String getConstructorInterceptor() { + return PRIVATE_CONSTRUCTOR_CLASS_INTERCEPT_CLASS; + } } }; } diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java index dba2c08ed..a612b9c3e 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java @@ -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); From 102c90dc59d5ce3cc20d26eb36124a17c9aeedf8 Mon Sep 17 00:00:00 2001 From: lsyf Date: Wed, 19 Feb 2020 17:23:33 +0800 Subject: [PATCH 5/5] =?UTF-8?q?Fix=20ehcache:=C2=A0format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ehcache/v2/define/EhcachePluginInstrumentation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java index 1ef702a9a..7f209b468 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java @@ -119,8 +119,8 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu @Override public boolean isOverrideArgs() { - return false; - } + return false; + } }, new InstanceMethodsInterceptPoint() {