Error tracking with Apollo Engine

We've already looked at how to inspect single operations using Apollo Engine. Under the Clients tab, you will find a separate view that covers all client types and their requests:

In this tab, you can directly see the percentage of errors that happened during each operation. In the currentUser query, there were 37.14% errors out of the total currentUser requests.

If you take a closer look at the left-hand side of the image, you will see that it says Unidentified clients. Since version 2.2.3 of Apollo Server, client awareness is supported. It allows you to identify the client and track how consumers use your API. Apollo automatically extracts an extensions field inside each GraphQL operation, which can hold a name and version. Both fields—Name and Version—are then directly transferred to Apollo Engine. We can filter by these fields in Apollo Engine. We will have a look at how to implement this in our back end next.

In this example, we'll use HTTP header fields to track the client type. There will be two header fields: apollo-client-name and apollo-client-version. We'll use these to set custom values to filter requests later in the Clients page. Open the index.js file from the graphql folder. Add the following function to the engine property of the ApolloServer initialization:

engine: {
apiKey: ENGINE_KEY,
generateClientInfo: ({
request
}) => {
const headers = request.http.headers;
const clientName = headers.get('apollo-client-name');
const clientVersion = headers.get('apollo-client-version');

if(clientName && clientVersion) {
return {
clientName,
clientVersion
};
} else {
return {
clientName: "Unknown Client",
clientVersion: "Unversioned",
};
}
},
},

The generateClientInfo function is executed with every request. We extract the two fields from the header. If they exist, we return an object with the clientName and clientVersion properties that have the values from the headers. Otherwise, we return a static Unkown Client text.

To get both of our clients – the front end and back end – set up, we have to add these fields. Perform the following steps:

  1. Open the index.js file of the client's apollo folder file.
  2. Add a new InfoLink to the file to set the two new header fields:
const InfoLink = (operation, next) => {
operation.setContext(context => ({
...context,
headers: {
...context.headers,
'apollo-client-name': 'Apollo Frontend Client',
'apollo-client-version': '1'
},
}));

return next(operation);
};

Like AuthLink, this link will add the two new header fields next to the authorization header. It sets the version header to '1' and the name of the client to 'Apollo Frontend Client'. We will see both in Apollo Engine soon.

  1. Add InfoLink in front of AuthLink in the ApolloLink.from function.
  2. On the back end, we need to edit the apollo.js file in the ssr folder:
const InfoLink = (operation, next) => {
operation.setContext(context => ({
...context,
headers: {
...context.headers,
'apollo-client-name': 'Apollo Backend Client',
'apollo-client-version': '1'
},
}));

return next(operation);
};
  1. The link is almost the same as the one for the front end, except that we set another apollo-client-name header. Add it just before AuthLink in the ApolloLink.from function.

The client name differs between the front end and back end code so you can compare both clients inside Apollo Engine. If you execute some requests from the back end and front end, you can see the result of these changes directly in Apollo Engine. Here, you can see an example of how that result should look:

At the top of the screenshot, we see the number of requests the back end has made. In the middle, all the clients that we have no further information on are listed, while at the bottom, we can see all requests that have been made by the client-side code. Unknown clients might be external applications that are accessing your API.

When releasing a new version of your application, you can increase the version number of the client. The version number represents another comparable field.

We now know which clients have accessed our API from the information provided by Apollo Engine. Let's take a look at what Apollo Engine can tell us about errors.

When you visit the Error tab, you will be presented with a screen that looks like the following screenshot:

The first chart shows the number of errors over a timeline. Under the graph, you can see each error with a timestamp and the stack trace. You can follow the link to see the trace in detail, with the location of the error. If you paid for the Team plan, you can also set alerts when the number of errors increases or the latency time goes up. You can find these alerts under the Integrations tab.

Next, we'll see how to improve the performance of our GraphQL API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.36.203