How to Monitor Express GraphQL Apps in New Relic
By Rajesh krishnakumar — “Go above and beyond!
Express graphql Server, an open-source GraphQL server that helps you connect a GraphQL schema to an HTTP server in Express node js application.
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, minimizes the amount of data transferred over the network and improves performance, makes it easier to evolve APIs over time, and enables powerful developer tools. With GraphQL, you can fetch and retrieve data in a single endpoint.
As we read above the GraphQL helps us to query/mutate the data using a single endpoint. This has a downside too, We can’t measure the performance of individual GraphQL operations. Such as calling external services, DB operations, etc. As you see below fig. 1 all the graphql operations mounted under a single endpoint.
To address this problem we will use New relic Node.js agent API -> setTransactionName
Using this api we will set the transaction name. So we can segregate the graphql operations.
As you see in the below Fig. 2 now all the graphql operations are segregated.
There are two ways you can implement this.
In the above code snippet, you can see we have added a function processRequest in that function we are setting the transaction name.
newrelic.setTransactionName(info.fieldName);
Here we are calling the new relic node agent api to set the transaction name.
info.fieldName -> Get individual graphql operation name
info contains the query AST and more execution information
fieldName: each field in your GraphQL schema needs to be backed by a resolver. The fieldName contains the name for the field that belongs to the current resolver.
c) Integrate this middleware to your graphql server so the request goto the middleware first then it goes to the resolver.
Note: Your graphql server initialization may look different.
2. The second approach updates the transaction name in the resolver of each graphql operation.
https://gist.github.com/rajeshkrishnakumar/732017a7b63139240dc0302c3ffc6aae#file-resolver-jsAs you see above code snippet we need to import a new relic package and call the node js agent api
Note -> Using the Middleware approach you don’t need to call a new relic agent api in all resolver
We are done. After doing any one of these approaches, boot your app and give some load to various individual operations in your app and check the new relic dashboard you will be able to view the individual graphql operation under the transaction section as shown in Fig. 2.
This was a little guide to show you how you can instrument your express graphql apps and drill down to arrest the bottleneck to improve your app performance. If you are using the apollo graphQL server you can use the below mention plugin and enable graphQL monitoring
[embed]https://github.com/newrelic/newrelic-node-apollo-server-plugin[/embed]New relic node api documentation https://docs.newrelic.com/docs/agents/nodejs-agent/api-guides/nodejs-agent-api/
Author
Rajesh Krishnakumar
Reviewer
Muthukumar K
Editor
Mridula Saravanan
We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
Leave a Reply