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:
Brian Clozel
2023-05-11 14:41:48 +02:00
parent 0a41104226
commit faea9d1c8b
2 changed files with 10 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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)