In a multi-threaded graphql server it is imperative to propagate a request correlation id to all threads invoked. This will ensure all threads can log the correlation id which will then provide insight into the code flow and ultimately which request triggered that section of code. If you log the original graphql query and variables with the correlation ID, you can now link any log to the original graphql request. Very useful for debugging then exceptions :)
We first create an Instrumentation class and override the beginExecution method. Inside this method we assign a unique correlation id to the loggers mapped diagnostic context (MDC). As our graphql service is an edge node, no upstream service will provide a correlation id to graphql. We therefor can use the graphql Execution ID. This a UUID generated by the graphql framework for each request. We then return a whenCompleted callback to clear the MDC. It’s important to do this as we want to ensure the MDC does not contain any values from the previous request. You can set an additional MDC.clear on a listener’s onComplete() method to ensure the original NIO thread has been cleared. As an alternative, the listener onRequest() method can also be used to set the original MDC on the tomcat NIO thread.
We then customize the logback console appender to print the %X{correlation_id} with every log line.
This will allow us to paste a correlation id into our log viewer tool and get every log line. E.g. Splunk, Sumo-logic & Logentries.
But wait! At this stage, the MDC is only tied to the NIO tomcat thread.
How do we propagate it to all threads such as dataloaders and async completable futures?
To solve this, we create a Correlation ID propagation executor service. This class will get the MDC correlation ID from the current thread and set it into the MDC map in the new executing thread in the target thread pool. Right before the thread is ready to accept another unit of work, we clear the MDC map. This can then be chained throughout all threads and will propagate and clear the MDC as required.
Now you will have a consistent correlation_id printed in all your application logs. Happy monitoring and debugging :)
See you in the next video where we introduce spring security to protect our graphql resolvers
Thanks,
Philip
Enterprise integration pattern, correlation identifier: https://www.enterpriseintegrationpatt...
Logback MDC: http://logback.qos.ch/manual/mdc.html
GraphQL Instrumentation: https://www.graphql-java.com/document...
Spring Boot GraphQL Java: https://github.com/graphql-java-kicks...
Good article on graphql paypal instrumentation: / graphql-instrumenting-your-api-and-unlocki...
Apollo tracing: https://github.com/apollographql/apol...
Thumbnail tracing picture: https://slonka.net/node-diy-distribut...