Search in sources :

Example 6 with ThreadLocalSpan

use of brave.propagation.ThreadLocalSpan in project brave by openzipkin.

the class TracingStatementInterceptor method preProcess.

/**
 * Uses {@link ThreadLocalSpan} as there's no attribute namespace shared between callbacks, but
 * all callbacks happen on the same thread.
 *
 * <p>Uses {@link ThreadLocalSpan#CURRENT_TRACER} and this interceptor initializes before
 * tracing.
 */
@Override
public ResultSetInternalMethods preProcess(String sql, Statement interceptedStatement, Connection connection) {
    // Gets the next span (and places it in scope) so code between here and postProcess can read it
    Span span = ThreadLocalSpan.CURRENT_TRACER.next();
    if (span == null || span.isNoop())
        return null;
    // When running a prepared statement, sql will be null and we must fetch the sql from the statement itself
    if (interceptedStatement instanceof PreparedStatement) {
        sql = ((PreparedStatement) interceptedStatement).getPreparedSql();
    }
    // Allow span names of single-word statements like COMMIT
    int spaceIndex = sql.indexOf(' ');
    span.kind(CLIENT).name(spaceIndex == -1 ? sql : sql.substring(0, spaceIndex));
    span.tag("sql.query", sql);
    parseServerIpAndPort(connection, span);
    span.start();
    return null;
}
Also used : PreparedStatement(com.mysql.jdbc.PreparedStatement) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Aggregations

Span (brave.Span)6 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)6 MongoSocketException (com.mongodb.MongoSocketException)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1 ConnectionId (com.mongodb.connection.ConnectionId)1 PreparedStatement (com.mysql.cj.jdbc.PreparedStatement)1 PreparedStatement (com.mysql.jdbc.PreparedStatement)1 InetSocketAddress (java.net.InetSocketAddress)1 SQLException (java.sql.SQLException)1 BsonDocument (org.bson.BsonDocument)1