API Reference: Usage reporting plugin
Apollo Server's built-in usage reporting plugin gathers data on how your clients use the operations and fields in your GraphQL schema. The plugin also handles pushing this usage data to GraphOS, as described in Metrics and logging.
This plugin is designed to be used in an Apollo Gateway or in a monolithic server; it is not designed to be used from a subgraph. In a supergraph running Apollo Federation, the Apollo Gateway or Apollo Router will send usage reports to Apollo's cloud. Subgraphs don't need to also send usage reports to Apollo's cloud; instead, they send it to the Router via inline traces and the Router combines execution information across all subgraphs and sends summarized reports to the cloud.
Default installation
📣 New in Apollo Server 4: error details are not included in traces by default. For more details, see Error Handling.
Apollo Server automatically installs and enables this plugin with default settings if you provide a graph API key and a graph ref to Apollo Server and your server is not a federated subgraph. You usually do this by setting the APOLLO_KEY
and APOLLO_GRAPH_REF
(or APOLLO_GRAPH_ID
and APOLLO_GRAPH_VARIANT
) environment variables. No other action is required.
If you don't provide an API key and graph ref, or if your server is a federated subgraph, this plugin is not automatically installed.
If you provide an API key but don't provide a graph ref, a warning is logged. You can disable the plugin to hide the warning.
If you provide an API key and graph ref but your server is a federated subgraph, a warning is logged. You can disable the plugin to hide the warning.
Custom installation
If you want to configure the usage reporting plugin, import it and pass it to your ApolloServer
constructor's plugins
array:
import { ApolloServer } from '@apollo/server';import { ApolloServerPluginUsageReporting } from '@apollo/server/plugin/usageReporting';const server = new ApolloServer({typeDefs,resolvers,plugins: [ApolloServerPluginUsageReporting({fieldLevelInstrumentation: 0.5,}),],});
import { ApolloServer } from '@apollo/server';import { ApolloServerPluginUsageReporting } from '@apollo/server/plugin/usageReporting';const server = new ApolloServer({typeDefs,resolvers,plugins: [ApolloServerPluginUsageReporting({fieldLevelInstrumentation: 0.5,}),],});
While you can install the usage reporting plugin in a server that is a federated subgraph, this is not recommended, and a warning will be logged.
Supported configuration options are listed below.
Options
Name / Type | Description |
---|---|
Configuring which data is sent to Apollo Studio | |
| Provide this object to configure which GraphQL variable values are included in trace data that's sent to Apollo Studio. Valid options are described in Valid The default value is |
| Provide this object to configure which request header names and values are included in trace data that's sent to Apollo Studio. Valid options are described in Valid The default value is |
| Provide this object to modify GraphQL operation errors before Apollo Server reports those errors to Apollo Studio. Valid options are described in Valid The default value is Note: If this |
| This option enables you to configure whether Apollo Server calculates detailed per-field statistics for each operation. It is used only for operations that reach execution without encountering an error (such as during parsing, validation, or resolving the operation name). It is not used if you provide an You can provide either a number or an an async function. If you provide a number, that number must be between For example, if you pass Providing a number
If you pass a function, the function is called once for each operation, and it's passed a corresponding If the function returns
(For more information about the difference between the "referencing operations" and "field executions" statistics, see the Studio Fields page documentation.) Returning If the function returns a positive number:
To determine the "estimation multiplier" that the function should return, take the reciprocal of the frequency with which the function returns a non-zero number for the associated operation. For example, if the function returns a non-zero number one out of every ten times for a particular operation, then the number it returns should be Note that returning The default value is a function that always returns |
| Specify this asynchronous function to configure which requests are included in usage reports sent to Apollo Studio. For example, you can omit requests that execute a particular operation or requests that include a particular HTTP header. Note that returning This function is called for each received request. It takes a If you don't want any usage reporting at all, don't use this option: instead, either avoid specifying an Apollo API key or explicitly disable the plugin. By default, all requests are included in usage reports. |
| Specify this function to provide Apollo Studio with client details for each processed request. Apollo Studio uses this information to segment metrics by client. This function is passed a By default, the plugin attempts to obtain these values from the incoming request's HTTP headers (specifically, |
| If you're using the |
| Statistics about operations that your server cannot execute are not reported under each document separately to Apollo Studio, but are grouped together as "parse failure", "validation failure", or "unknown operation name". By default, the usage reporting plugin does not include the full operation document in reported traces, because it is challenging to strip potential private information (like string constants) from invalid operations. If you'd like the usage reporting plugin to send the full operation document and operation name so you can view it in Apollo Studio's trace view, set this to true. |
Configuring communication protocol | |
| If This option is useful for stateless environments like Amazon Lambda where processes terminate after handling a small number of requests. Note that "immediately" does not mean synchronously with completing the response, but rather "very soon", such as after a |
| Specifies which Fetch API function implementation to use when sending usage reports. |
| The interval at which Apollo Server should send batched trace reports to Studio, in milliseconds. Regardless of this value, Apollo Server sends a trace report whenever the size of a pending batch exceeds the value of |
| Apollo Server sends a trace report whenever the size of a pending batched trace report exceeds this value (in bytes), regardless of its standard reporting interval. Note that this is a rough limit that includes the size of serialized traces and signatures. It ignores the size of the report's header and some other top-level bytes. The default value is 4MB ( |
| The maximum number of times Apollo Server should attempt to report each trace report, performing exponential backoff between attempts. The default value is |
| The minimum amount of backoff (in milliseconds) Apollo Server should perform before retrying a failed trace report. The default value is |
| Timeout for each individual attempt to send a report to Apollo. (This is for each HTTP POST, not for all potential retries.) The default value is |
| If you provide this object, the plugin sends it all log messages related to Apollo Studio communication, instead of sending them to the default logger. The object must implement all methods of the |
| If you provide this function, the plugin calls it whenever it encounters an error while reporting usage data. The details of the error are passed to the function. By default, the plugin logs these errors to its specified |
Internal and non-recommended options | |
| The URL base that the plugin sends reports to (not including the path). This option only needs to be set for testing and Apollo-internal uses. |
| If set, prints all reports as JSON when they are sent. (Note that for technical reasons, traces embedded in a report are printed separately when they are added to a report.) |
| Specify this function to create a signature for a query. This option is not recommended, because Apollo's servers make assumptions about how the signature relates to the operation you executed. |
Valid sendVariableValues
object signatures
Object | Description |
---|---|
{ none: true } | If you provide this object, no GraphQL variable values are sent to Apollo Studio. This is the default behavior. |
{ all: true } | If you provide this object, all GraphQL variable values are sent to Apollo Studio. |
{ onlyNames: ["apple", "orange"]} | If you provide an object with this structure, only values of the GraphQL variables with names that appear in the array are sent to Apollo Studio. To filter individual fields of a variable that contains an input type, use the transform function below instead. Case-sensitive. |
{ exceptNames: ["apple", "orange"]} | If you provide an object with this structure, all GraphQL variable values except values of the variables with names that appear in the array are sent to Apollo Studio. To filter individual fields of a variable that contains an input type, use the transform function below instead. Case-sensitive. |
{ transform: ({ variables, operationString)} => { ... } } | The value of For security reasons, if an error occurs in the |
Valid sendHeaders
object signatures
Object | Description |
---|---|
{ none: true } | If you provide this object, no request header names or values are sent to Apollo Studio. This is the default behavior. |
{ all: true } | If you provide this object, all GraphQL header names and values are sent to Apollo Studio, except for the protected headers listed below. |
{ onlyNames: ["apple", "orange"]} | If you provide an object with this structure, only names and values of the request headers with names that appear in the array are sent to Apollo Studio. Case-insensitive. |
{ exceptNames: ["apple", "orange"]} | If you provide an object with this structure, all GraphQL header values except values of headers with names that appear in the array are sent to Apollo Studio. Case-insensitive. |
Note: Regardless of your configuration, Apollo Server never sends the values of the following headers to Apollo Studio:
authorization
cookie
set-cookie
Valid sendErrors
object signatures
Object | Description |
---|---|
| If you provide this object, error messages are masked and extensions omitted in the traces sent to Apollo Studio. This is the default behavior. |
| If you provide this object, all error messages and extensions are included in the traces sent to Apollo Studio. |
| The value of The only properties of the reported error you can modify are its |
Disabling the plugin
If you don't want to install the usage reporting plugin and you are providing an API key to Apollo Server for other purposes, you can disable usage reporting by installing the ApolloServerPluginUsageReportingDisabled
plugin, like so:
import { ApolloServer } from '@apollo/server';import { ApolloServerPluginUsageReportingDisabled } from '@apollo/server/plugin/disabled';const server = new ApolloServer({typeDefs,resolvers,plugins: [ApolloServerPluginUsageReportingDisabled()],});
import { ApolloServer } from '@apollo/server';import { ApolloServerPluginUsageReportingDisabled } from '@apollo/server/plugin/disabled';const server = new ApolloServer({typeDefs,resolvers,plugins: [ApolloServerPluginUsageReportingDisabled()],});
This also disables the warning log if you provide an API key but do not provide a graph ref, or if you provide an API key and graph ref and your server is a federated subgraph.