Skip to content

Commit 8dc3ead

Browse files
otelbot[bot]trask
andauthored
Review fixes for graphql-java-20.0:library (#17345)
Co-authored-by: otelbot <197425009+otelbot@users.noreply.github.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent 91d1b07 commit 8dc3ead

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

instrumentation/graphql-java/graphql-java-20.0/library/src/main/java/io/opentelemetry/instrumentation/graphql/v20_0/OpenTelemetryInstrumentation.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.opentelemetry.instrumentation.graphql.common.v12_0.internal.OpenTelemetryInstrumentationHelper;
3232
import io.opentelemetry.instrumentation.graphql.common.v12_0.internal.OpenTelemetryInstrumentationState;
3333
import java.util.concurrent.CompletionStage;
34+
import javax.annotation.Nullable;
3435

3536
final class OpenTelemetryInstrumentation extends SimplePerformantInstrumentation {
3637
private final OpenTelemetryInstrumentationHelper helper;
@@ -89,35 +90,30 @@ public DataFetcher<?> instrumentDataFetcher(
8990
Context childContext = dataFetcherInstrumenter.start(parentContext, environment);
9091
state.setContextForPath(path, childContext);
9192

92-
boolean isCompletionStage = false;
93-
94-
Object fieldValue = null;
93+
Object fieldValue;
9594
try (Scope ignored = childContext.makeCurrent()) {
9695
fieldValue = dataFetcher.get(environment);
97-
isCompletionStage = fieldValue instanceof CompletionStage;
98-
99-
if (isCompletionStage) {
100-
return ((CompletionStage<?>) fieldValue)
101-
.whenComplete(
102-
(result, throwable) -> {
103-
handleDataFetcherResult(childContext, result);
104-
dataFetcherInstrumenter.end(childContext, environment, result, throwable);
105-
});
106-
}
107-
return fieldValue;
10896
} catch (Throwable throwable) {
10997
dataFetcherInstrumenter.end(childContext, environment, null, throwable);
11098
throw throwable;
111-
} finally {
112-
if (!isCompletionStage) {
113-
handleDataFetcherResult(childContext, fieldValue);
114-
dataFetcherInstrumenter.end(childContext, environment, fieldValue, null);
115-
}
11699
}
100+
101+
if (fieldValue instanceof CompletionStage) {
102+
return ((CompletionStage<?>) fieldValue)
103+
.whenComplete(
104+
(result, throwable) -> {
105+
handleDataFetcherResult(childContext, result);
106+
dataFetcherInstrumenter.end(childContext, environment, result, throwable);
107+
});
108+
}
109+
110+
handleDataFetcherResult(childContext, fieldValue);
111+
dataFetcherInstrumenter.end(childContext, environment, fieldValue, null);
112+
return fieldValue;
117113
};
118114
}
119115

120-
private static void handleDataFetcherResult(Context context, Object result) {
116+
private static void handleDataFetcherResult(Context context, @Nullable Object result) {
121117
if (!(result instanceof DataFetcherResult)) {
122118
return;
123119
}

0 commit comments

Comments
 (0)