Limit Sql body size (#5626)
This commit is contained in:
parent
5ed9762db6
commit
c9b6a78ca9
|
|
@ -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.
|
||||
* 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.
|
||||
* <p>
|
||||
* 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 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.
|
||||
* <p>
|
||||
* Set a negative number to save the complete parameter string to the tag.
|
||||
*/
|
||||
public static int SQL_PARAMETERS_MAX_LENGTH = 512;
|
||||
public static int SQL_BODY_MAX_LENGTH = 2048;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ public class PSSetterDefinitionOfJDBCInstrumentation implements InstanceMethodsI
|
|||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
ElementMatcher.Junction<MethodDescription> 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<String> setters = ignorable ? PS_IGNORABLE_SETTERS : PS_SETTERS;
|
||||
for (String setter : setters) {
|
||||
matcher = matcher.or(named(setter));
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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]"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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]");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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...");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
||||
|
|
|
|||
|
|
@ -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,14 @@ 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);
|
||||
|
||||
|
|
@ -90,7 +100,25 @@ 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, 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);
|
||||
|
||||
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, "SELECT * F...");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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|
|
||||
|
|
|
|||
|
|
@ -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 &
|
||||
|
|
|
|||
Loading…
Reference in New Issue