Fix hbase onConstruct NPE in the file configuration scenario (#602)

This commit is contained in:
袁世超 2023-09-01 09:51:54 +08:00 committed by GitHub
parent 6e47084501
commit bc7447a2c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 2088 additions and 43 deletions

View File

@ -5,7 +5,7 @@ Release Notes.
9.1.0
------------------
* Fix hbase onConstruct NPE in the file configuration scenario
#### Documentation

View File

@ -18,13 +18,9 @@
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;
import org.apache.skywalking.apm.util.StringUtil;
public class HTable100Interceptor extends HTableInterceptor {
@ -38,13 +34,6 @@ public class HTable100Interceptor extends HTableInterceptor {
} else {
return;
}
Field field = configuration.getClass().getDeclaredField("overlay");
field.setAccessible(true);
Properties properties = (Properties) field.get(configuration);
String value = properties.getProperty("hbase.zookeeper.quorum");
if (StringUtil.isNotBlank(value)) {
objInst.setSkyWalkingDynamicField(value);
}
tryGetZookeeperQuorum(objInst, configuration);
}
}

View File

@ -18,24 +18,15 @@
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;
import org.apache.skywalking.apm.util.StringUtil;
public class HTable200Interceptor extends HTableInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
Configuration configuration = ((ClusterConnection) allArguments[0]).getConfiguration();
Field field = configuration.getClass().getDeclaredField("overlay");
field.setAccessible(true);
Properties properties = (Properties) field.get(configuration);
String value = properties.getProperty("hbase.zookeeper.quorum");
if (StringUtil.isNotBlank(value)) {
objInst.setSkyWalkingDynamicField(value);
}
tryGetZookeeperQuorum(objInst, configuration);
}
}

View File

@ -18,13 +18,10 @@
package org.apache.skywalking.apm.plugin.hbase;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.util.StringUtil;
public class HTable220Interceptor extends HTableInterceptor {
@ -32,12 +29,6 @@ public class HTable220Interceptor extends HTableInterceptor {
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
Method getConfigurationMethod = Connection.class.getMethod("getConfiguration");
Configuration configuration = (Configuration) getConfigurationMethod.invoke(allArguments[0]);
Field field = configuration.getClass().getDeclaredField("overlay");
field.setAccessible(true);
Properties properties = (Properties) field.get(configuration);
String value = properties.getProperty("hbase.zookeeper.quorum");
if (StringUtil.isNotBlank(value)) {
objInst.setSkyWalkingDynamicField(value);
}
tryGetZookeeperQuorum(objInst, configuration);
}
}

View File

@ -34,12 +34,11 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.util.CollectionUtil;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.util.StringUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class HTableInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
@ -107,16 +106,18 @@ public class HTableInterceptor implements InstanceMethodsAroundInterceptor, Inst
}
@Override
@SuppressWarnings("rawtypes")
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
Configuration connection = ((ClusterConnection) allArguments[1]).getConfiguration();
Field field = connection.getClass().getDeclaredField("overlay");
field.setAccessible(true);
Properties properties = (Properties) field.get(connection);
for (Map.Entry entry : properties.entrySet()) {
if ("hbase.zookeeper.quorum".equals(entry.getKey())) {
objInst.setSkyWalkingDynamicField(entry.getValue().toString());
}
tryGetZookeeperQuorum(objInst, connection);
}
protected void tryGetZookeeperQuorum(EnhancedInstance objInst, Configuration configuration) throws Exception {
Method method = configuration.getClass().getDeclaredMethod("getProps");
method.setAccessible(true);
Properties props = (Properties) method.invoke(configuration);
String value = (String) props.get("hbase.zookeeper.quorum");
if (StringUtil.isNotBlank(value)) {
objInst.setSkyWalkingDynamicField(value);
}
}
}

View File

@ -19,6 +19,7 @@
package org.apache.skywalking.apm.plugin.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.ClusterConnection;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.junit.Test;
@ -38,6 +39,15 @@ public class HTable100InterceptorTest {
@Mock
private ClusterConnection clusterConnection;
@Test
public void testOnConstructWithXml() throws Throwable {
HTable100Interceptor interceptor = new HTable100Interceptor();
Configuration configuration = HBaseConfiguration.create();
Object[] args = new Object[]{configuration, null};
interceptor.onConstruct(objectInstance, args);
verify(objectInstance).setSkyWalkingDynamicField("127.0.0.1");
}
@Test
public void testOnConstructWithConfiguration() throws Throwable {