title: GraphQL + Spring Boot: how to collect (error) metrics?
body: <p>I've been working on adding monitoring metrics in our GraphQL gateway recently.</p>
  
  <p>We're using <a href="https://github.com/graphql-java-kickstart/graphql-spring-boot" rel="nofollow noreferrer">graphql-spring-boot</a> starter for the gateway.</p>
  
  <p>After reading the following documentations, I manage to send the basic graphql.timer.query.* metrics to Datadog</p>
  
  <ul>
  <li><a href="https://www.baeldung.com/spring-boot-actuators" rel="nofollow noreferrer">https://www.baeldung.com/spring-boot-actuators</a></li>
  <li><a href="https://docs.spring.io/spring-boot/docs/2.0.x/actuator-api/html/#metrics" rel="nofollow noreferrer">https://docs.spring.io/spring-boot/docs/2.0.x/actuator-api/html/#metrics</a></li>
  <li><a href="https://github.com/graphql-java-kickstart/graphql-spring-boot#tracing-and-metrics" rel="nofollow noreferrer">https://github.com/graphql-java-kickstart/graphql-spring-boot#tracing-and-metrics</a></li>
  </ul>
  
  <p>What I've achieved so far is, when I send a GraphQL query/mutation, I'd collect the request count and time accordingly. e.g. sending the query below</p>
  
  <pre><code>query HelloWorldQuery {
    greeting(
      name: "Bob"
    ) {
      message
    }
  }
  </code></pre>
  
  <p>I'll see metrics <code>graphql.timer.query.count</code> / <code>graphql.timer.query.sum</code> with tags <code>operationName=HelloWorldQuery</code></p>
  
  <p>It works like perfectly, until I want to test a query with errors. I realise there is no metrics/tags related to a failed query. For example, if I the above query returns null data and some GraphQL errors, I'd still collect <code>graphql.timer.query.count (operationName=HelloWorldQuery)</code>, but there's no additional tags for me to tell there is an error for that query.</p>
  
  <p>In the gateway, I have implemented a custom <code>GraphQLErrorHandler</code>, so I was thinking maybe I should add error counter (via MeterRegistry) in that class, but I am unable to get the <code>operationName</code> simply from GraphQLError type. the best I can get is error.getPath() which gives the method name (e.g. <code>greeting</code>) rather than the custom query name (<code>HelloWorldQuery</code> - to be consistent with what <code>graphql.timer.query.*</code> provides).</p>
  
  <p>My question is, how to solve the above problem? 
  And generally what is the best way of collecting GraphQL query metrics (including errors)?</p>
  
  <p>------------------- <strong>Update</strong> -------------------</p>
  
  <p><strong>2019-12-31</strong>
  I read a bit more about GraphQL Instrumentation <a href="https://github.com/graphql-java/graphql-java-page/blob/master/content/documentation/v10/instrumentation.md" rel="nofollow noreferrer">here</a> and checked the <a href="https://github.com/graphql-java-kickstart/graphql-spring-boot/blob/master/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/metrics/MetricsInstrumentation.java" rel="nofollow noreferrer">MetricsInstrumentation</a> implementation in graphql-spring-boot repo, the I have an idea of extending the MetricsInstrumentation class by adding error metrics there.</p>
  
  <p><strong>2020-01-02</strong>
  I tried to ingest my CustomMetricsInstrumentation class, but with no luck. There is internal AutoConfiguration wiring, which I cannot insert my auto configuration in the middle.</p>