Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues in the MySQL component where the executeBatch method could result in empty SQL statements . #702

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Release Notes.
* Fix the opentracing toolkit SPI config
* Improve 4x performance of ContextManagerExtendService.createTraceContext()
* Add a plugin that supports the Solon framework.
* Fixed issues in the MySQL component where the executeBatch method could result in empty SQL statements .


All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/213?closed=1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.skywalking.apm.plugin.jdbc.SqlBodyUtil;
import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
import org.apache.skywalking.apm.util.StringUtil;

import java.lang.reflect.Method;

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

/**
* The first argument of all intercept method in `com.mysql.jdbc.StatementImpl` class is SQL, except the
* `executeBatch` method that the jdbc plugin need to trace, because of this method argument size is zero.
* Except for the `executeBatch` method, the first parameter of all enhanced methods in `com.mysql.jdbc.StatementImpl` is the SQL statement.
* Therefore, executeBatch will attempt to obtain the SQL from `cacheObject`.
*/
String sql = "";
if (allArguments.length > 0) {
sql = (String) allArguments[0];
sql = SqlBodyUtil.limitSqlBodySize(sql);
} else if (StringUtil.isNotBlank(cacheObject.getSql())) {
sql = SqlBodyUtil.limitSqlBodySize(cacheObject.getSql());
}

Tags.DB_STATEMENT.set(span, sql);
span.setComponent(connectInfo.getComponent());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void setUp() {
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048;
serviceMethodInterceptor = new StatementExecuteMethodsInterceptor();

enhanceRequireCacheObject = new StatementEnhanceInfos(connectionInfo, "SELECT * FROM test", "CallableStatement");
enhanceRequireCacheObject = new StatementEnhanceInfos(connectionInfo, SQL, "CallableStatement");

when(objectInstance.getSkyWalkingDynamicField()).thenReturn(enhanceRequireCacheObject);
when(method.getName()).thenReturn("executeQuery");
when(connectionInfo.getComponent()).thenReturn(ComponentsDefine.H2_JDBC_DRIVER);
Expand All @@ -81,6 +82,24 @@ public void setUp() {
when(connectionInfo.getDatabasePeer()).thenReturn("localhost:3307");
}

@Test
public void testCreateDatabaseSpanWithNoMethodParamButWithCache() throws Throwable {
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048;

serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[0], null, null);
serviceMethodInterceptor.afterMethod(objectInstance, method, new Object[0], null, null);

assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment segment = segmentStorage.getTraceSegments().get(0);
assertThat(SegmentHelper.getSpans(segment).size(), is(1));
AbstractTracingSpan span = SegmentHelper.getSpans(segment).get(0);
SpanAssert.assertLayer(span, SpanLayer.DB);
assertThat(span.getOperationName(), is("H2/JDBC/CallableStatement/executeQuery"));
SpanAssert.assertTag(span, 0, "H2");
SpanAssert.assertTag(span, 1, "test");
SpanAssert.assertTag(span, 2, SQL);
}

@Test
public void testCreateDatabaseSpan() throws Throwable {
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048;
Expand Down
Loading