Skip to content

Commit ad98567

Browse files
committed
Adhoc fix for async GraphQL fetchers to log an exception
1 parent 695217c commit ad98567

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

dd-java-agent/instrumentation/graphql-java-14.0/src/main/java8/datadog/trace/instrumentation/graphqljava/InstrumentedDataFetcher.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import graphql.schema.DataFetchingEnvironment;
1212
import graphql.schema.GraphQLNamedType;
1313
import graphql.schema.GraphQLOutputType;
14+
import java.util.concurrent.CompletableFuture;
15+
import java.util.concurrent.CompletionException;
1416

1517
public class InstrumentedDataFetcher implements DataFetcher<Object> {
1618
private final DataFetcher<?> dataFetcher;
@@ -41,7 +43,20 @@ public Object get(DataFetchingEnvironment environment) throws Exception {
4143
fieldSpan.setTag("graphql.type", typeName);
4244
}
4345
try (AgentScope scope = activateSpan(fieldSpan)) {
44-
return dataFetcher.get(environment);
46+
Object result = dataFetcher.get(environment);
47+
if (result instanceof CompletableFuture) {
48+
return ((CompletableFuture<?>) result)
49+
.whenComplete(
50+
(r, e) -> {
51+
if (e instanceof CompletionException) {
52+
CompletionException ce = (CompletionException) e;
53+
DECORATE.onError(fieldSpan, ce.getCause());
54+
}
55+
});
56+
// TODO if it's a CompletableFuture then add a callback to set fieldSpan's error tags when
57+
// it fails
58+
}
59+
return result;
4560
} catch (Exception e) {
4661
fieldSpan.addThrowable(e);
4762
throw e;

dd-java-agent/instrumentation/graphql-java-14.0/src/test/groovy/GraphQLAsyncTest.groovy

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,6 @@ class GraphQLAsyncTest extends AgentTestRunner {
115115
defaultTags()
116116
}
117117
}
118-
span {
119-
operationName "getBookById"
120-
resourceName "book"
121-
childOf(span(3))
122-
spanType null
123-
errored false
124-
measured false
125-
tags {
126-
"$Tags.COMPONENT" "trace"
127-
defaultTags()
128-
}
129-
}
130118
span {
131119
operationName "graphql.field"
132120
resourceName "graphql.field"
@@ -140,6 +128,18 @@ class GraphQLAsyncTest extends AgentTestRunner {
140128
defaultTags()
141129
}
142130
}
131+
span {
132+
operationName "getBookById"
133+
resourceName "book"
134+
childOf(span(2))
135+
spanType null
136+
errored false
137+
measured false
138+
tags {
139+
"$Tags.COMPONENT" "trace"
140+
defaultTags()
141+
}
142+
}
143143
span {
144144
operationName "graphql.validation"
145145
resourceName "graphql.validation"
@@ -210,15 +210,14 @@ class GraphQLAsyncTest extends AgentTestRunner {
210210
resourceName "graphql.field"
211211
childOf(span(0))
212212
spanType DDSpanTypes.GRAPHQL
213-
// TODO this span is not marked as errored when async fetcher is in use
214-
// errored true
213+
errored true
215214
measured true
216215
tags {
217216
"$Tags.COMPONENT" "graphql-java"
218217
"graphql.type" "String"
219-
// "error.type" "java.lang.IllegalStateException"
220-
// "error.msg" "TEST"
221-
// "error.stack" String
218+
"error.type" "java.lang.IllegalStateException"
219+
"error.msg" "TEST"
220+
"error.stack" String
222221
defaultTags()
223222
}
224223
}

0 commit comments

Comments
 (0)