Docs
Launch GraphOS Studio

Metrics and logging

How to monitor Apollo Server's performance


Sending metrics from to requires an enterprise plan. If your organization doesn't currently have an , you can test out this functionality by signing up for a free Enterprise trial.

integrates seamlessly with Apollo GraphOS to help you monitor the execution of your . additionally provides configurable mechanisms for logging each phase of a GraphQL operation.

Using Federation? Check out the documentation for federated tracing.

Sending metrics to GraphOS

Apollo GraphOS provides an integrated hub for all of your performance data, which you can view in Apollo Studio. Studio aggregates and displays information for your schema, queries, requests, and errors. You can also configure alerts that support Slack and Datadog integrations.

Connecting to GraphOS

To connect to , first obtain a graph API key. To provide this key to , assign it to the APOLLO_KEY environment variable in your server's environment.

Then associate your server instance with a particular 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).

# 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, 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 requests they send to :

IdentifierHeader Name (default)Example Value
Client nameapollographql-client-nameiOS Native
Client versionapollographql-client-version1.0.1

Each of these 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 (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 , you can set default values for client name and version in the ApolloClient constructor. All requests to 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:

import { ApolloServer } from '@apollo/server';
import { ApolloServerPluginUsageReporting } from '@apollo/server/plugin/usageReporting';
const server = new ApolloServer({
typeDefs,
resolvers,
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',
};
}
},
}),
],
});
const { url } = await startStandaloneServer(server);
console.log(`🚀 Server listening at: ${url}`);
import { ApolloServer } from '@apollo/server';
import { ApolloServerPluginUsageReporting } from '@apollo/server/plugin/usageReporting';
const server = new ApolloServer({
typeDefs,
resolvers,
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',
};
}
},
}),
],
});
const { url } = await startStandaloneServer(server);
console.log(`🚀 Server listening at: ${url}`);

Logging

You can set up fine-grained logging in by defining a custom plugin. plugins enable you to perform actions in response to individual phases of the request lifecycle, such as whenever a GraphQL request is received from a client.

The example below defines a plugin that responds to three different 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.

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,
plugins: [myPlugin],
});
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,
plugins: [myPlugin],
});

OpenTelemetry support

See OpenTelemetry in Apollo Server.

Previous
Heroku
Next
Health checks
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company