diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java index c8e04593ce9c..18280dfa3160 100755 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java @@ -273,6 +273,22 @@ public static class MySQL { public static int SQL_PARAMETERS_MAX_LENGTH = 512; } + 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 SolrJ { /** * If true, trace all the query parameters(include deleteByIds and deleteByQuery) in Solr query request, 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 fb7a4c060b07..7415254bedbd 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 @@ -45,7 +45,7 @@ public PSSetterDefinitionOfJDBCInstrumentation(boolean ignorable) { public ElementMatcher getMethodsMatcher() { ElementMatcher.Junction matcher = none(); - if (Config.Plugin.MySQL.TRACE_SQL_PARAMETERS) { + if (Config.Plugin.MySQL.TRACE_SQL_PARAMETERS || Config.Plugin.POSTGRESQL.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/define/StatementEnhanceInfos.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/define/StatementEnhanceInfos.java index 3ec22a4692c2..5c4a0a195f87 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/define/StatementEnhanceInfos.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/define/StatementEnhanceInfos.java @@ -19,9 +19,8 @@ package org.apache.skywalking.apm.plugin.jdbc.define; -import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; - import java.util.Arrays; +import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; /** * {@link StatementEnhanceInfos} contain the {@link ConnectionInfo} and @@ -58,7 +57,7 @@ public void setParameter(int index, final Object parameter) { maxIndex = maxIndex > index ? maxIndex : index; index--; // start from 1 if (parameters == null) { - final int initialSize = Math.max(20, maxIndex); + final int initialSize = Math.max(16, maxIndex); parameters = new Object[initialSize]; Arrays.fill(parameters, null); } diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/CreateCallableStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/CreateCallableStatementInterceptor.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/CreatePreparedStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/CreatePreparedStatementInterceptor.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/CreateStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/CreateStatementInterceptor.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/JDBCPrepareStatementWithStringArrayInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/JDBCPrepareStatementWithStringArrayInterceptor.java old mode 100644 new mode 100755 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 old mode 100644 new mode 100755 index 6ad648c24253..6686e6be0602 --- 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 @@ -20,7 +20,9 @@ package org.apache.skywalking.apm.plugin.jdbc.postgresql; import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.conf.Config; import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.StringTag; import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; @@ -36,6 +38,9 @@ * @author zhangxin */ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMethodsAroundInterceptor { + + public static final StringTag SQL_PARAMETERS = new StringTag("db.sql.parameters"); + @Override public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, @@ -48,6 +53,19 @@ public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] Tags.DB_STATEMENT.set(span, cacheObject.getSql()); span.setComponent(connectInfo.getComponent()); + if (Config.Plugin.POSTGRESQL.TRACE_SQL_PARAMETERS) { + final Object[] parameters = cacheObject.getParameters(); + if (parameters != null && parameters.length > 0) { + int maxIndex = cacheObject.getMaxIndex(); + String parameterString = buildParameterString(parameters, maxIndex); + int sqlParametersMaxLength = Config.Plugin.MySQL.SQL_PARAMETERS_MAX_LENGTH; + if (sqlParametersMaxLength > 0 && parameterString.length() > sqlParametersMaxLength) { + parameterString = parameterString.substring(0, sqlParametersMaxLength) + "..." + "]"; + } + SQL_PARAMETERS.set(span, parameterString); + } + } + SpanLayer.asDB(span); } @@ -73,4 +91,19 @@ public final Object afterMethod(EnhancedInstance objInst, Method method, Object[ private String buildOperationName(ConnectionInfo connectionInfo, String methodName, String statementName) { return connectionInfo.getDBType() + "/JDBI/" + statementName + "/" + methodName; } + + private String buildParameterString(Object[] parameters, int maxIndex) { + String parameterString = "["; + boolean first = true; + for (int i = 0; i < maxIndex; i++) { + Object parameter = parameters[i]; + if (!first) { + parameterString += ","; + } + parameterString += parameter; + first = false; + } + parameterString += "]"; + return parameterString; + } } 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 old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/Variables.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/Variables.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java old mode 100644 new mode 100755 index 662d12c1bfb4..cda27df745f2 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java @@ -28,5 +28,4 @@ public class Constants { public static final String CREATE_STATEMENT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.jdbc.postgresql.CreateStatementInterceptor"; public static final String CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.jdbc.postgresql.CreatePreparedStatementInterceptor"; public static final String CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.jdbc.postgresql.CreateCallableStatementInterceptor"; - } diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/DriverInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/DriverInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc3ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc3ConnectionInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc4ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc4ConnectionInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgCallableStatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgCallableStatementInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgPreparedStatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgPreparedStatementInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgPreparedStatementSetterInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgPreparedStatementSetterInstrumentation.java new file mode 100644 index 000000000000..3893b43d09ce --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgPreparedStatementSetterInstrumentation.java @@ -0,0 +1,35 @@ +/* + * 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.postgresql.define; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.plugin.jdbc.PSSetterDefinitionOfJDBCInstrumentation; + +/** + * @author aderm + */ +public class PgPreparedStatementSetterInstrumentation extends PgPreparedStatementInstrumentation { + + @Override + public final InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new PSSetterDefinitionOfJDBCInstrumentation(false) + }; + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgStatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/PgStatementInstrumentation.java old mode 100644 new mode 100755 diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def old mode 100644 new mode 100755 index c1e3e0de6f27..e744f204fcfe --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def @@ -22,3 +22,4 @@ postgresql-8.x=org.apache.skywalking.apm.plugin.jdbc.postgresql.define.AbstractJ postgresql-8.x=org.apache.skywalking.apm.plugin.jdbc.postgresql.define.PgCallableStatementInstrumentation postgresql-8.x=org.apache.skywalking.apm.plugin.jdbc.postgresql.define.PgPreparedStatementInstrumentation postgresql-8.x=org.apache.skywalking.apm.plugin.jdbc.postgresql.define.PgStatementInstrumentation +postgresql-8.x=org.apache.skywalking.apm.plugin.jdbc.postgresql.define.PgPreparedStatementSetterInstrumentation diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index e5586140a738..979837a4792d 100755 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -102,6 +102,8 @@ property key | Description | Default | `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.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 b5f8537b0aec..ff5042d00303 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} ${home}/../libs/postgresql-above9.4.1207-scenario.jar & +java -jar ${agent_opts} -Dskywalking.plugin.postgresql.trace_sql_parameters=true ${home}/../libs/postgresql-above9.4.1207-scenario.jar & diff --git a/test/plugin/scenarios/postgresql-above9.4.1207-scenario/config/expectedData.yaml b/test/plugin/scenarios/postgresql-above9.4.1207-scenario/config/expectedData.yaml index efa343ecf18d..bb5260be15ce 100644 --- a/test/plugin/scenarios/postgresql-above9.4.1207-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/postgresql-above9.4.1207-scenario/config/expectedData.yaml @@ -53,6 +53,7 @@ segmentItems: - {key: "db.type", value: "sql"} - {key: "db.instance", value: "postgres"} - {key: "db.statement", value: "INSERT INTO test_007(id, value) VALUES(?,?)"} + - {key: "db.sql.parameters", value: "[1,1]"} startTime: nq 0 endTime: nq 0 isError: false