Skip to content

Commit 2e08217

Browse files
authored
Fixed issues in the MySQL component where the executeBatch method could result in empty SQL statements . (#702)
1 parent 0fc3cd8 commit 2e08217

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Release Notes.
2121
* Fix the opentracing toolkit SPI config
2222
* Improve 4x performance of ContextManagerExtendService.createTraceContext()
2323
* Add a plugin that supports the Solon framework.
24+
* Fixed issues in the MySQL component where the executeBatch method could result in empty SQL statements .
2425

2526

2627
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/213?closed=1)

apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.skywalking.apm.plugin.jdbc.SqlBodyUtil;
2929
import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
3030
import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
31+
import org.apache.skywalking.apm.util.StringUtil;
3132

3233
import java.lang.reflect.Method;
3334

@@ -52,14 +53,17 @@ public final void beforeMethod(EnhancedInstance objInst, Method method, Object[]
5253
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
5354

5455
/**
55-
* The first argument of all intercept method in `com.mysql.jdbc.StatementImpl` class is SQL, except the
56-
* `executeBatch` method that the jdbc plugin need to trace, because of this method argument size is zero.
56+
* Except for the `executeBatch` method, the first parameter of all enhanced methods in `com.mysql.jdbc.StatementImpl` is the SQL statement.
57+
* Therefore, executeBatch will attempt to obtain the SQL from `cacheObject`.
5758
*/
5859
String sql = "";
5960
if (allArguments.length > 0) {
6061
sql = (String) allArguments[0];
6162
sql = SqlBodyUtil.limitSqlBodySize(sql);
63+
} else if (StringUtil.isNotBlank(cacheObject.getSql())) {
64+
sql = SqlBodyUtil.limitSqlBodySize(cacheObject.getSql());
6265
}
66+
6367
Tags.DB_STATEMENT.set(span, sql);
6468
span.setComponent(connectInfo.getComponent());
6569

apm-sniffer/apm-sdk-plugin/mysql-common/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public void setUp() {
7272
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048;
7373
serviceMethodInterceptor = new StatementExecuteMethodsInterceptor();
7474

75-
enhanceRequireCacheObject = new StatementEnhanceInfos(connectionInfo, "SELECT * FROM test", "CallableStatement");
75+
enhanceRequireCacheObject = new StatementEnhanceInfos(connectionInfo, SQL, "CallableStatement");
76+
7677
when(objectInstance.getSkyWalkingDynamicField()).thenReturn(enhanceRequireCacheObject);
7778
when(method.getName()).thenReturn("executeQuery");
7879
when(connectionInfo.getComponent()).thenReturn(ComponentsDefine.H2_JDBC_DRIVER);
@@ -81,6 +82,24 @@ public void setUp() {
8182
when(connectionInfo.getDatabasePeer()).thenReturn("localhost:3307");
8283
}
8384

85+
@Test
86+
public void testCreateDatabaseSpanWithNoMethodParamButWithCache() throws Throwable {
87+
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048;
88+
89+
serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[0], null, null);
90+
serviceMethodInterceptor.afterMethod(objectInstance, method, new Object[0], null, null);
91+
92+
assertThat(segmentStorage.getTraceSegments().size(), is(1));
93+
TraceSegment segment = segmentStorage.getTraceSegments().get(0);
94+
assertThat(SegmentHelper.getSpans(segment).size(), is(1));
95+
AbstractTracingSpan span = SegmentHelper.getSpans(segment).get(0);
96+
SpanAssert.assertLayer(span, SpanLayer.DB);
97+
assertThat(span.getOperationName(), is("H2/JDBC/CallableStatement/executeQuery"));
98+
SpanAssert.assertTag(span, 0, "H2");
99+
SpanAssert.assertTag(span, 1, "test");
100+
SpanAssert.assertTag(span, 2, SQL);
101+
}
102+
84103
@Test
85104
public void testCreateDatabaseSpan() throws Throwable {
86105
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048;

0 commit comments

Comments
 (0)