未验证 提交 04bb6673 编写于 作者: A AngryMills 提交者: GitHub

support parameter collection for sqlserver (#7217)

上级 f32d3d07
...@@ -23,6 +23,7 @@ Release Notes. ...@@ -23,6 +23,7 @@ Release Notes.
* Add `Neo4j-4.x` plugin. * Add `Neo4j-4.x` plugin.
* Correct `profile.duration` to `profile.max_duration` in the default `agent.config` file. * Correct `profile.duration` to `profile.max_duration` in the default `agent.config` file.
* Fix the response time of gRPC. * Fix the response time of gRPC.
* Support parameter collection for SqlServer.
* Add `ShardingSphere-5.0.0-beta` plugin. * Add `ShardingSphere-5.0.0-beta` plugin.
* Fix some method exception error. * Fix some method exception error.
......
...@@ -18,10 +18,14 @@ ...@@ -18,10 +18,14 @@
package org.apache.skywalking.apm.plugin.mssql.commons; package org.apache.skywalking.apm.plugin.mssql.commons;
import org.apache.skywalking.apm.agent.core.context.tag.StringTag;
public class Constants { public class Constants {
public static final String CREATE_CALLABLE_STATEMENT_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.CreateCallableStatementInterceptor"; public static final String CREATE_CALLABLE_STATEMENT_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.CreateCallableStatementInterceptor";
public static final String CREATE_PREPARED_STATEMENT_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.CreatePreparedStatementInterceptor"; public static final String CREATE_PREPARED_STATEMENT_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.CreatePreparedStatementInterceptor";
public static final String CREATE_STATEMENT_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.CreateStatementInterceptor"; public static final String CREATE_STATEMENT_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.CreateStatementInterceptor";
public static final String PREPARED_STATEMENT_EXECUTE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.PreparedStatementExecuteMethodsInterceptor"; public static final String PREPARED_STATEMENT_EXECUTE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.PreparedStatementExecuteMethodsInterceptor";
public static final String STATEMENT_EXECUTE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.StatementExecuteMethodsInterceptor"; public static final String STATEMENT_EXECUTE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.mssql.commons.StatementExecuteMethodsInterceptor";
public static final StringTag SQL_PARAMETERS = new StringTag("db.sql.parameters");
} }
...@@ -26,6 +26,8 @@ import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; ...@@ -26,6 +26,8 @@ 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.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; 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.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.SqlBodyUtil;
import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.apache.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos;
import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
...@@ -47,6 +49,13 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho ...@@ -47,6 +49,13 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName());
Tags.DB_STATEMENT.set(span, SqlBodyUtil.limitSqlBodySize(cacheObject.getSql())); Tags.DB_STATEMENT.set(span, SqlBodyUtil.limitSqlBodySize(cacheObject.getSql()));
span.setComponent(connectInfo.getComponent()); span.setComponent(connectInfo.getComponent());
if (JDBCPluginConfig.Plugin.JDBC.TRACE_SQL_PARAMETERS) {
final Object[] parameters = cacheObject.getParameters();
if (parameters != null && parameters.length > 0) {
int maxIndex = cacheObject.getMaxIndex();
Constants.SQL_PARAMETERS.set(span, getParameterString(parameters, maxIndex));
}
}
SpanLayer.asDB(span); SpanLayer.asDB(span);
} }
} }
...@@ -73,4 +82,11 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho ...@@ -73,4 +82,11 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
private String buildOperationName(ConnectionInfo connectionInfo, String methodName, String statementName) { private String buildOperationName(ConnectionInfo connectionInfo, String methodName, String statementName) {
return connectionInfo.getDBType() + "/JDBI/" + statementName + "/" + methodName; return connectionInfo.getDBType() + "/JDBI/" + statementName + "/" + methodName;
} }
private String getParameterString(Object[] parameters, int maxIndex) {
return new PreparedStatementParameterBuilder()
.setParameters(parameters)
.setMaxIndex(maxIndex)
.build();
}
} }
/*
* 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.mssql.jdbc.define;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.plugin.jdbc.PSSetterDefinitionOfJDBCInstrumentation;
public class PreparedStatementIgnoredSetterInstrumentation extends PreparedStatementInstrumentation {
@Override
public final InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new PSSetterDefinitionOfJDBCInstrumentation(true)
};
}
}
/*
* 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.mssql.jdbc.define;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.plugin.jdbc.JDBCPreparedStatementNullSetterInstanceMethodsInterceptPoint;
public class PreparedStatementNullSetterInstrumentation extends PreparedStatementInstrumentation {
@Override
public final InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new JDBCPreparedStatementNullSetterInstanceMethodsInterceptPoint()
};
}
}
/*
* 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.mssql.jdbc.define;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.plugin.jdbc.PSSetterDefinitionOfJDBCInstrumentation;
public class PreparedStatementSetterInstrumentation extends PreparedStatementInstrumentation {
@Override
public final InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new PSSetterDefinitionOfJDBCInstrumentation(false)
};
}
}
...@@ -17,4 +17,7 @@ ...@@ -17,4 +17,7 @@
mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.DriverInstrumentation mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.DriverInstrumentation
mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.ConnectionInstrumentation mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.ConnectionInstrumentation
mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.PreparedStatementInstrumentation mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.PreparedStatementInstrumentation
mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.StatementInstrumentation mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.StatementInstrumentation
\ No newline at end of file mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.PreparedStatementSetterInstrumentation
mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.PreparedStatementNullSetterInstrumentation
mssql-jdbc=org.apache.skywalking.apm.plugin.mssql.jdbc.define.PreparedStatementIgnoredSetterInstrumentation
\ No newline at end of file
...@@ -18,4 +18,4 @@ ...@@ -18,4 +18,4 @@
home="$(cd "$(dirname $0)"; pwd)" home="$(cd "$(dirname $0)"; pwd)"
java -jar ${agent_opts} ${home}/../libs/mssql-jdbc-scenario.jar & java -jar ${agent_opts} -Dskywalking.plugin.jdbc.trace_sql_parameters=true ${home}/../libs/mssql-jdbc-scenario.jar &
\ No newline at end of file \ No newline at end of file
...@@ -46,6 +46,7 @@ segmentItems: ...@@ -46,6 +46,7 @@ segmentItems:
- {key: db.type, value: sql} - {key: db.type, value: sql}
- {key: db.instance, value: tempdb} - {key: db.instance, value: tempdb}
- {key: db.statement, value: 'INSERT INTO test_007(id, value) VALUES(?,?)'} - {key: db.statement, value: 'INSERT INTO test_007(id, value) VALUES(?,?)'}
- {key: db.sql.parameters, value: '[1,1]'}
logs: [] logs: []
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -55,10 +56,28 @@ segmentItems: ...@@ -55,10 +56,28 @@ segmentItems:
componentId: 104 componentId: 104
peer: mssql-server:1433 peer: mssql-server:1433
skipAnalysis: 'false' skipAnalysis: 'false'
- operationName: Mssql/JDBI/Statement/execute - operationName: Mssql/JDBI/PreparedStatement/execute
operationId: eq 0 operationId: eq 0
parentSpanId: 0 parentSpanId: 0
spanId: 3 spanId: 3
tags:
- {key: db.type, value: sql}
- {key: db.instance, value: tempdb}
- {key: db.statement, value: 'SELECT id, value FROM test_007 WHERE id=?'}
- {key: db.sql.parameters, value: '[1]'}
logs: []
startTime: nq 0
endTime: nq 0
isError: false
spanLayer: Database
spanType: Exit
componentId: 104
peer: mssql-server:1433
skipAnalysis: 'false'
- operationName: Mssql/JDBI/Statement/execute
operationId: eq 0
parentSpanId: 0
spanId: 4
tags: tags:
- {key: db.type, value: sql} - {key: db.type, value: sql}
- {key: db.instance, value: tempdb} - {key: db.instance, value: tempdb}
...@@ -75,7 +94,7 @@ segmentItems: ...@@ -75,7 +94,7 @@ segmentItems:
- operationName: Mssql/JDBI/Connection/close - operationName: Mssql/JDBI/Connection/close
operationId: eq 0 operationId: eq 0
parentSpanId: 0 parentSpanId: 0
spanId: 4 spanId: 5
tags: tags:
- {key: db.type, value: sql} - {key: db.type, value: sql}
- {key: db.instance, value: tempdb} - {key: db.instance, value: tempdb}
......
...@@ -50,7 +50,14 @@ public class SQLExecutor implements AutoCloseable { ...@@ -50,7 +50,14 @@ public class SQLExecutor implements AutoCloseable {
preparedStatement.execute(); preparedStatement.execute();
preparedStatement.close(); preparedStatement.close();
} }
public void queryData(String sql, String id) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
preparedStatement.execute();
preparedStatement.close();
}
public void dropTable(String sql) throws SQLException { public void dropTable(String sql) throws SQLException {
executeStatement(sql); executeStatement(sql);
} }
......
...@@ -45,6 +45,7 @@ public class CaseController { ...@@ -45,6 +45,7 @@ public class CaseController {
try (SQLExecutor sqlExecute = new SQLExecutor()) { try (SQLExecutor sqlExecute = new SQLExecutor()) {
sqlExecute.createTable(CREATE_TABLE_SQL); sqlExecute.createTable(CREATE_TABLE_SQL);
sqlExecute.insertData(INSERT_DATA_SQL, "1", "1"); sqlExecute.insertData(INSERT_DATA_SQL, "1", "1");
sqlExecute.queryData(QUERY_DATA_SQL, "1");
sqlExecute.dropTable(DROP_TABLE_SQL); sqlExecute.dropTable(DROP_TABLE_SQL);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Failed to execute sql.", e); LOGGER.error("Failed to execute sql.", e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册