Fix observability instrumentation for CompletionStage
This commit fixes the observability instrumentation for data fetchers when the return type is of `CompletionStage`. Prior to this commit, the instrumentation of `CompletionStage` return values as `DataFetcherResult` would wrap the asynchronous value twice. Closes gh-676
This commit is contained in:
@@ -35,7 +35,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
/**
|
||||
@@ -152,11 +152,10 @@ public class GraphQlObservationInstrumentation extends SimpleInstrumentation {
|
||||
if (error != null) {
|
||||
dataFetcherObservation.error(error);
|
||||
dataFetcherObservation.stop();
|
||||
return CompletableFuture.failedStage(error);
|
||||
throw new CompletionException(error);
|
||||
}
|
||||
dataFetcherObservation.stop();
|
||||
return CompletableFuture.completedStage(wrapAsDataFetcherResult(result, dataFetcherObservation));
|
||||
|
||||
return wrapAsDataFetcherResult(result, dataFetcherObservation);
|
||||
});
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -68,6 +68,10 @@ class GraphQlObservationInstrumentationTests {
|
||||
.toGraphQlService()
|
||||
.execute(TestExecutionRequest.forDocument(document));
|
||||
ResponseHelper response = ResponseHelper.forResponse(responseMono);
|
||||
|
||||
String name = response.rawValue("bookById.name");
|
||||
assertThat(name).isEqualTo("Nineteen Eighty-Four");
|
||||
|
||||
TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request")
|
||||
.that().hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS")
|
||||
.hasHighCardinalityKeyValueWithKey("graphql.execution.id");
|
||||
@@ -167,8 +171,10 @@ class GraphQlObservationInstrumentationTests {
|
||||
.toGraphQlService()
|
||||
.execute(TestExecutionRequest.forDocument(document));
|
||||
ResponseHelper response = ResponseHelper.forResponse(responseMono);
|
||||
assertThat(response.error(0).message()).isEqualTo("Resolved error: book fetching failure");
|
||||
|
||||
TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request")
|
||||
.that().hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS")
|
||||
.that().hasLowCardinalityKeyValue("graphql.outcome", "REQUEST_ERROR")
|
||||
.hasHighCardinalityKeyValueWithKey("graphql.execution.id");
|
||||
|
||||
TestObservationRegistryAssert.assertThat(this.observationRegistry)
|
||||
|
||||
Reference in New Issue
Block a user