Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public PSSetterDefinitionOfJDBCInstrumentation(boolean ignorable) {
public ElementMatcher<MethodDescription> getMethodsMatcher() {
ElementMatcher.Junction<MethodDescription> matcher = none();

if (Config.Plugin.MySQL.TRACE_SQL_PARAMETERS) {
if (Config.Plugin.MySQL.TRACE_SQL_PARAMETERS || Config.Plugin.POSTGRESQL.TRACE_SQL_PARAMETERS) {
final Set<String> setters = ignorable ? PS_IGNORABLE_SETTERS : PS_SETTERS;
for (String setter : setters) {
matcher = matcher.or(named(setter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
33 changes: 33 additions & 0 deletions ...che/skywalking/apm/plugin/jdbc/postgresql/PreparedStatementExecuteMethodsInterceptor.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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);
}

Expand All @@ -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;
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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)
};
}

}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions docs/en/setup/service-agent/java-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down