下面列出了怎么用com.mongodb.event.CommandStartedEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void commandStarted(final CommandStartedEvent event) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Sent command '{}:{}' with id {} to database '{}' "
+ "on connection '{}' to server '{}'",
event.getCommandName(),
event.getCommand().get(event.getCommandName()),
event.getRequestId(),
event.getDatabaseName(),
event.getConnectionDescription().getConnectionId(),
event.getConnectionDescription().getServerAddress());
}
}
@Override
public void customize(MongoClientSettings.Builder clientSettingsBuilder) {
super.customize(clientSettingsBuilder);
CommandListener listener = clientSettingsBuilder.build().getCommandListeners()
.get(0);
listener.commandStarted(new CommandStartedEvent(0, null, "", "",
BDDMockito.mock(BsonDocument.class)));
listener.commandSucceeded(new CommandSucceededEvent(1, null, "",
BDDMockito.mock(BsonDocument.class), 100));
}
/**
* 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) {
Span span = threadLocalSpan.next();
if (span == null || span.isNoop()) return;
String commandName = event.getCommandName();
String databaseName = event.getDatabaseName();
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();
}
CommandStartedEvent createCommandStartedEvent() {
return new CommandStartedEvent(
1,
createConnectionDescription(),
"dbName",
"insert",
LONG_COMMAND
);
}