Add postgresql agent sql query param show (#3695)
* Add postgresql agent sql query param show
This commit is contained in:
parent
be3db6794b
commit
7f69a73d13
|
|
@ -273,6 +273,22 @@ public class Config {
|
|||
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,
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class PSSetterDefinitionOfJDBCInstrumentation implements InstanceMethodsI
|
|||
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));
|
||||
|
|
|
|||
|
|
@ -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 class StatementEnhanceInfos {
|
|||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 @@ import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
|
|||
* @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 class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
|
|||
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 class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
0
apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/Variables.java
Normal file → Executable file
0
apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/Variables.java
Normal file → Executable file
|
|
@ -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";
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
1
apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def
Normal file → Executable file
1
apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def
Normal file → Executable file
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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|
|
||||
|
|
|
|||
|
|
@ -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 &
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue