Example usage for com.mongodb.event CommandStartedEvent getCommandName

List of usage examples for com.mongodb.event CommandStartedEvent getCommandName

Introduction

In this page you can find the example usage for com.mongodb.event CommandStartedEvent getCommandName.

Prototype

public String getCommandName() 

Source Link

Document

Gets the name of the command.

Usage

From source file:com.example.App.java

License:Apache License

public static void main(String[] args) throws InterruptedException {
    final MongoClientSettings settings = MongoClientSettings.builder()
            .addCommandListener(new CommandListener() {
                @Override/*from w  w  w. j av a 2  s. c  o m*/
                public void commandStarted(CommandStartedEvent event) {
                    logger.info("command: db = {}, command = {}", event.getDatabaseName(),
                            event.getCommandName());
                }

                @Override
                public void commandSucceeded(CommandSucceededEvent event) {
                    logger.info("command succeed: request = {}, command = {}", event.getRequestId(),
                            event.getCommandName());
                }

                @Override
                public void commandFailed(CommandFailedEvent event) {
                    logger.info("command failed: request = {}, command = {}", event.getRequestId(),
                            event.getCommandName());
                    logger.error("detail", event.getThrowable());
                }
            }).applicationName("sample-app")
            .applyToConnectionPoolSettings(builder -> builder.maxSize(1).minSize(1)).build();

    final MongoClient client = MongoClients.create(settings);
    final MongoDatabase database = client.getDatabase("sample");
    final MongoCollection<Document> collection = database.getCollection("test");

    final Document firstDocument = new Document("id", UUID.randomUUID()).append("name", "test user")
            .append("created", LocalDateTime.now());

    logger.info("document to be saved: {}", firstDocument);

    final Mono<Document> firstMono = Mono.create(sink -> collection.insertOne(firstDocument, (result, t) -> {
        if (t == null) {
            logger.info("inserted: {}", firstDocument);
            sink.success(firstDocument);
        } else {
            logger.error("error", t);
            sink.error(t);
        }
    }));

    final Mono<List<Document>> secondMono = create100Users(collection, firstMono);

    final Mono<Long> thirdMono = secondMono.then(Mono.create(sink -> collection.countDocuments((count, t) -> {
        if (t == null) {
            logger.info("collection has {} items.", count);
            sink.success(count);
        } else {
            logger.error("error", t);
            sink.error(t);
        }
    })));

    final Mono<List<Document>> fourthMono = create100Users(collection, thirdMono);
    final Mono<List<Document>> fifthMono = create100Users(collection, fourthMono);
    final Mono<List<Document>> sixthMono = create100Users(collection, fifthMono);

    final Mono<Document> seventhMono = sixthMono.then(Mono.create(sink -> collection.find().first((doc, t) -> {
        if (t == null) {
            logger.info("found document: {}", doc);
            sink.success(doc);
        } else {
            logger.error("error", t);
            sink.error(t);
        }
    })));

    final CountDownLatch latch = new CountDownLatch(1);
    seventhMono.doOnTerminate(() -> {
        latch.countDown();
        client.close();
    }).subscribe(doc -> logger.info("first document: {}", doc));
    latch.await();
}

From source file:com.google.cloud.trace.mongodb.TracingCommandListener.java

License:Open Source License

public void commandStarted(CommandStartedEvent event) {
    BsonDocument document = event.getCommand();
    Labels.Builder labels = Labels.builder();
    String commandName = event.getCommandName();
    labels.add(MongoLabels.COMMAND_NAME, commandName);
    String databaseName = event.getDatabaseName();
    labels.add(MongoLabels.DATABASE_NAME, databaseName);
    labels.add(MongoLabels.REQUEST_ID, Integer.toString(event.getRequestId()));
    if (document.containsKey("batchSize")) {
        int batchSize = document.getInt32("batchSize").getValue();
        labels.add(MongoLabels.BATCH_SIZE, Integer.toString(batchSize));
    }//from   ww  w. java 2s . co  m
    String collectionKey = collectionKeyByCommand.get(commandName);
    if (collectionKey != null && document.containsKey(collectionKey)) {
        String collectionName = document.getString(collectionKey).getValue();
        labels.add(MongoLabels.COLLECTION_NAME, collectionName);
    }

    TraceContext context = tracer.startSpan(commandName);
    tracer.annotateSpan(context, labels.build());
    contexts.set(new MongoDBCommandTraceContext(context, event.getRequestId()));
}

From source file:io.opentracing.contrib.mongo.TracingCommandListener.java

License:Apache License

Span buildSpan(CommandStartedEvent event) {
    Tracer.SpanBuilder spanBuilder = tracer
            .buildSpan(mongoSpanNameProvider.generateName(event.getCommandName()))
            .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

    Span span = spanBuilder.start();//www . ja  va2 s.  c  o m
    decorate(span, event);

    return span;
}

From source file:org.eclipse.ditto.services.utils.persistence.mongo.monitoring.KamonCommandListener.java

License:Open Source License

@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());
    }/*from www  .j  a va 2  s .  c o m*/
}