Skip to content

Commit faea9d1

Browse files
committed
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
1 parent 0a41104 commit faea9d1

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

spring-graphql/src/main/java/org/springframework/graphql/observation/GraphQlObservationInstrumentation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.apache.commons.logging.LogFactory;
3636
import org.springframework.lang.Nullable;
3737

38-
import java.util.concurrent.CompletableFuture;
38+
import java.util.concurrent.CompletionException;
3939
import java.util.concurrent.CompletionStage;
4040

4141
/**
@@ -152,11 +152,10 @@ public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher,
152152
if (error != null) {
153153
dataFetcherObservation.error(error);
154154
dataFetcherObservation.stop();
155-
return CompletableFuture.failedStage(error);
155+
throw new CompletionException(error);
156156
}
157157
dataFetcherObservation.stop();
158-
return CompletableFuture.completedStage(wrapAsDataFetcherResult(result, dataFetcherObservation));
159-
158+
return wrapAsDataFetcherResult(result, dataFetcherObservation);
160159
});
161160
}
162161
else {

spring-graphql/src/test/java/org/springframework/graphql/observation/GraphQlObservationInstrumentationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ void instrumentGraphQlRequestWhenSuccess(DataFetcher<?> dataFetcher) {
6868
.toGraphQlService()
6969
.execute(TestExecutionRequest.forDocument(document));
7070
ResponseHelper response = ResponseHelper.forResponse(responseMono);
71+
72+
String name = response.rawValue("bookById.name");
73+
assertThat(name).isEqualTo("Nineteen Eighty-Four");
74+
7175
TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request")
7276
.that().hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS")
7377
.hasHighCardinalityKeyValueWithKey("graphql.execution.id");
@@ -167,8 +171,10 @@ void instrumentGraphQlRequestWhenDataFetchingFailure(DataFetcher<?> dataFetcher)
167171
.toGraphQlService()
168172
.execute(TestExecutionRequest.forDocument(document));
169173
ResponseHelper response = ResponseHelper.forResponse(responseMono);
174+
assertThat(response.error(0).message()).isEqualTo("Resolved error: book fetching failure");
175+
170176
TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request")
171-
.that().hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS")
177+
.that().hasLowCardinalityKeyValue("graphql.outcome", "REQUEST_ERROR")
172178
.hasHighCardinalityKeyValueWithKey("graphql.execution.id");
173179

174180
TestObservationRegistryAssert.assertThat(this.observationRegistry)

0 commit comments

Comments
 (0)