Search in sources :

Example 6 with ConnectionId

use of com.mongodb.connection.ConnectionId 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)

Aggregations

ConnectionId (com.mongodb.connection.ConnectionId)6 ConnectionDescription (com.mongodb.connection.ConnectionDescription)5 ServerAddress (com.mongodb.ServerAddress)2 ClusterId (com.mongodb.connection.ClusterId)2 ServerId (com.mongodb.connection.ServerId)2 DescriptionHelper.createConnectionDescription (com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription)2 BsonDocument (org.bson.BsonDocument)2 Span (brave.Span)1 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)1 MongoClientException (com.mongodb.MongoClientException)1 MongoSocketException (com.mongodb.MongoSocketException)1 ClusterDescription (com.mongodb.connection.ClusterDescription)1 ServerDescription (com.mongodb.connection.ServerDescription)1 ClusterClosedEvent (com.mongodb.event.ClusterClosedEvent)1 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)1 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)1 ServerClosedEvent (com.mongodb.event.ServerClosedEvent)1 ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)1 ServerHeartbeatFailedEvent (com.mongodb.event.ServerHeartbeatFailedEvent)1 ServerHeartbeatStartedEvent (com.mongodb.event.ServerHeartbeatStartedEvent)1