@@ -19,6 +19,8 @@ import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -126,16 +128,7 @@ public class MongoObservationCommandListener implements CommandListener {
|
||||
@Override
|
||||
public void commandSucceeded(CommandSucceededEvent event) {
|
||||
|
||||
RequestContext requestContext = event.getRequestContext();
|
||||
|
||||
if (requestContext == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
|
||||
if (observation == null || !(observation.getContext()instanceof MongoHandlerContext context)) {
|
||||
return;
|
||||
}
|
||||
doInObservation(event.getRequestContext(), (observation, context) -> {
|
||||
|
||||
context.setCommandSucceededEvent(event);
|
||||
|
||||
@@ -144,21 +137,13 @@ public class MongoObservationCommandListener implements CommandListener {
|
||||
}
|
||||
|
||||
observation.stop();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandFailed(CommandFailedEvent event) {
|
||||
|
||||
RequestContext requestContext = event.getRequestContext();
|
||||
|
||||
if (requestContext == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
|
||||
if (observation == null || !(observation.getContext()instanceof MongoHandlerContext context)) {
|
||||
return;
|
||||
}
|
||||
doInObservation(event.getRequestContext(), (observation, context) -> {
|
||||
|
||||
context.setCommandFailedEvent(event);
|
||||
|
||||
@@ -168,6 +153,29 @@ public class MongoObservationCommandListener implements CommandListener {
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
Observation observation = requestContext.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
|
||||
if (observation == null || !(observation.getContext()instanceof MongoHandlerContext context)) {
|
||||
return;
|
||||
}
|
||||
|
||||
action.accept(observation, context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user