Skip to content

Commit bc7447a

Browse files
authored
Fix hbase onConstruct NPE in the file configuration scenario (#602)
1 parent 6e47084 commit bc7447a

7 files changed

Lines changed: 2088 additions & 43 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Release Notes.
55
9.1.0
66
------------------
77

8-
8+
* Fix hbase onConstruct NPE in the file configuration scenario
99

1010
#### Documentation
1111

apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable100Interceptor.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818

1919
package org.apache.skywalking.apm.plugin.hbase;
2020

21-
import java.lang.reflect.Field;
22-
import java.util.Properties;
23-
2421
import org.apache.hadoop.conf.Configuration;
2522
import org.apache.hadoop.hbase.client.ClusterConnection;
2623
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27-
import org.apache.skywalking.apm.util.StringUtil;
2824

2925
public class HTable100Interceptor extends HTableInterceptor {
3026

@@ -38,13 +34,6 @@ public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws
3834
} else {
3935
return;
4036
}
41-
42-
Field field = configuration.getClass().getDeclaredField("overlay");
43-
field.setAccessible(true);
44-
Properties properties = (Properties) field.get(configuration);
45-
String value = properties.getProperty("hbase.zookeeper.quorum");
46-
if (StringUtil.isNotBlank(value)) {
47-
objInst.setSkyWalkingDynamicField(value);
48-
}
37+
tryGetZookeeperQuorum(objInst, configuration);
4938
}
5039
}

apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable200Interceptor.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,15 @@
1818

1919
package org.apache.skywalking.apm.plugin.hbase;
2020

21-
import java.lang.reflect.Field;
22-
import java.util.Properties;
2321
import org.apache.hadoop.conf.Configuration;
2422
import org.apache.hadoop.hbase.client.ClusterConnection;
2523
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
26-
import org.apache.skywalking.apm.util.StringUtil;
2724

2825
public class HTable200Interceptor extends HTableInterceptor {
2926

3027
@Override
3128
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
3229
Configuration configuration = ((ClusterConnection) allArguments[0]).getConfiguration();
33-
Field field = configuration.getClass().getDeclaredField("overlay");
34-
field.setAccessible(true);
35-
Properties properties = (Properties) field.get(configuration);
36-
String value = properties.getProperty("hbase.zookeeper.quorum");
37-
if (StringUtil.isNotBlank(value)) {
38-
objInst.setSkyWalkingDynamicField(value);
39-
}
30+
tryGetZookeeperQuorum(objInst, configuration);
4031
}
4132
}

apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTable220Interceptor.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,17 @@
1818

1919
package org.apache.skywalking.apm.plugin.hbase;
2020

21-
import java.lang.reflect.Field;
2221
import java.lang.reflect.Method;
23-
import java.util.Properties;
2422
import org.apache.hadoop.conf.Configuration;
2523
import org.apache.hadoop.hbase.client.Connection;
2624
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27-
import org.apache.skywalking.apm.util.StringUtil;
2825

2926
public class HTable220Interceptor extends HTableInterceptor {
3027

3128
@Override
3229
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
3330
Method getConfigurationMethod = Connection.class.getMethod("getConfiguration");
3431
Configuration configuration = (Configuration) getConfigurationMethod.invoke(allArguments[0]);
35-
Field field = configuration.getClass().getDeclaredField("overlay");
36-
field.setAccessible(true);
37-
Properties properties = (Properties) field.get(configuration);
38-
String value = properties.getProperty("hbase.zookeeper.quorum");
39-
if (StringUtil.isNotBlank(value)) {
40-
objInst.setSkyWalkingDynamicField(value);
41-
}
32+
tryGetZookeeperQuorum(objInst, configuration);
4233
}
4334
}

apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hbase/HTableInterceptor.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@
3434
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
3535
import org.apache.skywalking.apm.agent.core.util.CollectionUtil;
3636
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
37+
import org.apache.skywalking.apm.util.StringUtil;
3738

38-
import java.lang.reflect.Field;
3939
import java.lang.reflect.Method;
4040
import java.nio.charset.StandardCharsets;
4141
import java.util.List;
42-
import java.util.Map;
4342
import java.util.Properties;
4443

4544
public class HTableInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
@@ -107,16 +106,18 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
107106
}
108107

109108
@Override
110-
@SuppressWarnings("rawtypes")
111109
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
112110
Configuration connection = ((ClusterConnection) allArguments[1]).getConfiguration();
113-
Field field = connection.getClass().getDeclaredField("overlay");
114-
field.setAccessible(true);
115-
Properties properties = (Properties) field.get(connection);
116-
for (Map.Entry entry : properties.entrySet()) {
117-
if ("hbase.zookeeper.quorum".equals(entry.getKey())) {
118-
objInst.setSkyWalkingDynamicField(entry.getValue().toString());
119-
}
111+
tryGetZookeeperQuorum(objInst, connection);
112+
}
113+
114+
protected void tryGetZookeeperQuorum(EnhancedInstance objInst, Configuration configuration) throws Exception {
115+
Method method = configuration.getClass().getDeclaredMethod("getProps");
116+
method.setAccessible(true);
117+
Properties props = (Properties) method.invoke(configuration);
118+
String value = (String) props.get("hbase.zookeeper.quorum");
119+
if (StringUtil.isNotBlank(value)) {
120+
objInst.setSkyWalkingDynamicField(value);
120121
}
121122
}
122123
}

apm-sniffer/apm-sdk-plugin/hbase-1.x-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/hbase/HTable100InterceptorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.skywalking.apm.plugin.hbase;
2020

2121
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.hadoop.hbase.HBaseConfiguration;
2223
import org.apache.hadoop.hbase.client.ClusterConnection;
2324
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2425
import org.junit.Test;
@@ -38,6 +39,15 @@ public class HTable100InterceptorTest {
3839
@Mock
3940
private ClusterConnection clusterConnection;
4041

42+
@Test
43+
public void testOnConstructWithXml() throws Throwable {
44+
HTable100Interceptor interceptor = new HTable100Interceptor();
45+
Configuration configuration = HBaseConfiguration.create();
46+
Object[] args = new Object[]{configuration, null};
47+
interceptor.onConstruct(objectInstance, args);
48+
verify(objectInstance).setSkyWalkingDynamicField("127.0.0.1");
49+
}
50+
4151
@Test
4252
public void testOnConstructWithConfiguration() throws Throwable {
4353

0 commit comments

Comments
 (0)