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.
Sending metrics to Apollo Studio
Connecting to Apollo Studio
To connect Apollo Server to Apollo Studio, first APOLLO_KEY
environment variable in your server's environment.
Then associate your server instance with a particular graph ID and APOLLO_GRAPH_REF
environment variable.
You can set environment variable values on the command line as seen below, or with the dotenv
npm package
# Replace the example values below with values specific to your use case.# This example associates your server with the `my-variant` variant of# the `my-graph` graph.$ APOLLO_KEY=YOUR_API_KEY APOLLO_GRAPH_REF=my-graph@my-variant \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
Identifying distinct clients
Apollo Studio's
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
Using custom headers
For more advanced cases, or to use headers other than the default headers, pass a generateClientInfo
function into the
const { ApolloServer } = require("apollo-server");const {ApolloServerPluginUsageReporting,ApolloServerPluginLandingPageLocalDefault,} = require('apollo-server-core');const server = new ApolloServer({typeDefs,resolvers,csrfPrevention: true,cache: "bounded",plugins: [ApolloServerPluginUsageReporting({generateClientInfo: ({request}) => {const headers = request.http && request.http.headers;if(headers) {return {clientName: headers["apollographql-client-name"],clientVersion: headers["apollographql-client-version"],};} else {return {clientName: "Unknown Client",clientVersion: "Unversioned",};}},}),ApolloServerPluginLandingPageLocalDefault({ embed: true }),],});server.listen().then(({ url }) => {console.log(`🚀 Server ready at ${url}`);});
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
const myPlugin = {// Fires whenever a GraphQL request is received from a client.async requestDidStart(requestContext) {console.log('Request started! Query:\n' +requestContext.request.query);return {// Fires whenever Apollo Server will parse a GraphQL// request to create its associated document AST.async parsingDidStart(requestContext) {console.log('Parsing started!');},// Fires whenever Apollo Server will validate a// request's document AST against your GraphQL schema.async validationDidStart(requestContext) {console.log('Validation started!');},}},};const server = new ApolloServer({typeDefs,resolvers,csrfPrevention: true,cache: 'bounded',plugins: [myPlugin,ApolloServerPluginLandingPageLocalDefault({ embed: true }),]});