Skip to content

Commit 91386d4

Browse files
authored
Merge pull request apache#637 from ascrutae/fix/support-postgresql-plugin
Change the mechanism of postgresql plugin
2 parents 07f8257 + cef99e8 commit 91386d4

File tree

16 files changed

+381
-23
lines changed

16 files changed

+381
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Project repository: https://github.com/OpenSkywalking/skywalking
1717
*/
1818

19-
package org.skywalking.apm.plugin.jdbc.mysql.define;
19+
package org.skywalking.apm.plugin.jdbc.define;
2020

2121
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
2222

apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateCallableStatementInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2323
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2424
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25-
import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos;
25+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
2626
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
2727

2828
/**

apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreatePreparedStatementInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2323
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2424
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25-
import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos;
25+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
2626
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
2727

2828
/**

apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateStatementInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2323
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2424
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25-
import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos;
25+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
2626
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
2727

2828
/**

apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2727
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2828
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29-
import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos;
29+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
3030
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
3131

3232
/**

apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.skywalking.apm.agent.test.tools.SpanAssert;
3838
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner;
3939
import org.skywalking.apm.network.trace.component.ComponentsDefine;
40-
import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos;
40+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
4141
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
4242

4343
import static org.hamcrest.CoreMatchers.is;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.jdbc.postgresql;
20+
21+
import java.lang.reflect.Method;
22+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
23+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
24+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
26+
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
27+
28+
/**
29+
* {@link CreateStatementInterceptor} intercepts the {@link org.postgresql.jdbc.PgConnection#prepareCall} method in
30+
* {@link org.postgresql.jdbc.PgConnection}, {@link org.postgresql.jdbc.PgConnection} or {@link org.postgresql.jdbc3.Jdbc3Connection} class.
31+
*
32+
* @author zhangxin
33+
*/
34+
public class CreateCallableStatementInterceptor implements InstanceMethodsAroundInterceptor {
35+
@Override
36+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
37+
MethodInterceptResult result) throws Throwable {
38+
39+
}
40+
41+
@Override
42+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
43+
Object ret) throws Throwable {
44+
if (ret instanceof EnhancedInstance) {
45+
((EnhancedInstance)ret).setSkyWalkingDynamicField(new StatementEnhanceInfos((ConnectionInfo)objInst.getSkyWalkingDynamicField(), (String)allArguments[0], "CallableStatement"));
46+
}
47+
return ret;
48+
}
49+
50+
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
51+
Class<?>[] argumentsTypes, Throwable t) {
52+
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.jdbc.postgresql;
20+
21+
import java.lang.reflect.Method;
22+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
23+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
24+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
26+
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
27+
28+
/**
29+
* {@link CreatePreparedStatementInterceptor} intercepts the {@link org.postgresql.jdbc.PgConnection#prepareStatement} method in
30+
* {@link org.postgresql.jdbc.PgConnection}, {@link org.postgresql.jdbc.PgConnection} or {@link org.postgresql.jdbc3.Jdbc3Connection} class.
31+
*
32+
* @author zhangxin
33+
*/
34+
public class CreatePreparedStatementInterceptor implements InstanceMethodsAroundInterceptor {
35+
@Override
36+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
37+
MethodInterceptResult result) throws Throwable {
38+
39+
}
40+
41+
@Override
42+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
43+
Object ret) throws Throwable {
44+
if (ret instanceof EnhancedInstance) {
45+
((EnhancedInstance)ret).setSkyWalkingDynamicField(new StatementEnhanceInfos((ConnectionInfo)objInst.getSkyWalkingDynamicField(), (String)allArguments[0], "PreparedStatement"));
46+
}
47+
return ret;
48+
}
49+
50+
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
51+
Class<?>[] argumentsTypes, Throwable t) {
52+
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.jdbc.postgresql;
20+
21+
import java.lang.reflect.Method;
22+
import org.postgresql.jdbc.PgConnection;
23+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
24+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
25+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
26+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
27+
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
28+
29+
/**
30+
* {@link CreateStatementInterceptor} intercepts the {@link PgConnection#createStatement} method in
31+
* {@link org.postgresql.jdbc.PgConnection}, {@link org.postgresql.jdbc.PgConnection} or {@link org.postgresql.jdbc3.Jdbc3Connection} class.
32+
*
33+
* @author zhangxin
34+
*/
35+
public class CreateStatementInterceptor implements InstanceMethodsAroundInterceptor {
36+
@Override
37+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
38+
MethodInterceptResult result) throws Throwable {
39+
40+
}
41+
42+
@Override
43+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
44+
Object ret) throws Throwable {
45+
46+
if (ret instanceof EnhancedInstance) {
47+
((EnhancedInstance)ret).setSkyWalkingDynamicField(new StatementEnhanceInfos((ConnectionInfo)objInst.getSkyWalkingDynamicField(), "", "CallableStatement"));
48+
}
49+
50+
return ret;
51+
}
52+
53+
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
54+
Class<?>[] argumentsTypes, Throwable t) {
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.jdbc.postgresql;
20+
21+
import java.lang.reflect.Method;
22+
import org.skywalking.apm.agent.core.context.ContextManager;
23+
import org.skywalking.apm.agent.core.context.tag.Tags;
24+
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
25+
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
26+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
28+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29+
import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
30+
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
31+
32+
/**
33+
* {@link StatementExecuteMethodsInterceptor} create the exit span when the client call the interceptor methods.
34+
*
35+
* @author zhangxin
36+
*/
37+
public class StatementExecuteMethodsInterceptor implements InstanceMethodsAroundInterceptor {
38+
@Override
39+
public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
40+
Class<?>[] argumentsTypes,
41+
MethodInterceptResult result) throws Throwable {
42+
StatementEnhanceInfos cacheObject = (StatementEnhanceInfos)objInst.getSkyWalkingDynamicField();
43+
ConnectionInfo connectInfo = cacheObject.getConnectionInfo();
44+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(connectInfo, method.getName(), cacheObject.getStatementName()), connectInfo.getDatabasePeer());
45+
Tags.DB_TYPE.set(span, "sql");
46+
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
47+
Tags.DB_STATEMENT.set(span, cacheObject.getSql());
48+
span.setComponent(connectInfo.getComponent());
49+
50+
SpanLayer.asDB(span);
51+
}
52+
53+
@Override
54+
public final Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
55+
Class<?>[] argumentsTypes,
56+
Object ret) throws Throwable {
57+
StatementEnhanceInfos cacheObject = (StatementEnhanceInfos)objInst.getSkyWalkingDynamicField();
58+
if (cacheObject.getConnectionInfo() != null) {
59+
ContextManager.stopSpan();
60+
}
61+
return ret;
62+
}
63+
64+
@Override public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
65+
Class<?>[] argumentsTypes, Throwable t) {
66+
StatementEnhanceInfos cacheObject = (StatementEnhanceInfos)objInst.getSkyWalkingDynamicField();
67+
if (cacheObject.getConnectionInfo() != null) {
68+
ContextManager.activeSpan().errorOccurred().log(t);
69+
}
70+
}
71+
72+
private String buildOperationName(ConnectionInfo connectionInfo, String methodName, String statementName) {
73+
return connectionInfo.getDBType() + "/JDBI/" + statementName + "/" + methodName;
74+
}
75+
}

0 commit comments

Comments
 (0)