Search in sources :

Example 1 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 <T extends Resultset> T preProcess(String sql, Statement interceptedStatement) {
    // 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.cj.jdbc.PreparedStatement) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Example 2 with ThreadLocalSpan

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

the class TracingQueryInterceptor 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 <T extends Resultset> T preProcess(Supplier<String> sqlSupplier, Query interceptedQuery) {
    // 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;
    String sql = sqlSupplier.get();
    // 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 : Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Example 3 with ThreadLocalSpan

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

the class TraceMongoCommandListener method commandStarted.

/**
 * Uses {@link ThreadLocalSpan} as there's no attribute namespace shared between callbacks, but
 * all callbacks happen on the same thread.
 */
@Override
public void commandStarted(CommandStartedEvent event) {
    String databaseName = event.getDatabaseName();
    // don't trace commands like "endSessions"
    if ("admin".equals(databaseName))
        return;
    Span span = threadLocalSpan.next();
    if (span == null || span.isNoop())
        return;
    String commandName = event.getCommandName();
    BsonDocument command = event.getCommand();
    String collectionName = getCollectionName(command, commandName);
    span.name(getSpanName(commandName, collectionName)).kind(CLIENT).remoteServiceName("mongodb-" + databaseName).tag("mongodb.command", commandName);
    if (collectionName != null) {
        span.tag("mongodb.collection", collectionName);
    }
    ConnectionDescription connectionDescription = event.getConnectionDescription();
    if (connectionDescription != null) {
        ConnectionId connectionId = connectionDescription.getConnectionId();
        if (connectionId != null) {
            span.tag("mongodb.cluster_id", connectionId.getServerId().getClusterId().getValue());
        }
        try {
            InetSocketAddress socketAddress = connectionDescription.getServerAddress().getSocketAddress();
            span.remoteIpAndPort(socketAddress.getAddress().getHostAddress(), socketAddress.getPort());
        } catch (MongoSocketException ignored) {
        }
    }
    span.start();
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) BsonDocument(org.bson.BsonDocument) InetSocketAddress(java.net.InetSocketAddress) MongoSocketException(com.mongodb.MongoSocketException) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Example 4 with ThreadLocalSpan

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

the class TracingExceptionInterceptor method interceptException.

/**
 * Uses {@link ThreadLocalSpan} as there's no attribute namespace shared between callbacks, but
 * all callbacks happen on the same thread. The span will already have been created in {@link
 * TracingQueryInterceptor}.
 *
 * <p>Uses {@link ThreadLocalSpan#CURRENT_TRACER} and this interceptor initializes before
 * tracing.
 */
@Override
public Exception interceptException(Exception e) {
    Span span = ThreadLocalSpan.CURRENT_TRACER.remove();
    if (span == null || span.isNoop())
        return null;
    span.error(e);
    if (e instanceof SQLException) {
        span.tag("error", Integer.toString(((SQLException) e).getErrorCode()));
    }
    span.finish();
    return null;
}
Also used : SQLException(java.sql.SQLException) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Example 5 with ThreadLocalSpan

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

the class TracingJdbcEventListener method onBeforeAnyExecute.

/**
 * 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 void onBeforeAnyExecute(StatementInformation info) {
    String sql = includeParameterValues ? info.getSqlWithValues() : info.getSql();
    if (!isLoggable(sql))
        return;
    // 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;
    // 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(info.getConnectionInformation().getConnection(), span);
    span.start();
}
Also used : 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