From c9b6a78ca9340bdf94de76be791177d57958a0b3 Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Sun, 4 Oct 2020 23:22:32 +0800 Subject: [PATCH] Limit Sql body size (#5626) --- .../apm/plugin/jdbc/JDBCPluginConfig.java | 36 ++----------- ...SetterDefinitionOfJDBCInstrumentation.java | 2 +- .../PreparedStatementParameterBuilder.java | 12 ++--- .../apm/plugin/jdbc/SqlBodyUtil.java | 40 ++++++++++++++ ...PreparedStatementParameterBuilderTest.java | 7 +-- .../apm/plugin/jdbc/SqlBodyUtilTest.java | 53 +++++++++++++++++++ ...redStatementExecuteMethodsInterceptor.java | 6 +-- .../StatementExecuteMethodsInterceptor.java | 2 + ...tatementExecuteMethodsInterceptorTest.java | 39 +++++++++++++- ...tatementExecuteMethodsInterceptorTest.java | 24 +++++++++ ...redStatementExecuteMethodsInterceptor.java | 6 +-- .../StatementExecuteMethodsInterceptor.java | 3 +- ...tatementExecuteMethodsInterceptorTest.java | 30 ++++++++++- ...redStatementExecuteMethodsInterceptor.java | 6 +-- .../StatementExecuteMethodsInterceptor.java | 5 +- .../setup/service-agent/java-agent/README.md | 9 ++-- .../bin/startup.sh | 2 +- 17 files changed, 218 insertions(+), 64 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtil.java create mode 100644 apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtilTest.java diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/JDBCPluginConfig.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/JDBCPluginConfig.java index 9a22acc351..8b8b953a0a 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/JDBCPluginConfig.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/JDBCPluginConfig.java @@ -23,7 +23,7 @@ import org.apache.skywalking.apm.agent.core.boot.PluginConfig; public class JDBCPluginConfig { public static class Plugin { @PluginConfig(root = JDBCPluginConfig.class) - public static class MySQL { + public static class JDBC { /** * If set to true, the parameters of the sql (typically {@link java.sql.PreparedStatement}) would be * collected. @@ -36,39 +36,13 @@ public class JDBCPluginConfig { * Set a negative number to save the complete parameter string to the tag. */ public static int SQL_PARAMETERS_MAX_LENGTH = 512; - } - - @PluginConfig(root = JDBCPluginConfig.class) - public static class POSTGRESQL { - /** - * If set to true, the parameters of the sql (typically {@link java.sql.PreparedStatement}) would be - * collected. - */ - public static boolean TRACE_SQL_PARAMETERS = false; - - /** - * For the sake of performance, SkyWalking won't save the entire parameters string into the tag, but only - * the first {@code SQL_PARAMETERS_MAX_LENGTH} characters. - *

- * Set a negative number to save the complete parameter string to the tag. - */ - public static int SQL_PARAMETERS_MAX_LENGTH = 512; - } - - public static class MARIADB { - /** - * If set to true, the parameters of the sql (typically {@link java.sql.PreparedStatement}) would be - * collected. - */ - public static boolean TRACE_SQL_PARAMETERS = false; - /** - * For the sake of performance, SkyWalking won't save the entire parameters string into the tag, but only - * the first {@code SQL_PARAMETERS_MAX_LENGTH} characters. + * For the sake of performance, SkyWalking won't save the entire sql body into the tag, but only the first + * {@code SQL_BODY_MAX_LENGTH} characters. *

- * Set a negative number to save the complete parameter string to the tag. + * Set a negative number to save the complete sql body to the tag. */ - public static int SQL_PARAMETERS_MAX_LENGTH = 512; + public static int SQL_BODY_MAX_LENGTH = 2048; } } } diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PSSetterDefinitionOfJDBCInstrumentation.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PSSetterDefinitionOfJDBCInstrumentation.java index 43fe947b74..8ee16bf554 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PSSetterDefinitionOfJDBCInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PSSetterDefinitionOfJDBCInstrumentation.java @@ -40,7 +40,7 @@ public class PSSetterDefinitionOfJDBCInstrumentation implements InstanceMethodsI public ElementMatcher getMethodsMatcher() { ElementMatcher.Junction matcher = none(); - if (JDBCPluginConfig.Plugin.MySQL.TRACE_SQL_PARAMETERS || JDBCPluginConfig.Plugin.POSTGRESQL.TRACE_SQL_PARAMETERS || JDBCPluginConfig.Plugin.MARIADB.TRACE_SQL_PARAMETERS) { + if (JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS) { final Set setters = ignorable ? PS_IGNORABLE_SETTERS : PS_SETTERS; for (String setter : setters) { matcher = matcher.or(named(setter)); diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilder.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilder.java index 33bda28783..3fe899dffe 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilder.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilder.java @@ -22,7 +22,6 @@ public class PreparedStatementParameterBuilder { private static final String EMPTY_LIST = "[]"; private Object[] parameters; private Integer maxIndex; - private int maxLength = 0; public PreparedStatementParameterBuilder setParameters(Object[] parameters) { this.parameters = parameters; @@ -34,11 +33,6 @@ public class PreparedStatementParameterBuilder { return this; } - public PreparedStatementParameterBuilder setMaxLength(int maxLength) { - this.maxLength = maxLength; - return this; - } - public String build() { if (parameters == null) { return EMPTY_LIST; @@ -57,10 +51,10 @@ public class PreparedStatementParameterBuilder { } stringBuilder.append(parameter); first = false; - + // cut the string as soon as it reached the length limitation - if (maxLength > 0 && (stringBuilder.length() + EMPTY_LIST.length()) > maxLength) { - return format(stringBuilder).substring(0, maxLength) + "..."; + if (JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH > 0 && (stringBuilder.length() + EMPTY_LIST.length()) > JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH) { + return format(stringBuilder).substring(0, JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH) + "..."; } } return format(stringBuilder); diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtil.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtil.java new file mode 100644 index 0000000000..a847da6f1b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtil.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.jdbc; + +/** + * Sql body utility + */ +public class SqlBodyUtil { + private static final String EMPTY_STRING = ""; + + /** + * Limit sql body size to specify {@code JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH} + * @param sql Sql to limit + */ + public static String limitSqlBodySize(String sql) { + if (sql == null) { + return EMPTY_STRING; + } + if (JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH > 0 && sql.length() > JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH) { + return sql.substring(0, JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH) + "..."; + } + return sql; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilderTest.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilderTest.java index 11d6fa39cc..1df692166e 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilderTest.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/PreparedStatementParameterBuilderTest.java @@ -69,6 +69,7 @@ public class PreparedStatementParameterBuilderTest { @Test public void testParametersString() { + JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH = 0; builder = new PreparedStatementParameterBuilder(); Object[] parameters = new Object[]{ "", @@ -87,7 +88,7 @@ public class PreparedStatementParameterBuilderTest { public void testMaxLength() { builder = new PreparedStatementParameterBuilder(); builder.setParameters(PARAMETERS); - builder.setMaxLength(10); + JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH = 10; assertThat(builder.build(), is("[test,1234...")); } @@ -95,7 +96,7 @@ public class PreparedStatementParameterBuilderTest { public void testMaxLengthZero() { builder = new PreparedStatementParameterBuilder(); builder.setParameters(PARAMETERS); - builder.setMaxLength(0); + JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH = 0; assertThat(builder.build(), is("[test,1234]")); } @@ -103,7 +104,7 @@ public class PreparedStatementParameterBuilderTest { public void testMaxLengthOutOfRange() { builder = new PreparedStatementParameterBuilder(); builder.setParameters(PARAMETERS); - builder.setMaxLength(20); + JDBCPluginConfig.Plugin.JDBC.SQL_PARAMETERS_MAX_LENGTH = 20; assertThat(builder.build(), is("[test,1234]")); } } diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtilTest.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtilTest.java new file mode 100644 index 0000000000..6945a34403 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/SqlBodyUtilTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.jdbc; + +import org.junit.After; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class SqlBodyUtilTest { + + @Test + public void testBuildWithEmptySqlBody() { + String sql = SqlBodyUtil.limitSqlBodySize(null); + assertThat(sql, is("")); + } + + @Test + public void testBuildWithDefaultLength() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; + String sql = SqlBodyUtil.limitSqlBodySize("select * from dual"); + assertThat(sql, is("select * from dual")); + } + + @Test + public void testBuildWithMaxLength() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 10; + String sql = SqlBodyUtil.limitSqlBodySize("select * from dual"); + assertThat(sql, is("select * f...")); + } + + @After + public void clean() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptor.java index 195dca04fe..a2b0fff116 100644 --- a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptor.java @@ -28,6 +28,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; import org.apache.skywalking.apm.plugin.jdbc.PreparedStatementParameterBuilder; +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; @@ -47,10 +48,10 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho .getStatementName()), connectInfo.getDatabasePeer()); Tags.DB_TYPE.set(span, "sql"); Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); - Tags.DB_STATEMENT.set(span, cacheObject.getSql()); + Tags.DB_STATEMENT.set(span, SqlBodyUtil.limitSqlBodySize(cacheObject.getSql())); span.setComponent(connectInfo.getComponent()); - if (JDBCPluginConfig.Plugin.MARIADB.TRACE_SQL_PARAMETERS) { + if (JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS) { final Object[] parameters = cacheObject.getParameters(); if (parameters != null && parameters.length > 0) { int maxIndex = cacheObject.getMaxIndex(); @@ -88,7 +89,6 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho return new PreparedStatementParameterBuilder() .setParameters(parameters) .setMaxIndex(maxIndex) - .setMaxLength(JDBCPluginConfig.Plugin.MARIADB.SQL_PARAMETERS_MAX_LENGTH) .build(); } } diff --git a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptor.java index 87d55dd100..d5ecc1bee3 100644 --- a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptor.java @@ -25,6 +25,7 @@ import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +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; @@ -42,6 +43,7 @@ public class StatementExecuteMethodsInterceptor implements InstanceMethodsAround Tags.DB_TYPE.set(span, "sql"); Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); String sql = allArguments.length > 0 ? (String) allArguments[0] : ""; + sql = SqlBodyUtil.limitSqlBodySize(sql); Tags.DB_STATEMENT.set(span, sql); span.setComponent(connectInfo.getComponent()); SpanLayer.asDB(span); diff --git a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptorTest.java index c603f4731a..4bce7fb8be 100644 --- a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/PreparedStatementExecuteMethodsInterceptorTest.java @@ -34,6 +34,7 @@ import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; import org.apache.skywalking.apm.plugin.jdbc.JDBCPreparedStatementSetterInterceptor; import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -72,7 +73,7 @@ public class PreparedStatementExecuteMethodsInterceptorTest { @Before public void setUp() { - JDBCPluginConfig.Plugin.MARIADB.TRACE_SQL_PARAMETERS = true; + JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS = true; preparedStatementSetterInterceptor = new JDBCPreparedStatementSetterInterceptor(); serviceMethodInterceptor = new PreparedStatementExecuteMethodsInterceptor(); @@ -85,6 +86,12 @@ public class PreparedStatementExecuteMethodsInterceptorTest { when(connectionInfo.getDatabasePeer()).thenReturn("localhost:3306"); } + @After + public void clean() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; + JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS = false; + } + @Test public void testExecutePreparedStatement() throws Throwable { preparedStatementSetterInterceptor.beforeMethod( @@ -113,4 +120,34 @@ public class PreparedStatementExecuteMethodsInterceptorTest { SpanAssert.assertTag(span, 3, "[abcd,efgh]"); } + @Test + public void testExecutePreparedStatementWithLimitSqlBody() throws Throwable { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 10; + + preparedStatementSetterInterceptor.beforeMethod( + objectInstance, method, new Object[] { + 1, + "abcd" + }, null, null); + preparedStatementSetterInterceptor.beforeMethod( + objectInstance, method, new Object[] { + 2, + "efgh" + }, null, null); + + serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[] {SQL}, null, null); + serviceMethodInterceptor.afterMethod(objectInstance, method, new Object[] {SQL}, 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("Mariadb/JDBI/PreparedStatement/")); + SpanAssert.assertTag(span, 0, "sql"); + SpanAssert.assertTag(span, 1, "test"); + SpanAssert.assertTag(span, 2, "Select * f..."); + SpanAssert.assertTag(span, 3, "[abcd,efgh]"); + } + } diff --git a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptorTest.java index 9b877bb6ca..b9d4925c2a 100644 --- a/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mariadb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mariadb/v2/StatementExecuteMethodsInterceptorTest.java @@ -29,8 +29,10 @@ import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.apache.skywalking.apm.agent.test.tools.SpanAssert; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -80,6 +82,11 @@ public class StatementExecuteMethodsInterceptorTest { when(connectionInfo.getDatabasePeer()).thenReturn("localhost:3306"); } + @After + public void clean() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; + } + @Test public void testExecuteStatement() { serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[]{SQL}, null, null); @@ -96,4 +103,21 @@ public class StatementExecuteMethodsInterceptorTest { SpanAssert.assertTag(span, 2, SQL); } + @Test + public void testExecuteStatementWithLimitSqlBody() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 10; + serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[]{SQL}, null, null); + serviceMethodInterceptor.afterMethod(objectInstance, method, new Object[]{SQL}, 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("Mariadb/JDBI/CallableStatement/")); + SpanAssert.assertTag(span, 0, "sql"); + SpanAssert.assertTag(span, 1, "test"); + SpanAssert.assertTag(span, 2, "Select * f..."); + } + } diff --git a/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/PreparedStatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/PreparedStatementExecuteMethodsInterceptor.java index 26c243a76a..23b6c68b45 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/PreparedStatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/PreparedStatementExecuteMethodsInterceptor.java @@ -28,6 +28,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; import org.apache.skywalking.apm.plugin.jdbc.PreparedStatementParameterBuilder; +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; @@ -53,10 +54,10 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho .getStatementName()), connectInfo.getDatabasePeer()); Tags.DB_TYPE.set(span, "sql"); Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); - Tags.DB_STATEMENT.set(span, cacheObject.getSql()); + Tags.DB_STATEMENT.set(span, SqlBodyUtil.limitSqlBodySize(cacheObject.getSql())); span.setComponent(connectInfo.getComponent()); - if (JDBCPluginConfig.Plugin.MySQL.TRACE_SQL_PARAMETERS) { + if (JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS) { final Object[] parameters = cacheObject.getParameters(); if (parameters != null && parameters.length > 0) { int maxIndex = cacheObject.getMaxIndex(); @@ -96,7 +97,6 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho return new PreparedStatementParameterBuilder() .setParameters(parameters) .setMaxIndex(maxIndex) - .setMaxLength(JDBCPluginConfig.Plugin.MySQL.SQL_PARAMETERS_MAX_LENGTH) .build(); } } diff --git a/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java index 57052d13f8..cbda3336cd 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java @@ -25,6 +25,7 @@ import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +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; @@ -57,8 +58,8 @@ public class StatementExecuteMethodsInterceptor implements InstanceMethodsAround String sql = ""; if (allArguments.length > 0) { sql = (String) allArguments[0]; + sql = SqlBodyUtil.limitSqlBodySize(sql); } - Tags.DB_STATEMENT.set(span, sql); span.setComponent(connectInfo.getComponent()); diff --git a/apm-sniffer/apm-sdk-plugin/mysql-common/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mysql-common/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java index 2a09c4708a..1d07845913 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-common/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-common/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java @@ -30,8 +30,10 @@ import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; import org.apache.skywalking.apm.agent.test.tools.SpanAssert; import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -48,6 +50,8 @@ import static org.powermock.api.mockito.PowerMockito.when; @PowerMockRunnerDelegate(TracingSegmentRunner.class) public class StatementExecuteMethodsInterceptorTest { + private static final String SQL = "SELECT * FROM test"; + @SegmentStoragePoint private SegmentStorage segmentStorage; @@ -77,8 +81,32 @@ public class StatementExecuteMethodsInterceptorTest { when(connectionInfo.getDatabasePeer()).thenReturn("localhost:3307"); } + @After + public void clean() { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; + } + @Test public void testCreateDatabaseSpan() throws Throwable { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; + serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[] {"SELECT * FROM test"}, null, null); + serviceMethodInterceptor.afterMethod(objectInstance, method, new Object[] {"SELECT * FROM test"}, 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/JDBI/CallableStatement/")); + SpanAssert.assertTag(span, 0, "sql"); + SpanAssert.assertTag(span, 1, "test"); + SpanAssert.assertTag(span, 2, SQL); + } + + @Test + public void testCreateDatabaseSpanWithLimitSqlBody() throws Throwable { + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 10; + serviceMethodInterceptor.beforeMethod(objectInstance, method, new Object[] {"SELECT * FROM test"}, null, null); serviceMethodInterceptor.afterMethod(objectInstance, method, new Object[] {"SELECT * FROM test"}, null, null); @@ -90,7 +118,7 @@ public class StatementExecuteMethodsInterceptorTest { assertThat(span.getOperationName(), is("H2/JDBI/CallableStatement/")); SpanAssert.assertTag(span, 0, "sql"); SpanAssert.assertTag(span, 1, "test"); - SpanAssert.assertTag(span, 2, "SELECT * FROM test"); + SpanAssert.assertTag(span, 2, "SELECT * F..."); } } diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/PreparedStatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/PreparedStatementExecuteMethodsInterceptor.java index 9045ad6ade..e827be9b7b 100755 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/PreparedStatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/PreparedStatementExecuteMethodsInterceptor.java @@ -29,6 +29,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; import org.apache.skywalking.apm.plugin.jdbc.PreparedStatementParameterBuilder; +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; @@ -50,10 +51,10 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho .getDatabasePeer()); Tags.DB_TYPE.set(span, "sql"); Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); - Tags.DB_STATEMENT.set(span, cacheObject.getSql()); + Tags.DB_STATEMENT.set(span, SqlBodyUtil.limitSqlBodySize(cacheObject.getSql())); span.setComponent(connectInfo.getComponent()); - if (JDBCPluginConfig.Plugin.POSTGRESQL.TRACE_SQL_PARAMETERS) { + if (JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS) { final Object[] parameters = cacheObject.getParameters(); if (parameters != null && parameters.length > 0) { int maxIndex = cacheObject.getMaxIndex(); @@ -92,7 +93,6 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho return new PreparedStatementParameterBuilder() .setParameters(parameters) .setMaxIndex(maxIndex) - .setMaxLength(JDBCPluginConfig.Plugin.POSTGRESQL.SQL_PARAMETERS_MAX_LENGTH) .build(); } } diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java index c1a76ac255..a66e5d8cde 100755 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java @@ -26,6 +26,7 @@ import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +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; @@ -42,7 +43,9 @@ public class StatementExecuteMethodsInterceptor implements InstanceMethodsAround .getDatabasePeer()); Tags.DB_TYPE.set(span, "sql"); Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); - Tags.DB_STATEMENT.set(span, (String) allArguments[0]); + String sql = (String) allArguments[0]; + sql = SqlBodyUtil.limitSqlBodySize(sql); + Tags.DB_STATEMENT.set(span, sql); span.setComponent(connectInfo.getComponent()); SpanLayer.asDB(span); diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index 970fa984b1..b32e780604 100755 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -121,12 +121,9 @@ property key | Description | Default | `plugin.elasticsearch.trace_dsl`|If true, trace all the DSL(Domain Specific Language) in ElasticSearch access, default is false.|`false`| `plugin.springmvc.use_qualified_name_as_endpoint_name`|If true, the fully qualified method name will be used as the endpoint name instead of the request URL, default is false.|`false`| `plugin.toolit.use_qualified_name_as_operation_name`|If true, the fully qualified method name will be used as the operation name instead of the given operation name, default is false.|`false`| -`plugin.mysql.trace_sql_parameters`|If set to true, the parameters of the sql (typically `java.sql.PreparedStatement`) would be collected.|`false`| -`plugin.mysql.sql_parameters_max_length`|If set to positive number, the `db.sql.parameters` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|`512`| -`plugin.postgresql.trace_sql_parameters`|If set to true, the parameters of the sql (typically `java.sql.PreparedStatement`) would be collected.|`false`| -`plugin.postgresql.sql_parameters_max_length`|If set to positive number, the `db.sql.parameters` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|`512`| -`plugin.mariadb.trace_sql_parameters`|If set to true, the parameters of the sql (typically `java.sql.PreparedStatement`) would be collected.|`false`| -`plugin.mariadb.sql_parameters_max_length`|If set to positive number, the `db.sql.parameters` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|`512`| +`plugin.jdbc.trace_sql_parameters`|If set to true, the parameters of the sql (typically `java.sql.PreparedStatement`) would be collected.|`false`| +`plugin.jdbc.sql_parameters_max_length`|If set to positive number, the `db.sql.parameters` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|`512`| +`plugin.jdbc.sql_body_max_length`|If set to positive number, the `db.statement` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|`2048`| `plugin.solrj.trace_statement`|If true, trace all the query parameters(include deleteByIds and deleteByQuery) in Solr query request, default is false.|`false`| `plugin.solrj.trace_ops_params`|If true, trace all the operation parameters in Solr request, default is false.|`false`| `plugin.light4j.trace_handler_chain`|If true, trace all middleware/business handlers that are part of the Light4J handler chain for a request.|false| diff --git a/test/plugin/scenarios/postgresql-above9.4.1207-scenario/bin/startup.sh b/test/plugin/scenarios/postgresql-above9.4.1207-scenario/bin/startup.sh index ff5042d003..70b8fd2941 100644 --- a/test/plugin/scenarios/postgresql-above9.4.1207-scenario/bin/startup.sh +++ b/test/plugin/scenarios/postgresql-above9.4.1207-scenario/bin/startup.sh @@ -17,4 +17,4 @@ home="$(cd "$(dirname $0)"; pwd)" -java -jar ${agent_opts} -Dskywalking.plugin.postgresql.trace_sql_parameters=true ${home}/../libs/postgresql-above9.4.1207-scenario.jar & +java -jar ${agent_opts} -Dskywalking.plugin.jdbc.trace_sql_parameters=true ${home}/../libs/postgresql-above9.4.1207-scenario.jar & -- GitLab