Skip to content

Commit 032f4c7

Browse files
authored
Merge pull request apache#562 from ascrutae/fix/mysql-issue
fix occur ClassCastException when call the method of return generate key
2 parents 72adebd + 2dcb7b5 commit 032f4c7

26 files changed

+933
-106
lines changed

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/ConnectionServiceMethodInterceptor.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2828
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
2929
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
30-
import org.skywalking.apm.util.StringUtil;
3130

3231
/**
33-
* {@link ConnectionServiceMethodInterceptor} create an exit span when the client call the following methods in the class
34-
* that extend {@link java.sql.Connection}.
32+
* {@link ConnectionServiceMethodInterceptor} create an exit span when the following methods execute:
3533
* 1. close
3634
* 2. rollback
3735
* 3. releaseSavepoint
3836
* 4. commit
37+
*
3938
* @author zhangxin
4039
*/
4140
public class ConnectionServiceMethodInterceptor implements InstanceMethodsAroundInterceptor {
@@ -45,13 +44,7 @@ public final void beforeMethod(EnhancedInstance objInst, Method method, Object[]
4544
Class<?>[] argumentsTypes,
4645
MethodInterceptResult result) throws Throwable {
4746
ConnectionInfo connectInfo = (ConnectionInfo)objInst.getSkyWalkingDynamicField();
48-
String remotePeer;
49-
if (!StringUtil.isEmpty(connectInfo.getHosts())) {
50-
remotePeer = connectInfo.getHosts();
51-
} else {
52-
remotePeer = connectInfo.getHost() + ":" + connectInfo.getPort();
53-
}
54-
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/Connection/" + method.getName(), remotePeer);
47+
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/Connection/" + method.getName(), connectInfo.getDatabasePeer());
5548
Tags.DB_TYPE.set(span, "sql");
5649
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
5750
Tags.DB_STATEMENT.set(span, "");

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/trace/CallableStatementTracing.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.skywalking.apm.agent.core.context.tag.Tags;
2424
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
2525
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
26-
import org.skywalking.apm.util.StringUtil;
2726

2827
/**
2928
* {@link CallableStatementTracing} create an exit span when the client call the method in the class that extend {@link
@@ -37,13 +36,7 @@ public static <R> R execute(java.sql.CallableStatement realStatement,
3736
ConnectionInfo connectInfo, String method, String sql, Executable<R> exec)
3837
throws SQLException {
3938
try {
40-
String remotePeer;
41-
if (!StringUtil.isEmpty(connectInfo.getHosts())) {
42-
remotePeer = connectInfo.getHosts();
43-
} else {
44-
remotePeer = connectInfo.getHost() + ":" + connectInfo.getPort();
45-
}
46-
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/CallableStatement/" + method, remotePeer);
39+
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/CallableStatement/" + method, connectInfo.getDatabasePeer());
4740
Tags.DB_TYPE.set(span, "sql");
4841
SpanLayer.asDB(span);
4942
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/trace/ConnectionInfo.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,12 @@ public class ConnectionInfo {
3131
* DB type, such as mysql, oracle, h2.
3232
*/
3333
private final String dbType;
34-
/**
35-
* Database host name.
36-
*/
37-
private String host;
38-
/**
39-
* Database port.
40-
*/
41-
private int port;
4234
/**
4335
* Operation database name.
4436
*/
4537
private final String databaseName;
46-
/**
47-
* Database hosts.
48-
*/
49-
private String hosts;
38+
39+
private String databasePeer;
5040

5141
/**
5242
* Component
@@ -55,15 +45,14 @@ public class ConnectionInfo {
5545

5646
public ConnectionInfo(OfficialComponent component, String dbType, String host, int port, String databaseName) {
5747
this.dbType = dbType;
58-
this.host = host;
59-
this.port = port;
48+
this.databasePeer = host + ":" + port;
6049
this.databaseName = databaseName;
6150
this.component = component;
6251
}
6352

6453
public ConnectionInfo(OfficialComponent component, String dbType, String hosts, String databaseName) {
6554
this.dbType = dbType;
66-
this.hosts = hosts;
55+
this.databasePeer = hosts;
6756
this.databaseName = databaseName;
6857
this.component = component;
6958
}
@@ -72,20 +61,12 @@ public String getDBType() {
7261
return dbType;
7362
}
7463

75-
public String getHost() {
76-
return host;
77-
}
78-
79-
public int getPort() {
80-
return port;
81-
}
82-
8364
public String getDatabaseName() {
8465
return databaseName;
8566
}
8667

87-
public String getHosts() {
88-
return hosts;
68+
public String getDatabasePeer() {
69+
return databasePeer;
8970
}
9071

9172
public OfficialComponent getComponent() {

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/trace/PreparedStatementTracing.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.skywalking.apm.agent.core.context.tag.Tags;
2424
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
2525
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
26-
import org.skywalking.apm.util.StringUtil;
2726

2827
/**
2928
* {@link PreparedStatementTracing} create an exit span when the client call the method in the class that extend {@link
@@ -37,14 +36,7 @@ public static <R> R execute(java.sql.PreparedStatement realStatement,
3736
ConnectionInfo connectInfo, String method, String sql, Executable<R> exec)
3837
throws SQLException {
3938
try {
40-
String remotePeer;
41-
if (!StringUtil.isEmpty(connectInfo.getHosts())) {
42-
remotePeer = connectInfo.getHosts();
43-
} else {
44-
remotePeer = connectInfo.getHost() + ":" + connectInfo.getPort();
45-
}
46-
47-
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/PreparedStatement/" + method, remotePeer);
39+
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/PreparedStatement/" + method, connectInfo.getDatabasePeer());
4840
Tags.DB_TYPE.set(span, "sql");
4941
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
5042
Tags.DB_STATEMENT.set(span, sql);

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/trace/StatementTracing.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.skywalking.apm.agent.core.context.tag.Tags;
2424
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
2525
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
26-
import org.skywalking.apm.util.StringUtil;
2726

2827
/**
2928
* {@link PreparedStatementTracing} create an exit span when the client call the method in the class that extend {@link
@@ -36,14 +35,7 @@ public static <R> R execute(java.sql.Statement realStatement,
3635
ConnectionInfo connectInfo, String method, String sql, Executable<R> exec)
3736
throws SQLException {
3837
try {
39-
String remotePeer;
40-
if (!StringUtil.isEmpty(connectInfo.getHosts())) {
41-
remotePeer = connectInfo.getHosts();
42-
} else {
43-
remotePeer = connectInfo.getHost() + ":" + connectInfo.getPort();
44-
}
45-
46-
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/Statement/" + method, remotePeer);
38+
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/Statement/" + method, connectInfo.getDatabasePeer());
4739
Tags.DB_TYPE.set(span, "sql");
4840
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
4941
Tags.DB_STATEMENT.set(span, sql);

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/skywalking/apm/plugin/jdbc/ConnectionTracing.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,14 @@
2424
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
2525
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
2626
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
27-
import org.skywalking.apm.util.StringUtil;
2827

2928
public class ConnectionTracing {
3029

3130
public static <R> R execute(java.sql.Connection realConnection,
3231
ConnectionInfo connectInfo, String method, String sql, Executable<R> exec)
3332
throws SQLException {
3433
try {
35-
String remotePeer;
36-
if (!StringUtil.isEmpty(connectInfo.getHosts())) {
37-
remotePeer = connectInfo.getHosts();
38-
} else {
39-
remotePeer = connectInfo.getHost() + ":" + connectInfo.getPort();
40-
}
41-
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/Connection/" + method, remotePeer);
34+
AbstractSpan span = ContextManager.createExitSpan(connectInfo.getDBType() + "/JDBI/Connection/" + method, connectInfo.getDatabasePeer());
4235
Tags.DB_TYPE.set(span, "sql");
4336
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
4437
Tags.DB_STATEMENT.set(span, sql);

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,95 +30,86 @@ public void testParseMysqlJDBCURLWithHost() {
3030
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql//primaryhost/test");
3131
assertThat(connectionInfo.getDBType(), is("Mysql"));
3232
assertThat(connectionInfo.getDatabaseName(), is("test"));
33-
assertThat(connectionInfo.getHost(), is("primaryhost"));
34-
assertThat(connectionInfo.getPort(), is(3306));
33+
assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3306"));
3534
}
3635

3736
@Test
3837
public void testParseMysqlJDBCURLWithHostAndPort() {
3938
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql//primaryhost:3307/test?profileSQL=true");
4039
assertThat(connectionInfo.getDBType(), is("Mysql"));
4140
assertThat(connectionInfo.getDatabaseName(), is("test"));
42-
assertThat(connectionInfo.getHost(), is("primaryhost"));
43-
assertThat(connectionInfo.getPort(), is(3307));
41+
assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3307"));
4442
}
4543

4644
@Test
4745
public void testParseMysqlJDBCURLWithMultiHost() {
4846
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql//primaryhost:3307,secondaryhost1,secondaryhost2/test?profileSQL=true");
4947
assertThat(connectionInfo.getDBType(), is("Mysql"));
5048
assertThat(connectionInfo.getDatabaseName(), is("test"));
51-
assertThat(connectionInfo.getHosts(), is("primaryhost:3307,secondaryhost1:3306,secondaryhost2:3306,"));
49+
assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3307,secondaryhost1:3306,secondaryhost2:3306,"));
5250
}
5351

5452
@Test
5553
public void testParseMysqlJDBCURLWithConnectorJs() {
5654
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql:replication://master,slave1,slave2,slave3/test");
5755
assertThat(connectionInfo.getDBType(), is("Mysql"));
5856
assertThat(connectionInfo.getDatabaseName(), is("test"));
59-
assertThat(connectionInfo.getHosts(), is("master:3306,slave1:3306,slave2:3306,slave3:3306,"));
57+
assertThat(connectionInfo.getDatabasePeer(), is("master:3306,slave1:3306,slave2:3306,slave3:3306,"));
6058
}
6159

6260
@Test
6361
public void testParseOracleJDBCURLWithHost() {
6462
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@localhost:orcl");
6563
assertThat(connectionInfo.getDBType(), is("Oracle"));
6664
assertThat(connectionInfo.getDatabaseName(), is("orcl"));
67-
assertThat(connectionInfo.getHost(), is("localhost"));
68-
assertThat(connectionInfo.getPort(), is(1521));
65+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:1521"));
6966
}
7067

7168
@Test
7269
public void testParseOracleJDBCURLWithHostAndPort() {
7370
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@localhost:1522:orcl");
7471
assertThat(connectionInfo.getDBType(), is("Oracle"));
7572
assertThat(connectionInfo.getDatabaseName(), is("orcl"));
76-
assertThat(connectionInfo.getHost(), is("localhost"));
77-
assertThat(connectionInfo.getPort(), is(1522));
73+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:1522"));
7874
}
7975

8076
@Test
8177
public void testParseOracleJDBCURLWithUserNameAndPassword() {
8278
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:scott/tiger@myhost:1521:orcl");
8379
assertThat(connectionInfo.getDBType(), is("Oracle"));
8480
assertThat(connectionInfo.getDatabaseName(), is("orcl"));
85-
assertThat(connectionInfo.getHost(), is("myhost"));
86-
assertThat(connectionInfo.getPort(), is(1521));
81+
assertThat(connectionInfo.getDatabasePeer(), is("myhost:1521"));
8782
}
8883

8984
@Test
9085
public void testParseH2JDBCURLWithEmbedded() {
9186
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:file:/data/sample");
9287
assertThat(connectionInfo.getDBType(), is("H2"));
9388
assertThat(connectionInfo.getDatabaseName(), is("/data/sample"));
94-
assertThat(connectionInfo.getHost(), is("localhost"));
95-
assertThat(connectionInfo.getPort(), is(-1));
89+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1"));
9690
}
9791

9892
@Test
9993
public void testParseH2JDBCURLWithEmbeddedRunningInWindows() {
10094
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:file:C:/data/sample");
10195
assertThat(connectionInfo.getDBType(), is("H2"));
10296
assertThat(connectionInfo.getDatabaseName(), is("C:/data/sample"));
103-
assertThat(connectionInfo.getHost(), is("localhost"));
104-
assertThat(connectionInfo.getPort(), is(-1));
97+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1"));
10598
}
10699

107100
@Test
108101
public void testParseH2JDBCURLWithMemoryMode() {
109102
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:mem:test_mem");
110103
assertThat(connectionInfo.getDBType(), is("H2"));
111104
assertThat(connectionInfo.getDatabaseName(), is("test_mem"));
112-
assertThat(connectionInfo.getHost(), is("localhost"));
113-
assertThat(connectionInfo.getPort(), is(-1));
105+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1"));
114106
}
115107

116108
@Test
117109
public void testParseH2JDBCURL() {
118110
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:tcp://localhost:8084/~/sample");
119111
assertThat(connectionInfo.getDBType(), is("H2"));
120112
assertThat(connectionInfo.getDatabaseName(), is("sample"));
121-
assertThat(connectionInfo.getHost(), is("localhost"));
122-
assertThat(connectionInfo.getPort(), is(8084));
113+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:8084"));
123114
}
124115
}

apm-sniffer/apm-sdk-plugin/mysql-2.x-plugin/src/main/resources/skywalking-plugin.def

Lines changed: 0 additions & 2 deletions
This file was deleted.

apm-sniffer/apm-sdk-plugin/mysql-2.x-plugin/pom.xml renamed to apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
</parent>
2626
<modelVersion>4.0.0</modelVersion>
2727

28-
<artifactId>apm-mysql-2.x-plugin</artifactId>
28+
<artifactId>apm-mysql-5.x-plugin</artifactId>
2929
<packaging>jar</packaging>
3030

31-
<name>mysql-2.x-plugin</name>
31+
<name>mysql-5.x-plugin</name>
3232
<url>http://maven.apache.org</url>
3333

3434
<properties>
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>mysql</groupId>
4747
<artifactId>mysql-connector-java</artifactId>
48-
<version>[2.0.14,6.0.6]</version>
48+
<version>[5.1.22,6.0.6]</version>
4949
<scope>provided</scope>
5050
</dependency>
5151
<dependency>
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.mysql;
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.mysql.define.StatementEnhanceInfos;
26+
import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
27+
28+
/**
29+
* {@link CreateStatementInterceptor} intercepts the {@link com.mysql.jdbc.ConnectionImpl#createStatement()} method in
30+
* the {@link com.mysql.jdbc.ConnectionImpl} 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+
}

0 commit comments

Comments
 (0)