Skip to content

Commit 3d0079b

Browse files
authored
Merge pull request #116 from graphql-java/v20-instrumentation
Add Instrumentation hooks with state parameters
2 parents 1f00b49 + 1e143fa commit 3d0079b

2 files changed

Lines changed: 14 additions & 16 deletions

File tree

documentation/instrumentation.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: Instrumentation allows you to inject code that can observe the exec
88
The ``graphql.execution.instrumentation.Instrumentation`` interface allows you to inject code that can observe the
99
execution of a query and also change the runtime behaviour.
1010

11-
The primary use case for this is to allow say performance monitoring and custom logging but it could be used for many different purposes.
11+
The primary use case for this is to allow say performance monitoring and custom logging, but it could be used for many purposes.
1212

1313
When you build the ``Graphql`` object you can specify what ``Instrumentation`` to use (if any).
1414

@@ -22,7 +22,7 @@ GraphQL.newGraphQL(schema)
2222

2323
An implementation of ``Instrumentation`` needs to implement the "begin" step methods that represent the execution of a graphql query.
2424

25-
Each step must give back a non null ``graphql.execution.instrumentation.InstrumentationContext`` object which will be called back
25+
Each step must give back a non-null ``graphql.execution.instrumentation.InstrumentationContext`` object which will be called back
2626
when the step completes, and will be told that it succeeded or failed with a Throwable.
2727

2828
The following is a basic custom ``Instrumentation`` that measures overall execution time and puts it into a stateful object.
@@ -47,19 +47,18 @@ class CustomInstrumentation extends SimpleInstrumentation {
4747
}
4848

4949
@Override
50-
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
50+
public @Nullable InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters, InstrumentationState state) {
5151
long startNanos = System.nanoTime();
5252
return new SimpleInstrumentationContext<ExecutionResult>() {
5353
@Override
5454
public void onCompleted(ExecutionResult result, Throwable t) {
55-
CustomInstrumentationState state = parameters.getInstrumentationState();
56-
state.recordTiming(parameters.getQuery(), System.nanoTime() - startNanos);
55+
((CustomInstrumentationState) state).recordTiming(parameters.getQuery(), System.nanoTime() - startNanos);
5756
}
5857
};
5958
}
6059

6160
@Override
62-
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
61+
public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
6362
//
6463
// this allows you to intercept the data fetcher used to fetch a field and provide another one, perhaps
6564
// that enforces certain behaviours or has certain side effects on the data
@@ -68,9 +67,9 @@ class CustomInstrumentation extends SimpleInstrumentation {
6867
}
6968

7069
@Override
71-
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
70+
public @NotNull CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters, InstrumentationState state) {
7271
//
73-
// this allows you to instrument the execution result some how. For example the Tracing support uses this to put
72+
// this allows you to instrument the execution result somehow. For example the Tracing support uses this to put
7473
// the `extensions` map of data in place
7574
//
7675
return CompletableFuture.completedFuture(executionResult);

versioned_docs/version-v20/instrumentation.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: Instrumentation allows you to inject code that can observe the exec
88
The ``graphql.execution.instrumentation.Instrumentation`` interface allows you to inject code that can observe the
99
execution of a query and also change the runtime behaviour.
1010

11-
The primary use case for this is to allow say performance monitoring and custom logging but it could be used for many different purposes.
11+
The primary use case for this is to allow say performance monitoring and custom logging, but it could be used for many purposes.
1212

1313
When you build the ``Graphql`` object you can specify what ``Instrumentation`` to use (if any).
1414

@@ -22,7 +22,7 @@ GraphQL.newGraphQL(schema)
2222

2323
An implementation of ``Instrumentation`` needs to implement the "begin" step methods that represent the execution of a graphql query.
2424

25-
Each step must give back a non null ``graphql.execution.instrumentation.InstrumentationContext`` object which will be called back
25+
Each step must give back a non-null ``graphql.execution.instrumentation.InstrumentationContext`` object which will be called back
2626
when the step completes, and will be told that it succeeded or failed with a Throwable.
2727

2828
The following is a basic custom ``Instrumentation`` that measures overall execution time and puts it into a stateful object.
@@ -47,19 +47,18 @@ class CustomInstrumentation extends SimpleInstrumentation {
4747
}
4848

4949
@Override
50-
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
50+
public @Nullable InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters, InstrumentationState state) {
5151
long startNanos = System.nanoTime();
5252
return new SimpleInstrumentationContext<ExecutionResult>() {
5353
@Override
5454
public void onCompleted(ExecutionResult result, Throwable t) {
55-
CustomInstrumentationState state = parameters.getInstrumentationState();
56-
state.recordTiming(parameters.getQuery(), System.nanoTime() - startNanos);
55+
((CustomInstrumentationState) state).recordTiming(parameters.getQuery(), System.nanoTime() - startNanos);
5756
}
5857
};
5958
}
6059

6160
@Override
62-
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
61+
public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
6362
//
6463
// this allows you to intercept the data fetcher used to fetch a field and provide another one, perhaps
6564
// that enforces certain behaviours or has certain side effects on the data
@@ -68,9 +67,9 @@ class CustomInstrumentation extends SimpleInstrumentation {
6867
}
6968

7069
@Override
71-
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
70+
public @NotNull CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters, InstrumentationState state) {
7271
//
73-
// this allows you to instrument the execution result some how. For example the Tracing support uses this to put
72+
// this allows you to instrument the execution result somehow. For example the Tracing support uses this to put
7473
// the `extensions` map of data in place
7574
//
7675
return CompletableFuture.completedFuture(executionResult);

0 commit comments

Comments
 (0)