Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(executionInput, instrumentationState);
//
// finish up instrumentation
executionResult = executionResult.thenApply(er -> {
executionInstrumentation.onEnd(er);
executionResult = executionResult.handle((er, throwable) -> {
executionInstrumentation.onEnd(er, throwable);
return er;
});
//
Expand Down Expand Up @@ -429,11 +429,11 @@ private ParseResult parse(ExecutionInput executionInput, InstrumentationState in
try {
document = parser.parseDocument(executionInput.getQuery());
} catch (ParseCancellationException e) {
parseInstrumentation.onEnd(e);
parseInstrumentation.onEnd(null,e);
return ParseResult.ofError((RecognitionException) e.getCause());
}

parseInstrumentation.onEnd(document);
parseInstrumentation.onEnd(document,null);
return ParseResult.of(document);
}

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

validationCtx.onEnd(validationErrors);
validationCtx.onEnd(validationErrors,null);
return validationErrors;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
}

result.thenAccept(dataFetchCtx::onEnd);
result = result.whenComplete(dataFetchCtx::onEnd);

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext execu
.thenCompose((fetchedValue) ->
completeField(executionContext, parameters, fetchedValue));

result.thenAccept(fieldCtx::onEnd);
result.whenComplete(fieldCtx::onEnd);
return result;
}

Expand Down Expand Up @@ -218,8 +218,8 @@ protected CompletableFuture<Object> fetchField(ExecutionContext executionContext
fetchedValue = new CompletableFuture<>();
fetchedValue.completeExceptionally(e);
}
fetchedValue.thenAccept(fetchCtx::onEnd);
return fetchedValue.handle((result, exception) -> {
fetchCtx.onEnd(result,exception);
if (exception != null) {
handleFetchingException(executionContext, parameters, field, fieldDef, argumentValues, environment, fetchCtx, exception);
return null;
Expand All @@ -237,7 +237,7 @@ private void handleFetchingException(ExecutionContext executionContext,
DataFetchingEnvironment environment,
InstrumentationContext<Object> fetchCtx,
Throwable e) {
fetchCtx.onEnd(e);
fetchCtx.onEnd(null, e);

DataFetcherExceptionHandlerParameters handlerParameters = DataFetcherExceptionHandlerParameters.newExceptionParameters()
.executionContext(executionContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,8 @@ private static class ChainedInstrumentationContext<T> implements Instrumentation
}

@Override
public void onEnd(T result) {
contexts.forEach(context -> context.onEnd(result));
}

@Override
public void onEnd(Exception e) {
contexts.forEach(context -> context.onEnd(e));
public void onEnd(T result, Throwable t) {
contexts.forEach(context -> context.onEnd(result, t));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

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

Expand All @@ -32,7 +35,7 @@ default InstrumentationState createState() {
}

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

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

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

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

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

/**
* This is called just before a field {@link DataFetcher} is invoked and when this step finishes the {@link InstrumentationContext#onEnd(Object)}
* This is called just before a field {@link DataFetcher} is invoked and when this step finishes the {@link InstrumentationContext#onEnd(Object, Throwable)}
* will be called indicating that the step has finished.
*
* @param parameters the parameters to this step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

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

/**
* This is invoked when the execution step is completed successfully
* @param result the successful result of the step
* This is invoked when the execution step is completed
*
* @param result the result of the step (which may be null)
* @param t this exception will be non null if an exception was thrown during the step
*/
void onEnd(T result);
void onEnd(T result, Throwable t);

/**
* This is invoked when the execution step is completed unsuccessfully
* @param e the exception that happened during the step
*/
void onEnd(Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchP

public static class NoOpInstrumentationContext<T> implements InstrumentationContext<T> {
@Override
public void onEnd(T result) {
}

@Override
public void onEnd(Exception e) {
public void onEnd(T result, Throwable t) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,7 @@ public CompletableFuture<ExecutionResult> instrumentExecutionResult(CompletableF
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
TracingSupport tracingSupport = parameters.getInstrumentationState();
TracingSupport.TracingContext ctx = tracingSupport.beginField(parameters.getEnvironment());
return new InstrumentationContext<Object>() {
@Override
public void onEnd(Object result) {
ctx.onEnd();
}

@Override
public void onEnd(Exception e) {
ctx.onEnd();
}
};
return (result, t) -> ctx.onEnd();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ class TestingInstrumentContext<T> implements InstrumentationContext<T> {
}

@Override
void onEnd(T result) {
end()
}

@Override
void onEnd(Exception e) {
void onEnd(T result, Throwable t) {
end()
}
}
Expand Down