Metrics and logging
How to monitor Apollo Server's performance
Apollo Server integrates seamlessly with Apollo Studio to help you monitor the execution of your GraphQL operations. It also provides configurable mechanisms for logging each phase of a GraphQL operation.
Using Federation? Check out the documentation for federated tracing.
Sending metrics to Apollo Studio
Apollo Studio provides an integrated hub for all of your GraphQL performance data. It aggregates and displays information for your schema, queries, requests, and errors. You can also configure alerts that support Slack and Datadog integrations.
Connecting to Apollo Studio
To connect Apollo Server to Apollo Studio, first obtain a graph API key. To provide this key to Apollo Server, assign it to the APOLLO_KEY
environment variable in your server's environment.
Then associate your server instance with a particular graph ID and graph variant by setting the APOLLO_GRAPH_REF
environment variable.
You can set environment variable values on the command line as seen below, or with the dotenv
npm package (or similar).
1# Replace the example values below with values specific to your use case.
2# This example associates your server with the `my-variant` variant of
3# the `my-graph` graph.
4$ APOLLO_KEY=YOUR_API_KEY APOLLO_GRAPH_REF=my-graph@my-variant \
5 node start-server.js
Communicating with Studio
By default, Apollo Server aggregates your traces and sends them in batches to Studio every minute. This behavior is highly configurable, and you can change the parameters in the Usage Reporting plugin's configuration.
Identifying distinct clients
Apollo Studio's client awareness feature enables you to view metrics for distinct versions of your clients. To enable this, your clients need to include some or all of the following identifying information in the headers of GraphQL requests they send to Apollo Server:
Identifier | Header Name (default) | Example Value |
---|---|---|
Client name | apollographql-client-name | iOS Native |
Client version | apollographql-client-version | 1.0.1 |
Each of these fields can have any string value that's useful for your application. To simplify the browsing and sorting of your client data in Studio, a three-part version number (such as 1.0.1
) is recommended for client versions.
Client version is not tied to your current version of Apollo Client (or any other client library). You define this value and are responsible for updating it whenever meaningful changes are made to your client.
Setting client awareness headers in Apollo Client
If you're using Apollo Client, you can set default values for client name and
version in the ApolloClient
constructor. All requests to Apollo Server will automatically include these values in the appropriate headers.
Using custom headers
For more advanced cases, or to use headers other than the default headers, pass a generateClientInfo
function into the usage reporting plugin:
1const { ApolloServer } = require("apollo-server");
2const {
3 ApolloServerPluginUsageReporting,
4 ApolloServerPluginLandingPageLocalDefault,
5} = require('apollo-server-core');
6
7const server = new ApolloServer({
8 typeDefs,
9 resolvers,
10 csrfPrevention: true,
11 cache: "bounded",
12 plugins: [
13 ApolloServerPluginUsageReporting({
14 generateClientInfo: ({
15 request
16 }) => {
17 const headers = request.http && request.http.headers;
18 if(headers) {
19 return {
20 clientName: headers["apollographql-client-name"],
21 clientVersion: headers["apollographql-client-version"],
22 };
23 } else {
24 return {
25 clientName: "Unknown Client",
26 clientVersion: "Unversioned",
27 };
28 }
29 },
30 }),
31 ApolloServerPluginLandingPageLocalDefault({ embed: true }),
32 ],
33});
34
35server.listen().then(({ url }) => {
36 console.log(`🚀 Server ready at ${url}`);
37});
Logging
You can set up fine-grained operation logging in Apollo Server by defining a custom plugin. Apollo Server plugins enable you to perform actions in response to individual phases of the GraphQL request lifecycle, such as whenever a GraphQL request is received from a client.
The example below defines a plugin that responds to three different operation events. As it shows, you provide an array of your defined plugins
to the ApolloServer
constructor.
For a list of available lifecycle events and their descriptions, see Plugins.
1const myPlugin = {
2 // Fires whenever a GraphQL request is received from a client.
3 async requestDidStart(requestContext) {
4 console.log('Request started! Query:\n' +
5 requestContext.request.query);
6
7 return {
8 // Fires whenever Apollo Server will parse a GraphQL
9 // request to create its associated document AST.
10 async parsingDidStart(requestContext) {
11 console.log('Parsing started!');
12 },
13
14 // Fires whenever Apollo Server will validate a
15 // request's document AST against your GraphQL schema.
16 async validationDidStart(requestContext) {
17 console.log('Validation started!');
18 },
19
20 }
21 },
22};
23
24const server = new ApolloServer({
25 typeDefs,
26 resolvers,
27 csrfPrevention: true,
28 cache: 'bounded',
29 plugins: [
30 myPlugin,
31 ApolloServerPluginLandingPageLocalDefault({ embed: true }),
32 ]
33});