22
33import graphql .ExecutionResult ;
44import graphql .execution .instrumentation .InstrumentationContext ;
5- import graphql .execution .instrumentation .InstrumentationState ;
6- import graphql .execution .instrumentation .SimpleInstrumentation ;
75import graphql .execution .instrumentation .SimpleInstrumentationContext ;
86import graphql .execution .instrumentation .parameters .InstrumentationExecutionParameters ;
9- import graphql .servlet . GraphQLContext ;
7+ import graphql .execution . instrumentation . tracing . TracingInstrumentation ;
108import io .micrometer .core .instrument .MeterRegistry ;
119import io .micrometer .core .instrument .Timer ;
1210
11+ import java .util .Map ;
1312import java .util .concurrent .TimeUnit ;
1413
1514/**
1615 * @author Bruno Rodrigues
1716 */
18- public class MetricsInstrumentation extends SimpleInstrumentation {
17+ public class MetricsInstrumentation extends TracingInstrumentation {
1918
2019 private MeterRegistry meterRegistry ;
2120
2221 private static final String OPERATION_TIME_METRIC_NAME = "graphql.timer.operation" ;
2322 private static final String OPERATION_NAME_TAG = "operationName" ;
23+ private static final String OPERATION = "operation" ;
2424 private static final String UNKNOWN_OPERATION_NAME = "unknown" ;
2525 private static final String EXCEPTION_TAG = "exceptionName" ;
2626 private static final String NONE_EXCEPTION = "None" ;
@@ -30,26 +30,20 @@ public MetricsInstrumentation(MeterRegistry meterRegistry) {
3030 this .meterRegistry = meterRegistry ;
3131 }
3232
33- //This create a custom instrumentation state that stores any needed value.
34- //In this case, it stores the begin time of the query
35- @ Override
36- public InstrumentationState createState () {
37- return new MetricsSupport ();
38- }
39-
4033 @ Override
4134 public InstrumentationContext <ExecutionResult > beginExecution (InstrumentationExecutionParameters parameters ) {
4235
4336 return new SimpleInstrumentationContext <ExecutionResult >() {
4437 @ Override
4538 public void onCompleted (ExecutionResult result , Throwable t ) {
4639
47- GraphQLContext graphQLContext = parameters .getContext ();
48- MetricsSupport metricsSupport = parameters .getInstrumentationState ();
49- if (graphQLContext .getHttpServletRequest ().isPresent ()) {
40+ if (result .getExtensions ().containsKey ("tracingData" )) {
5041
51- Timer timer = buildTimer (parameters .getOperation (), t );
52- timer .record (metricsSupport .getTotalTime (), TimeUnit .NANOSECONDS );
42+ Map <String , Object > tracingData = (Map <String , Object >) result .getExtensions ().get ("tracing" );
43+ Timer timer = buildTimer (parameters .getOperation (), "execution" , t );
44+ timer .record ((long ) tracingData .get ("duration" ), TimeUnit .NANOSECONDS );
45+ timer .record ((long ) ((Map <String , Object >)tracingData .get ("validation" )).get ("duration" ), TimeUnit .NANOSECONDS );
46+ timer .record ((long ) ((Map <String , Object >)tracingData .get ("parsing" )).get ("duration" ), TimeUnit .NANOSECONDS );
5347
5448 }
5549
@@ -59,10 +53,11 @@ public void onCompleted(ExecutionResult result, Throwable t) {
5953
6054 }
6155
62- private Timer buildTimer (String operationName , Throwable t ) {
56+ private Timer buildTimer (String operationName , String operation , Throwable t ) {
6357 return Timer .builder (OPERATION_TIME_METRIC_NAME )
6458 .description (TIMER_DESCRIPTION )
6559 .tag (OPERATION_NAME_TAG , operationName != null ? operationName : UNKNOWN_OPERATION_NAME )
60+ .tag (OPERATION , operation )
6661 .tag (EXCEPTION_TAG , t == null ? NONE_EXCEPTION : t .getClass ().getSimpleName ())
6762 .register (meterRegistry );
6863 }
0 commit comments