Browse Source

Polishing.

Refactor duplicate code into callback.

See #4481
4.1.x
Mark Paluch 2 years ago
parent
commit
0b3d1b823f
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 56
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservationCommandListener.java

56
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservationCommandListener.java

@ -19,6 +19,8 @@ import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
import java.util.function.BiConsumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -126,30 +128,43 @@ public class MongoObservationCommandListener implements CommandListener {
@Override @Override
public void commandSucceeded(CommandSucceededEvent event) { public void commandSucceeded(CommandSucceededEvent event) {
RequestContext requestContext = event.getRequestContext(); doInObservation(event.getRequestContext(), (observation, context) -> {
if (requestContext == null) { context.setCommandSucceededEvent(event);
return;
}
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null); if (log.isDebugEnabled()) {
if (observation == null || !(observation.getContext()instanceof MongoHandlerContext context)) { log.debug("Command succeeded - will stop observation [" + observation + "]");
return; }
}
context.setCommandSucceededEvent(event);
if (log.isDebugEnabled()) {
log.debug("Command succeeded - will stop observation [" + observation + "]");
}
observation.stop(); observation.stop();
});
} }
@Override @Override
public void commandFailed(CommandFailedEvent event) { public void commandFailed(CommandFailedEvent event) {
RequestContext requestContext = event.getRequestContext(); doInObservation(event.getRequestContext(), (observation, context) -> {
context.setCommandFailedEvent(event);
if (log.isDebugEnabled()) {
log.debug("Command failed - will stop observation [" + observation + "]");
}
observation.error(event.getThrowable());
observation.stop();
});
}
/**
* Performs the given action for the {@link Observation} and {@link MongoHandlerContext} if there is an ongoing Mongo
* Observation. Exceptions thrown by the action are relayed to the caller.
*
* @param requestContext the context to extract the Observation from.
* @param action the action to invoke.
*/
private void doInObservation(@Nullable RequestContext requestContext,
BiConsumer<Observation, MongoHandlerContext> action) {
if (requestContext == null) { if (requestContext == null) {
return; return;
@ -160,14 +175,7 @@ public class MongoObservationCommandListener implements CommandListener {
return; return;
} }
context.setCommandFailedEvent(event); action.accept(observation, context);
if (log.isDebugEnabled()) {
log.debug("Command failed - will stop observation [" + observation + "]");
}
observation.error(event.getThrowable());
observation.stop();
} }
/** /**

Loading…
Cancel
Save