Feature/fix spring data hadoop (#546)

This commit is contained in:
gzlicanyi 2023-06-06 16:21:03 +08:00 committed by GitHub
parent af4ddc61c9
commit 5002da5a13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 3 deletions

View File

@ -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

View File

@ -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);

View File

@ -76,8 +76,9 @@ public class HTableInstrumentation extends ClassInstanceMethodsEnhancePluginDefi
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> 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

View File

@ -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());
}
}