Skip to content

Commit 5002da5

Browse files
authored
Feature/fix spring data hadoop (#546)
1 parent af4ddc6 commit 5002da5

4 files changed

Lines changed: 88 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Release Notes.
77

88
* Support Jdk17 ZGC metric collect
99
* Support Jetty 11.x plugin
10+
* Fix the scenario of using the HBase plugin with spring-data-hadoop.
1011

1112
#### Documentation
1213

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.lang.reflect.Field;
2222
import java.util.Properties;
23+
2324
import org.apache.hadoop.conf.Configuration;
2425
import org.apache.hadoop.hbase.client.ClusterConnection;
2526
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
@@ -29,7 +30,15 @@ public class HTable100Interceptor extends HTableInterceptor {
2930

3031
@Override
3132
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
32-
Configuration configuration = ((ClusterConnection) allArguments[1]).getConfiguration();
33+
Configuration configuration;
34+
if (allArguments[1] instanceof ClusterConnection) {
35+
configuration = ((ClusterConnection) allArguments[1]).getConfiguration();
36+
} else if (allArguments[0] instanceof Configuration) {
37+
configuration = (Configuration) allArguments[0];
38+
} else {
39+
return;
40+
}
41+
3342
Field field = configuration.getClass().getDeclaredField("overlay");
3443
field.setAccessible(true);
3544
Properties properties = (Properties) field.get(configuration);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
7676
new ConstructorInterceptPoint() {
7777
@Override
7878
public ElementMatcher<MethodDescription> getConstructorMatcher() {
79-
return takesArguments(6)
80-
.and(takesArgumentWithType(0, "org.apache.hadoop.hbase.TableName"));
79+
return takesArguments(2)
80+
.and(takesArgumentWithType(1, "org.apache.hadoop.hbase.TableName")).or(takesArguments(6)
81+
.and(takesArgumentWithType(0, "org.apache.hadoop.hbase.TableName")));
8182
}
8283

8384
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.hbase;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.hadoop.hbase.client.ClusterConnection;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Mock;
27+
import org.mockito.Mockito;
28+
import org.mockito.junit.MockitoJUnitRunner;
29+
import static org.mockito.ArgumentMatchers.any;
30+
import static org.mockito.Mockito.verify;
31+
32+
@RunWith(MockitoJUnitRunner.class)
33+
public class HTable100InterceptorTest {
34+
35+
@Mock
36+
private EnhancedInstance objectInstance;
37+
38+
@Mock
39+
private ClusterConnection clusterConnection;
40+
41+
@Test
42+
public void testOnConstructWithConfiguration() throws Throwable {
43+
44+
HTable100Interceptor interceptor = new HTable100Interceptor();
45+
46+
Configuration configuration = new Configuration();
47+
configuration.set("hbase.zookeeper.quorum", "localhost");
48+
49+
//test construct: public HTable(Configuration conf, final TableName tableName) throws IOException
50+
Object[] args = new Object[]{configuration, null};
51+
interceptor.onConstruct(objectInstance, args);
52+
verify(objectInstance).setSkyWalkingDynamicField(any());
53+
}
54+
55+
@Test
56+
public void testOnConstructWithConnection() throws Throwable {
57+
HTable100Interceptor interceptor = new HTable100Interceptor();
58+
59+
Configuration configuration = new Configuration();
60+
configuration.set("hbase.zookeeper.quorum", "localhost");
61+
Mockito.when(clusterConnection.getConfiguration()).thenReturn(configuration);
62+
63+
//test construct: public HTable(TableName tableName, final ClusterConnection connection,
64+
// final ConnectionConfiguration tableConfig,
65+
// final RpcRetryingCallerFactory rpcCallerFactory,
66+
// final RpcControllerFactory rpcControllerFactory,
67+
// final ExecutorService pool) throws IOException
68+
69+
Object[] args = new Object[]{null, clusterConnection, null, null, null, null};
70+
interceptor.onConstruct(objectInstance, args);
71+
verify(objectInstance).setSkyWalkingDynamicField(any());
72+
}
73+
74+
}

0 commit comments

Comments
 (0)