TracingCommandListener implements CommandListener { 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)); } 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())); } TracingCommandListener();  TracingCommandListener(Tracer tracer); void commandStarted(CommandStartedEvent event); void commandSucceeded(CommandSucceededEvent event); void commandFailed(CommandFailedEvent event);  }
@Test public void testCommandStarted() { commandListener.commandStarted(findStartEvent); assertThat(tracer.startSpanEvents).hasSize(1); StartSpanEvent startSpanEvent = tracer.startSpanEvents.get(0); assertThat(startSpanEvent.getName()).isEqualTo("find"); assertThat(startSpanEvent.getOptions()).isEqualTo(null); assertThat(tracer.annotateEvents).hasSize(1); AnnotateEvent annotateEvent = tracer.annotateEvents.get(0); assertThat(annotateEvent.getLabels().getLabels()).containsAllOf( new Label("/mongodb/commandName", "find"), new Label("/mongodb/databaseName", "test-database"), new Label("/mongodb/collectionName", "test-collection"), new Label("/mongodb/requestId", "1"), new Label("/mongodb/batchSize", "20") ); assertThat(annotateEvent.getTraceContext()).isEqualTo(startSpanEvent.getTraceContext()); }