TracingCommandListener implements CommandListener { public void commandFailed(CommandFailedEvent event) { MongoDBCommandTraceContext commandContext = contexts.get(); if (commandContext == null || commandContext.getRequestId() != event.getRequestId()) { contexts.remove(); return; } Labels.Builder labels = Labels.builder(); labels.add(MongoLabels.STATUS, "FAILURE"); labels.add(MongoLabels.ERROR, event.getThrowable().getMessage()); tracer.annotateSpan(commandContext.getContext(), labels.build()); tracer.endSpan(commandContext.getContext()); contexts.remove(); } TracingCommandListener();  TracingCommandListener(Tracer tracer); void commandStarted(CommandStartedEvent event); void commandSucceeded(CommandSucceededEvent event); void commandFailed(CommandFailedEvent event);  }
@Test public void testCommandFailed() { commandListener.commandStarted(findStartEvent); StartSpanEvent startEvent = tracer.startSpanEvents.get(0); tracer.reset(); commandListener.commandFailed(findFailedEvent); assertThat(tracer.endSpanEvents).hasSize(1); EndSpanEvent endSpanEvent = tracer.endSpanEvents.get(0); assertThat(endSpanEvent.getTraceContext()).isEqualTo(startEvent.getTraceContext()); assertThat(endSpanEvent.getEndSpanOptions()).isNull(); assertThat(tracer.annotateEvents).hasSize(1); AnnotateEvent annotateEvent = tracer.annotateEvents.get(0); assertThat(annotateEvent.getTraceContext()).isEqualTo(endSpanEvent.getTraceContext()); assertThat(annotateEvent.getLabels().getLabels()).containsAllOf( new Label("/mongodb/status", "FAILURE"), new Label("/mongodb/error", "Error!") ); }