|
31 | 31 | import io.opentelemetry.instrumentation.graphql.common.v12_0.internal.OpenTelemetryInstrumentationHelper; |
32 | 32 | import io.opentelemetry.instrumentation.graphql.common.v12_0.internal.OpenTelemetryInstrumentationState; |
33 | 33 | import java.util.concurrent.CompletionStage; |
| 34 | +import javax.annotation.Nullable; |
34 | 35 |
|
35 | 36 | final class OpenTelemetryInstrumentation extends SimplePerformantInstrumentation { |
36 | 37 | private final OpenTelemetryInstrumentationHelper helper; |
@@ -89,35 +90,30 @@ public DataFetcher<?> instrumentDataFetcher( |
89 | 90 | Context childContext = dataFetcherInstrumenter.start(parentContext, environment); |
90 | 91 | state.setContextForPath(path, childContext); |
91 | 92 |
|
92 | | - boolean isCompletionStage = false; |
93 | | - |
94 | | - Object fieldValue = null; |
| 93 | + Object fieldValue; |
95 | 94 | try (Scope ignored = childContext.makeCurrent()) { |
96 | 95 | 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; |
108 | 96 | } catch (Throwable throwable) { |
109 | 97 | dataFetcherInstrumenter.end(childContext, environment, null, throwable); |
110 | 98 | throw throwable; |
111 | | - } finally { |
112 | | - if (!isCompletionStage) { |
113 | | - handleDataFetcherResult(childContext, fieldValue); |
114 | | - dataFetcherInstrumenter.end(childContext, environment, fieldValue, null); |
115 | | - } |
116 | 99 | } |
| 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; |
117 | 113 | }; |
118 | 114 | } |
119 | 115 |
|
120 | | - private static void handleDataFetcherResult(Context context, Object result) { |
| 116 | + private static void handleDataFetcherResult(Context context, @Nullable Object result) { |
121 | 117 | if (!(result instanceof DataFetcherResult)) { |
122 | 118 | return; |
123 | 119 | } |
|
0 commit comments