From 5002da5a130ad6e3bd0f889ce865aa2cd1a472f2 Mon Sep 17 00:00:00 2001 From: gzlicanyi Date: Tue, 6 Jun 2023 16:21:03 +0800 Subject: [PATCH] Feature/fix spring data hadoop (#546) --- CHANGES.md | 1 + .../plugin/hbase/HTable100Interceptor.java | 11 ++- .../hbase/define/HTableInstrumentation.java | 5 +- .../hbase/HTable100InterceptorTest.java | 74 +++++++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/hbase/HTable100InterceptorTest.java diff --git a/CHANGES.md b/CHANGES.md index 95d404c07..984fc42d8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ Release Notes. * Support Jdk17 ZGC metric collect * Support Jetty 11.x plugin +* Fix the scenario of using the HBase plugin with spring-data-hadoop. #### Documentation diff --git a/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable100Interceptor.java b/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable100Interceptor.java index 3b3528a8e..d53713a21 100644 --- a/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable100Interceptor.java +++ b/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable100Interceptor.java @@ -20,6 +20,7 @@ package org.apache.skywalking.apm.plugin.hbase; import java.lang.reflect.Field; import java.util.Properties; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.ClusterConnection; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; @@ -29,7 +30,15 @@ public class HTable100Interceptor extends HTableInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable { - Configuration configuration = ((ClusterConnection) allArguments[1]).getConfiguration(); + Configuration configuration; + if (allArguments[1] instanceof ClusterConnection) { + configuration = ((ClusterConnection) allArguments[1]).getConfiguration(); + } else if (allArguments[0] instanceof Configuration) { + configuration = (Configuration) allArguments[0]; + } else { + return; + } + Field field = configuration.getClass().getDeclaredField("overlay"); field.setAccessible(true); Properties properties = (Properties) field.get(configuration); diff --git a/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/define/HTableInstrumentation.java b/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/define/HTableInstrumentation.java index 7ca5d20d8..77326044a 100644 --- a/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/define/HTableInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/define/HTableInstrumentation.java @@ -76,8 +76,9 @@ public class HTableInstrumentation extends ClassInstanceMethodsEnhancePluginDefi new ConstructorInterceptPoint() { @Override public ElementMatcher getConstructorMatcher() { - return takesArguments(6) - .and(takesArgumentWithType(0, "org.apache.hadoop.hbase.TableName")); + return takesArguments(2) + .and(takesArgumentWithType(1, "org.apache.hadoop.hbase.TableName")).or(takesArguments(6) + .and(takesArgumentWithType(0, "org.apache.hadoop.hbase.TableName"))); } @Override diff --git a/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/hbase/HTable100InterceptorTest.java b/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/hbase/HTable100InterceptorTest.java new file mode 100644 index 000000000..dd29489f2 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/hbase/HTable100InterceptorTest.java @@ -0,0 +1,74 @@ +/* + * 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.hbase; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.client.ClusterConnection; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class HTable100InterceptorTest { + + @Mock + private EnhancedInstance objectInstance; + + @Mock + private ClusterConnection clusterConnection; + + @Test + public void testOnConstructWithConfiguration() throws Throwable { + + HTable100Interceptor interceptor = new HTable100Interceptor(); + + Configuration configuration = new Configuration(); + configuration.set("hbase.zookeeper.quorum", "localhost"); + + //test construct: public HTable(Configuration conf, final TableName tableName) throws IOException + Object[] args = new Object[]{configuration, null}; + interceptor.onConstruct(objectInstance, args); + verify(objectInstance).setSkyWalkingDynamicField(any()); + } + + @Test + public void testOnConstructWithConnection() throws Throwable { + HTable100Interceptor interceptor = new HTable100Interceptor(); + + Configuration configuration = new Configuration(); + configuration.set("hbase.zookeeper.quorum", "localhost"); + Mockito.when(clusterConnection.getConfiguration()).thenReturn(configuration); + + //test construct: public HTable(TableName tableName, final ClusterConnection connection, + // final ConnectionConfiguration tableConfig, + // final RpcRetryingCallerFactory rpcCallerFactory, + // final RpcControllerFactory rpcControllerFactory, + // final ExecutorService pool) throws IOException + + Object[] args = new Object[]{null, clusterConnection, null, null, null, null}; + interceptor.onConstruct(objectInstance, args); + verify(objectInstance).setSkyWalkingDynamicField(any()); + } + +} \ No newline at end of file