Skip to content

Commit a666d0d

Browse files
authored
one instrustumentation callback method (#593)
* Fixed javadoc warning * Made the instrumentation callback one method to suit lambdas and be easily to implement * Now with new method shape * fixed shape of chained code
1 parent 1fc479a commit a666d0d

9 files changed

Lines changed: 30 additions & 54 deletions

File tree

‎src/main/java/graphql/GraphQL.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
384384
CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(executionInput, instrumentationState);
385385
//
386386
// finish up instrumentation
387-
executionResult = executionResult.thenApply(er -> {
388-
executionInstrumentation.onEnd(er);
387+
executionResult = executionResult.handle((er, throwable) -> {
388+
executionInstrumentation.onEnd(er, throwable);
389389
return er;
390390
});
391391
//
@@ -429,11 +429,11 @@ private ParseResult parse(ExecutionInput executionInput, InstrumentationState in
429429
try {
430430
document = parser.parseDocument(executionInput.getQuery());
431431
} catch (ParseCancellationException e) {
432-
parseInstrumentation.onEnd(e);
432+
parseInstrumentation.onEnd(null,e);
433433
return ParseResult.ofError((RecognitionException) e.getCause());
434434
}
435435

436-
parseInstrumentation.onEnd(document);
436+
parseInstrumentation.onEnd(document,null);
437437
return ParseResult.of(document);
438438
}
439439

@@ -443,7 +443,7 @@ private List<ValidationError> validate(ExecutionInput executionInput, Document d
443443
Validator validator = new Validator();
444444
List<ValidationError> validationErrors = validator.validateDocument(graphQLSchema, document);
445445

446-
validationCtx.onEnd(validationErrors);
446+
validationCtx.onEnd(validationErrors,null);
447447
return validationErrors;
448448
}
449449

‎src/main/java/graphql/execution/Execution.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
122122
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
123123
}
124124

125-
result.thenAccept(dataFetchCtx::onEnd);
125+
result = result.whenComplete(dataFetchCtx::onEnd);
126126

127127
return result;
128128
}

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext execu
162162
.thenCompose((fetchedValue) ->
163163
completeField(executionContext, parameters, fetchedValue));
164164

165-
result.thenAccept(fieldCtx::onEnd);
165+
result.whenComplete(fieldCtx::onEnd);
166166
return result;
167167
}
168168

@@ -218,8 +218,8 @@ protected CompletableFuture<Object> fetchField(ExecutionContext executionContext
218218
fetchedValue = new CompletableFuture<>();
219219
fetchedValue.completeExceptionally(e);
220220
}
221-
fetchedValue.thenAccept(fetchCtx::onEnd);
222221
return fetchedValue.handle((result, exception) -> {
222+
fetchCtx.onEnd(result,exception);
223223
if (exception != null) {
224224
handleFetchingException(executionContext, parameters, field, fieldDef, argumentValues, environment, fetchCtx, exception);
225225
return null;
@@ -237,7 +237,7 @@ private void handleFetchingException(ExecutionContext executionContext,
237237
DataFetchingEnvironment environment,
238238
InstrumentationContext<Object> fetchCtx,
239239
Throwable e) {
240-
fetchCtx.onEnd(e);
240+
fetchCtx.onEnd(null, e);
241241

242242
DataFetcherExceptionHandlerParameters handlerParameters = DataFetcherExceptionHandlerParameters.newExceptionParameters()
243243
.executionContext(executionContext)

‎src/main/java/graphql/execution/instrumentation/ChainedInstrumentation.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,8 @@ private static class ChainedInstrumentationContext<T> implements Instrumentation
150150
}
151151

152152
@Override
153-
public void onEnd(T result) {
154-
contexts.forEach(context -> context.onEnd(result));
155-
}
156-
157-
@Override
158-
public void onEnd(Exception e) {
159-
contexts.forEach(context -> context.onEnd(e));
153+
public void onEnd(T result, Throwable t) {
154+
contexts.forEach(context -> context.onEnd(result, t));
160155
}
161156
}
162157
}

‎src/main/java/graphql/execution/instrumentation/Instrumentation.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616
/**
1717
* Provides the capability to instrument the execution steps of a GraphQL query.
18-
* <p>
18+
*
1919
* For example you might want to track which fields are taking the most time to fetch from the backing database
2020
* or log what fields are being asked for.
21+
*
22+
* Remember that graphql calls can cross threads so make sure you think about the thread safety of any instrumentation
23+
* code when you are writing it.
2124
*/
2225
public interface Instrumentation {
2326

@@ -32,7 +35,7 @@ default InstrumentationState createState() {
3235
}
3336

3437
/**
35-
* This is called just before a query is executed and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
38+
* This is called just before a query is executed and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
3639
* will be called indicating that the step has finished.
3740
*
3841
* @param parameters the parameters to this step
@@ -42,7 +45,7 @@ default InstrumentationState createState() {
4245
InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters);
4346

4447
/**
45-
* This is called just before a query is parsed and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
48+
* This is called just before a query is parsed and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
4649
* will be called indicating that the step has finished.
4750
*
4851
* @param parameters the parameters to this step
@@ -52,7 +55,7 @@ default InstrumentationState createState() {
5255
InstrumentationContext<Document> beginParse(InstrumentationExecutionParameters parameters);
5356

5457
/**
55-
* This is called just before the parsed query Document is validated and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
58+
* This is called just before the parsed query Document is validated and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
5659
* will be called indicating that the step has finished.
5760
*
5861
* @param parameters the parameters to this step
@@ -62,7 +65,7 @@ default InstrumentationState createState() {
6265
InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters);
6366

6467
/**
65-
* This is called just before the data fetch is started and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
68+
* This is called just before the data fetch is started and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
6669
* will be called indicating that the step has finished.
6770
*
6871
* @param parameters the parameters to this step
@@ -72,7 +75,7 @@ default InstrumentationState createState() {
7275
InstrumentationContext<ExecutionResult> beginDataFetch(InstrumentationDataFetchParameters parameters);
7376

7477
/**
75-
* This is called just before a field is resolved and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
78+
* This is called just before a field is resolved and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
7679
* will be called indicating that the step has finished.
7780
*
7881
* @param parameters the parameters to this step
@@ -82,7 +85,7 @@ default InstrumentationState createState() {
8285
InstrumentationContext<ExecutionResult> beginField(InstrumentationFieldParameters parameters);
8386

8487
/**
85-
* This is called just before a field {@link DataFetcher} is invoked and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
88+
* This is called just before a field {@link DataFetcher} is invoked and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
8689
* will be called indicating that the step has finished.
8790
*
8891
* @param parameters the parameters to this step

‎src/main/java/graphql/execution/instrumentation/InstrumentationContext.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
/**
44
* When a {@link Instrumentation}.'beginXXX' method is called then it must return a non null InstrumentationContext
5-
* that will the be invoked as {@link #onEnd(Object)} or {@link #onEnd(Exception)} when the step completes.
5+
* that will the be invoked as {@link #onEnd(Object, java.lang.Throwable)} when the step completes.
66
*
77
* This pattern of construction of an object then call back is intended to allow "timers" to be created that can instrument what has
88
* just happened or "loggers" to be called to record what has happened.
99
*/
1010
public interface InstrumentationContext<T> {
1111

1212
/**
13-
* This is invoked when the execution step is completed successfully
14-
* @param result the successful result of the step
13+
* This is invoked when the execution step is completed
14+
*
15+
* @param result the result of the step (which may be null)
16+
* @param t this exception will be non null if an exception was thrown during the step
1517
*/
16-
void onEnd(T result);
18+
void onEnd(T result, Throwable t);
1719

18-
/**
19-
* This is invoked when the execution step is completed unsuccessfully
20-
* @param e the exception that happened during the step
21-
*/
22-
void onEnd(Exception e);
2320
}

‎src/main/java/graphql/execution/instrumentation/NoOpInstrumentation.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchP
5353

5454
public static class NoOpInstrumentationContext<T> implements InstrumentationContext<T> {
5555
@Override
56-
public void onEnd(T result) {
57-
}
58-
59-
@Override
60-
public void onEnd(Exception e) {
56+
public void onEnd(T result, Throwable t) {
6157
}
6258
}
6359
}

‎src/main/java/graphql/execution/instrumentation/tracing/TracingInstrumentation.java‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,7 @@ public CompletableFuture<ExecutionResult> instrumentExecutionResult(CompletableF
4646
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
4747
TracingSupport tracingSupport = parameters.getInstrumentationState();
4848
TracingSupport.TracingContext ctx = tracingSupport.beginField(parameters.getEnvironment());
49-
return new InstrumentationContext<Object>() {
50-
@Override
51-
public void onEnd(Object result) {
52-
ctx.onEnd();
53-
}
54-
55-
@Override
56-
public void onEnd(Exception e) {
57-
ctx.onEnd();
58-
}
59-
};
49+
return (result, t) -> ctx.onEnd();
6050
}
6151

6252
@Override

‎src/test/groovy/graphql/execution/instrumentation/TestingInstrumentContext.groovy‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ class TestingInstrumentContext<T> implements InstrumentationContext<T> {
1919
}
2020

2121
@Override
22-
void onEnd(T result) {
23-
end()
24-
}
25-
26-
@Override
27-
void onEnd(Exception e) {
22+
void onEnd(T result, Throwable t) {
2823
end()
2924
}
3025
}

0 commit comments

Comments
 (0)