From 54c1cd5cb04f012ff48c8b87c11df11041e3f499 Mon Sep 17 00:00:00 2001 From: lsyf Date: Mon, 17 Feb 2020 23:31:28 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20ehcache:=C2=A0=20missing=20interceptor=20?= =?UTF-8?q?of=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