Loading apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/PreparedStatementExecuteMethodsInterceptor.java +6 −6 Original line number Diff line number Diff line Loading @@ -40,15 +40,15 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField(); ConnectionInfo connectInfo = cacheObject.getConnectionInfo(); /** * For avoid NPE. In this particular case, Execute sql inside the {@link com.mysql.jdbc.ConnectionImpl} constructor, * before the interceptor sets the connectionInfo. * * When invoking prepareCall, cacheObject is null. Because it will determine procedures's parameter types by executing sql in mysql * before the interceptor sets the statementEnhanceInfos. * @see JDBCDriverInterceptor#afterMethod(EnhancedInstance, Method, Object[], Class[], Object) */ if (connectInfo != null) { if (cacheObject != null && cacheObject.getConnectionInfo() != null) { ConnectionInfo connectInfo = cacheObject.getConnectionInfo(); AbstractSpan span = ContextManager.createExitSpan(buildOperationName(connectInfo, method.getName(), cacheObject .getStatementName()), connectInfo.getDatabasePeer()); Tags.DB_TYPE.set(span, "sql"); Loading @@ -73,7 +73,7 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho public final Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField(); if (cacheObject.getConnectionInfo() != null) { if (cacheObject != null && cacheObject.getConnectionInfo() != null) { ContextManager.stopSpan(); } return ret; Loading @@ -83,7 +83,7 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField(); if (cacheObject.getConnectionInfo() != null) { if (cacheObject != null && cacheObject.getConnectionInfo() != null) { ContextManager.activeSpan().errorOccurred().log(t); } } Loading test/plugin/scenarios/mysql-scenario/config/expectedData.yaml +68 −1 Original line number Diff line number Diff line Loading @@ -72,10 +72,77 @@ segmentItems: componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Connection/close - operationName: Mysql/JDBI/Statement/execute operationId: eq 0 parentSpanId: 0 spanId: 4 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: "create procedure testProcedure(IN id varchar(10)) \n begin \n select id; \n end"} logs: [] startTime: nq 0 endTime: nq 0 isError: false spanLayer: Database spanType: Exit componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Statement/executeQuery operationId: eq 0 parentSpanId: 0 spanId: 5 spanLayer: Database startTime: nq 0 endTime: nq 0 componentId: 33 isError: false spanType: Exit peer: mysql-server:3306 skipAnalysis: false tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: SHOW CREATE PROCEDURE `test`.`testProcedure`} - operationName: Mysql/JDBI/CallableStatement/execute operationId: eq 0 parentSpanId: 0 spanId: 6 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: "call testProcedure( ? )"} logs: [] startTime: nq 0 endTime: nq 0 isError: false spanLayer: Database spanType: Exit componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Statement/execute operationId: eq 0 parentSpanId: 0 spanId: 7 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: "drop procedure testProcedure"} logs: [] startTime: nq 0 endTime: nq 0 isError: false spanLayer: Database spanType: Exit componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Connection/close operationId: eq 0 parentSpanId: 0 spanId: 8 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} Loading test/plugin/scenarios/mysql-scenario/src/main/java/org/apache/skywalking/apm/testcase/mysql/SQLExecutor.java +20 −1 Original line number Diff line number Diff line Loading @@ -51,6 +51,25 @@ public class SQLExecutor implements AutoCloseable { } public void dropTable(String sql) throws SQLException { executeStatement(sql); } public void createProcedure(String sql) throws SQLException { executeStatement(sql); } public void dropProcedure(String sql) throws SQLException { executeStatement(sql); } public void callProcedure(String sql, String id) throws SQLException { PreparedStatement preparedStatement = connection.prepareCall(sql); preparedStatement.setString(1, id); preparedStatement.execute(); preparedStatement.close(); } public void executeStatement(String sql) throws SQLException { Statement preparedStatement = connection.createStatement(); preparedStatement.execute(sql); preparedStatement.close(); Loading test/plugin/scenarios/mysql-scenario/src/main/java/org/apache/skywalking/apm/testcase/mysql/controller/CaseController.java +7 −1 Original line number Diff line number Diff line Loading @@ -40,6 +40,9 @@ public class CaseController { private static final String QUERY_DATA_SQL = "SELECT id, value FROM test_007 WHERE id=?"; private static final String DELETE_DATA_SQL = "DELETE FROM test_007 WHERE id=?"; private static final String DROP_TABLE_SQL = "DROP table test_007"; private static final String CREATE_PROCEDURE_SQL = "create procedure testProcedure(IN id varchar(10)) \n begin \n select id; \n end"; private static final String CALL_PROCEDURE_SQL = "call testProcedure( ? )"; private static final String DROP_PROCEDURE_SQL = "drop procedure testProcedure"; @RequestMapping("/mysql-scenario") @ResponseBody Loading @@ -48,6 +51,9 @@ public class CaseController { sqlExecute.createTable(CREATE_TABLE_SQL); sqlExecute.insertData(INSERT_DATA_SQL, "1", "1"); sqlExecute.dropTable(DROP_TABLE_SQL); sqlExecute.createProcedure(CREATE_PROCEDURE_SQL); sqlExecute.callProcedure(CALL_PROCEDURE_SQL, "nihao"); sqlExecute.dropProcedure(DROP_PROCEDURE_SQL); } catch (Exception e) { logger.error("Failed to execute sql.", e); throw e; Loading Loading
apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/PreparedStatementExecuteMethodsInterceptor.java +6 −6 Original line number Diff line number Diff line Loading @@ -40,15 +40,15 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField(); ConnectionInfo connectInfo = cacheObject.getConnectionInfo(); /** * For avoid NPE. In this particular case, Execute sql inside the {@link com.mysql.jdbc.ConnectionImpl} constructor, * before the interceptor sets the connectionInfo. * * When invoking prepareCall, cacheObject is null. Because it will determine procedures's parameter types by executing sql in mysql * before the interceptor sets the statementEnhanceInfos. * @see JDBCDriverInterceptor#afterMethod(EnhancedInstance, Method, Object[], Class[], Object) */ if (connectInfo != null) { if (cacheObject != null && cacheObject.getConnectionInfo() != null) { ConnectionInfo connectInfo = cacheObject.getConnectionInfo(); AbstractSpan span = ContextManager.createExitSpan(buildOperationName(connectInfo, method.getName(), cacheObject .getStatementName()), connectInfo.getDatabasePeer()); Tags.DB_TYPE.set(span, "sql"); Loading @@ -73,7 +73,7 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho public final Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField(); if (cacheObject.getConnectionInfo() != null) { if (cacheObject != null && cacheObject.getConnectionInfo() != null) { ContextManager.stopSpan(); } return ret; Loading @@ -83,7 +83,7 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField(); if (cacheObject.getConnectionInfo() != null) { if (cacheObject != null && cacheObject.getConnectionInfo() != null) { ContextManager.activeSpan().errorOccurred().log(t); } } Loading
test/plugin/scenarios/mysql-scenario/config/expectedData.yaml +68 −1 Original line number Diff line number Diff line Loading @@ -72,10 +72,77 @@ segmentItems: componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Connection/close - operationName: Mysql/JDBI/Statement/execute operationId: eq 0 parentSpanId: 0 spanId: 4 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: "create procedure testProcedure(IN id varchar(10)) \n begin \n select id; \n end"} logs: [] startTime: nq 0 endTime: nq 0 isError: false spanLayer: Database spanType: Exit componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Statement/executeQuery operationId: eq 0 parentSpanId: 0 spanId: 5 spanLayer: Database startTime: nq 0 endTime: nq 0 componentId: 33 isError: false spanType: Exit peer: mysql-server:3306 skipAnalysis: false tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: SHOW CREATE PROCEDURE `test`.`testProcedure`} - operationName: Mysql/JDBI/CallableStatement/execute operationId: eq 0 parentSpanId: 0 spanId: 6 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: "call testProcedure( ? )"} logs: [] startTime: nq 0 endTime: nq 0 isError: false spanLayer: Database spanType: Exit componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Statement/execute operationId: eq 0 parentSpanId: 0 spanId: 7 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} - {key: db.statement, value: "drop procedure testProcedure"} logs: [] startTime: nq 0 endTime: nq 0 isError: false spanLayer: Database spanType: Exit componentId: 33 peer: mysql-server:3306 skipAnalysis: 'false' - operationName: Mysql/JDBI/Connection/close operationId: eq 0 parentSpanId: 0 spanId: 8 tags: - {key: db.type, value: sql} - {key: db.instance, value: test} Loading
test/plugin/scenarios/mysql-scenario/src/main/java/org/apache/skywalking/apm/testcase/mysql/SQLExecutor.java +20 −1 Original line number Diff line number Diff line Loading @@ -51,6 +51,25 @@ public class SQLExecutor implements AutoCloseable { } public void dropTable(String sql) throws SQLException { executeStatement(sql); } public void createProcedure(String sql) throws SQLException { executeStatement(sql); } public void dropProcedure(String sql) throws SQLException { executeStatement(sql); } public void callProcedure(String sql, String id) throws SQLException { PreparedStatement preparedStatement = connection.prepareCall(sql); preparedStatement.setString(1, id); preparedStatement.execute(); preparedStatement.close(); } public void executeStatement(String sql) throws SQLException { Statement preparedStatement = connection.createStatement(); preparedStatement.execute(sql); preparedStatement.close(); Loading
test/plugin/scenarios/mysql-scenario/src/main/java/org/apache/skywalking/apm/testcase/mysql/controller/CaseController.java +7 −1 Original line number Diff line number Diff line Loading @@ -40,6 +40,9 @@ public class CaseController { private static final String QUERY_DATA_SQL = "SELECT id, value FROM test_007 WHERE id=?"; private static final String DELETE_DATA_SQL = "DELETE FROM test_007 WHERE id=?"; private static final String DROP_TABLE_SQL = "DROP table test_007"; private static final String CREATE_PROCEDURE_SQL = "create procedure testProcedure(IN id varchar(10)) \n begin \n select id; \n end"; private static final String CALL_PROCEDURE_SQL = "call testProcedure( ? )"; private static final String DROP_PROCEDURE_SQL = "drop procedure testProcedure"; @RequestMapping("/mysql-scenario") @ResponseBody Loading @@ -48,6 +51,9 @@ public class CaseController { sqlExecute.createTable(CREATE_TABLE_SQL); sqlExecute.insertData(INSERT_DATA_SQL, "1", "1"); sqlExecute.dropTable(DROP_TABLE_SQL); sqlExecute.createProcedure(CREATE_PROCEDURE_SQL); sqlExecute.callProcedure(CALL_PROCEDURE_SQL, "nihao"); sqlExecute.dropProcedure(DROP_PROCEDURE_SQL); } catch (Exception e) { logger.error("Failed to execute sql.", e); throw e; Loading