@ -113,10 +113,6 @@ public class MongoObservationCommandListener implements CommandListener {
Observation parent = observationFromContext ( requestContext ) ;
Observation parent = observationFromContext ( requestContext ) ;
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Found the following observation passed from the mongo context [" + parent + "]" ) ;
}
MongoHandlerContext observationContext = new MongoHandlerContext ( connectionString , event , requestContext ) ;
MongoHandlerContext observationContext = new MongoHandlerContext ( connectionString , event , requestContext ) ;
observationContext . setRemoteServiceName ( "mongo" ) ;
observationContext . setRemoteServiceName ( "mongo" ) ;
@ -141,22 +137,20 @@ public class MongoObservationCommandListener implements CommandListener {
@Override
@Override
public void commandSucceeded ( CommandSucceededEvent event ) {
public void commandSucceeded ( CommandSucceededEvent event ) {
doIn Observation( event . getRequestContext ( ) , ( observation , context ) - > {
stop Observation( event . getRequestContext ( ) , ( observation , context ) - > {
context . setCommandSucceededEvent ( event ) ;
context . setCommandSucceededEvent ( event ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Command succeeded - will stop observation [" + observation + "]" ) ;
log . debug ( "Command succeeded - will stop observation [" + observation + "]" ) ;
}
}
observation . stop ( ) ;
} ) ;
} ) ;
}
}
@Override
@Override
public void commandFailed ( CommandFailedEvent event ) {
public void commandFailed ( CommandFailedEvent event ) {
doIn Observation( event . getRequestContext ( ) , ( observation , context ) - > {
stop Observation( event . getRequestContext ( ) , ( observation , context ) - > {
context . setCommandFailedEvent ( event ) ;
context . setCommandFailedEvent ( event ) ;
@ -165,18 +159,17 @@ public class MongoObservationCommandListener implements CommandListener {
}
}
observation . error ( event . getThrowable ( ) ) ;
observation . error ( event . getThrowable ( ) ) ;
observation . stop ( ) ;
} ) ;
} ) ;
}
}
/ * *
/ * *
* Performs the given action for the { @link Observation } and { @link MongoHandlerContext } if there is an ongoing Mongo
* Stops the { @link Observation } after applying { @code action } given { @link MongoHandlerContext } if there is an
* Observation . Exceptions thrown by the action are relayed to the caller .
* ongoing Mongo Observation . Exceptions thrown by the action are relayed to the caller .
*
*
* @param requestContext the context to extract the Observation from .
* @param requestContext the context to extract the Observation from .
* @param action the action to invoke .
* @param action the action to invoke .
* /
* /
private void doIn Observation( @Nullable RequestContext requestContext ,
private void stop Observation( @Nullable RequestContext requestContext ,
BiConsumer < Observation , MongoHandlerContext > action ) {
BiConsumer < Observation , MongoHandlerContext > action ) {
if ( requestContext = = null ) {
if ( requestContext = = null ) {
@ -188,7 +181,18 @@ public class MongoObservationCommandListener implements CommandListener {
return ;
return ;
}
}
action . accept ( observation , context ) ;
try {
action . accept ( observation , context ) ;
} finally {
observation . stop ( ) ;
if ( log . isDebugEnabled ( ) ) {
log . debug (
"Restoring parent observation [" + observation + "] for Mongo instrumentation and put it in Mongo context" ) ;
}
requestContext . put ( ObservationThreadLocalAccessor . KEY , observation . getContext ( ) . getParentObservation ( ) ) ;
}
}
}
/ * *
/ * *
@ -211,7 +215,7 @@ public class MongoObservationCommandListener implements CommandListener {
}
}
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "No observation was found - will not create any child observations " ) ;
log . debug ( "No observation was found: Creating a new root observation " ) ;
}
}
return null ;
return null ;