Skip to content

Commit a4bc936

Browse files
authored
Merge pull request #207 from Netflix/update-instrumentation
Update instrumentation docs to match updated examples
2 parents f6f5aef + b500a81 commit a4bc936

1 file changed

Lines changed: 42 additions & 41 deletions

File tree

docs/advanced/instrumentation.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ If we wouldn't do this, the result for an async data fetcher would always be 0,
1818
```java
1919

2020
@Component
21-
public class ExampleTracingInstrumentation extends SimpleInstrumentation {
21+
public class ExampleTracingInstrumentation extends SimplePerformantInstrumentation {
2222
private final static Logger LOGGER = LoggerFactory.getLogger(ExampleTracingInstrumentation.class);
2323

2424
@Override
25-
public InstrumentationState createState() {
25+
public @Nullable InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
2626
return new TracingState();
2727
}
28-
28+
2929
@Override
30-
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
31-
TracingState tracingState = parameters.getInstrumentationState();
30+
public @Nullable InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters, InstrumentationState state) {
31+
TracingState tracingState = (TracingState) state;
3232
tracingState.startTime = System.currentTimeMillis();
33-
return super.beginExecution(parameters);
33+
return super.beginExecution(parameters, state);
3434
}
35-
35+
3636
@Override
37-
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
37+
public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
3838
// We only care about user code
3939
if(parameters.isTrivialDataFetcher()) {
4040
return dataFetcher;
4141
}
42-
42+
4343
return environment -> {
4444
long startTime = System.currentTimeMillis();
4545
Object result = dataFetcher.get(environment);
@@ -52,20 +52,20 @@ If we wouldn't do this, the result for an async data fetcher would always be 0,
5252
long totalTime = System.currentTimeMillis() - startTime;
5353
LOGGER.info("Datafetcher {} took {}ms", findDatafetcherTag(parameters), totalTime);
5454
}
55-
55+
5656
return result;
5757
};
5858
}
59-
59+
6060
@Override
61-
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
62-
TracingState tracingState = parameters.getInstrumentationState();
61+
public @NotNull CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters, InstrumentationState state) {
62+
TracingState tracingState = (TracingState) state;
6363
long totalTime = System.currentTimeMillis() - tracingState.startTime;
6464
LOGGER.info("Total execution time: {}ms", totalTime);
65-
66-
return super.instrumentExecutionResult(executionResult, parameters);
65+
66+
return super.instrumentExecutionResult(executionResult, parameters, state);
6767
}
68-
68+
6969
private String findDatafetcherTag(InstrumentationFieldFetchParameters parameters) {
7070
GraphQLOutputType type = parameters.getExecutionStepInfo().getParent().getType();
7171
GraphQLObjectType parent;
@@ -74,10 +74,10 @@ If we wouldn't do this, the result for an async data fetcher would always be 0,
7474
} else {
7575
parent = (GraphQLObjectType) type;
7676
}
77-
77+
7878
return parent.getName() + "." + parameters.getExecutionStepInfo().getPath().getSegmentName();
7979
}
80-
80+
8181
static class TracingState implements InstrumentationState {
8282
long startTime;
8383
}
@@ -86,29 +86,30 @@ If we wouldn't do this, the result for an async data fetcher would always be 0,
8686
=== "Kotlin"
8787
```kotlin
8888
@Component
89-
class ExampleTracingInstrumentation: SimpleInstrumentation() {
90-
89+
class ExampleTracingInstrumentation: SimplePerformantInstrumentation() {
90+
9191
val logger : Logger = LoggerFactory.getLogger(ExampleTracingInstrumentation::class.java)
92-
93-
override fun createState(): InstrumentationState {
92+
93+
override fun createState(parameters: InstrumentationCreateStateParameters): InstrumentationState {
9494
return TraceState()
9595
}
96-
97-
override fun beginExecution(parameters: InstrumentationExecutionParameters): InstrumentationContext<ExecutionResult> {
98-
val state: TraceState = parameters.getInstrumentationState()
96+
97+
override fun beginExecution(parameters: InstrumentationExecutionParameters, state: InstrumentationState): InstrumentationContext<ExecutionResult>? {
98+
require(state is TraceState)
9999
state.traceStartTime = System.currentTimeMillis()
100-
101-
return super.beginExecution(parameters)
100+
101+
return super.beginExecution(parameters, state)
102102
}
103-
104-
override fun instrumentDataFetcher(dataFetcher: DataFetcher<*>, parameters: InstrumentationFieldFetchParameters): DataFetcher<*> {
103+
104+
override fun instrumentDataFetcher(dataFetcher: DataFetcher<*>, parameters: InstrumentationFieldFetchParameters, state: InstrumentationState): DataFetcher<*> {
105+
105106
// We only care about user code
106-
if(parameters.isTrivialDataFetcher) {
107+
if(parameters.isTrivialDataFetcher || parameters.executionStepInfo.path.toString().startsWith("/__schema")) {
107108
return dataFetcher
108109
}
109-
110+
110111
val dataFetcherName = findDatafetcherTag(parameters)
111-
112+
112113
return DataFetcher { environment ->
113114
val startTime = System.currentTimeMillis()
114115
val result = dataFetcher.get(environment)
@@ -121,30 +122,30 @@ If we wouldn't do this, the result for an async data fetcher would always be 0,
121122
val totalTime = System.currentTimeMillis() - startTime
122123
logger.info("Datafetcher '$dataFetcherName': ${totalTime}ms")
123124
}
124-
125+
125126
result
126127
}
127128
}
128-
129-
override fun instrumentExecutionResult(executionResult: ExecutionResult, parameters: InstrumentationExecutionParameters): CompletableFuture<ExecutionResult> {
130-
val state: TraceState = parameters.getInstrumentationState()
129+
130+
override fun instrumentExecutionResult(executionResult: ExecutionResult, parameters: InstrumentationExecutionParameters, state: InstrumentationState): CompletableFuture<ExecutionResult> {
131+
require(state is TraceState)
131132
val totalTime = System.currentTimeMillis() - state.traceStartTime
132133
logger.info("Total execution time: ${totalTime}ms")
133-
134-
return super.instrumentExecutionResult(executionResult, parameters)
134+
135+
return super.instrumentExecutionResult(executionResult, parameters, state)
135136
}
136-
137+
137138
private fun findDatafetcherTag(parameters: InstrumentationFieldFetchParameters): String {
138139
val type = parameters.executionStepInfo.parent.type
139140
val parentType = if (type is GraphQLNonNull) {
140141
type.wrappedType as GraphQLObjectType
141142
} else {
142143
type as GraphQLObjectType
143144
}
144-
145+
145146
return "${parentType.name}.${parameters.executionStepInfo.path.segmentName}"
146147
}
147-
148+
148149
data class TraceState(var traceStartTime: Long = 0): InstrumentationState
149150
}
150151
```

0 commit comments

Comments
 (0)