1919package org .apache .skywalking .apm .plugin .jdbc .trace ;
2020
2121import java .sql .SQLException ;
22+ import java .util .Objects ;
23+ import org .apache .skywalking .apm .agent .core .context .ContextManager ;
2224import org .apache .skywalking .apm .agent .core .context .tag .Tags ;
2325import org .apache .skywalking .apm .agent .core .context .trace .AbstractSpan ;
2426import org .apache .skywalking .apm .agent .core .context .trace .SpanLayer ;
25- import org .apache .skywalking .apm .agent .core .context .ContextManager ;
27+ import org .apache .skywalking .apm .plugin .jdbc .JDBCPluginConfig ;
28+ import org .apache .skywalking .apm .plugin .jdbc .PreparedStatementParameterBuilder ;
29+ import org .apache .skywalking .apm .plugin .jdbc .define .StatementEnhanceInfos ;
2630
2731/**
2832 * {@link PreparedStatementTracing} create an exit span when the client call the method in the class that extend {@link
3135public class PreparedStatementTracing {
3236
3337 public static <R > R execute (java .sql .PreparedStatement realStatement , ConnectionInfo connectInfo , String method ,
34- String sql , Executable <R > exec ) throws SQLException {
35- final AbstractSpan span = ContextManager .createExitSpan (connectInfo .getDBType () + "/JDBC/PreparedStatement/" + method , connectInfo
36- .getDatabasePeer ());
38+ String sql , Executable <R > exec , StatementEnhanceInfos statementEnhanceInfos ) throws SQLException {
39+ final AbstractSpan span = ContextManager .createExitSpan (
40+ connectInfo .getDBType () + "/JDBC/PreparedStatement/" + method , connectInfo
41+ .getDatabasePeer ());
3742 try {
3843 Tags .DB_TYPE .set (span , connectInfo .getDBType ());
3944 Tags .DB_INSTANCE .set (span , connectInfo .getDatabaseName ());
4045 Tags .DB_STATEMENT .set (span , sql );
4146 span .setComponent (connectInfo .getComponent ());
42-
4347 SpanLayer .asDB (span );
48+ if (JDBCPluginConfig .Plugin .JDBC .TRACE_SQL_PARAMETERS && Objects .nonNull (statementEnhanceInfos )) {
49+ final Object [] parameters = statementEnhanceInfos .getParameters ();
50+ if (parameters != null && parameters .length > 0 ) {
51+ int maxIndex = statementEnhanceInfos .getMaxIndex ();
52+ Tags .SQL_PARAMETERS .set (span , getParameterString (parameters , maxIndex ));
53+ }
54+ }
4455 return exec .exe (realStatement , sql );
4556 } catch (SQLException e ) {
4657 span .log (e );
@@ -50,7 +61,15 @@ public static <R> R execute(java.sql.PreparedStatement realStatement, Connection
5061 }
5162 }
5263
64+ private static String getParameterString (Object [] parameters , int maxIndex ) {
65+ return new PreparedStatementParameterBuilder ()
66+ .setParameters (parameters )
67+ .setMaxIndex (maxIndex )
68+ .build ();
69+ }
70+
5371 public interface Executable <R > {
72+
5473 R exe (java .sql .PreparedStatement realConnection , String sql ) throws SQLException ;
5574 }
5675}
0 commit comments