// index.jsconst plugin = require('@newrelic/apollo-server-plugin')
// imported from supported moduleconst server = newApolloServer({
typeDefs,
resolvers,
plugins: [plugin]
})
Getting started
The @newrelic/apollo-server-plugin exports a createPlugin function that does accept limited configuration. Since Apollo Server will invoke any function passed to plugins, invoking this yourself is not required unless you plan to override specific configuration.
To use the plugin without specific configuration, you can pass the function in directly similar to:
// index.jsconst plugin = require('@newrelic/apollo-server-plugin')
// imported from supported moduleconst server = newApolloServer({
typeDefs,
resolvers,
plugins: [plugin]
})
Note: If you are using Typescript, you must pass in the appropriate ApolloServerPlugin type.
The New Relic plugin is known to work with the following Apollo Server modules:
@apollo/server
@apollo/gateway
@apollo/subgraph
apollo-server (>= 2.14)
apollo-server-express
apollo-server-hapi
apollo-server-koa
apollo-server-fastify
apollo-server-lambda
Other plugins may work, depending on their underlying implementation, but have not been verified.
Interaction with other Apollo Plugins
Transaction and segment/span timings may be affected by other plugins used in the Apollo Server setup. In order to get more accurate resolver timings, it is recommended to add the New Relic plugin last.
Configuration may be passed into the createPlugin function to override specific values. To override configuration, invoke the createPlugin function prior to passing to Apollo Server. The configuration object and all properties are optional.
[captureScalars = false] Enable capture of timing of fields resolved with the
GraphQLScalarType return type. This may be desired when performing time intensive
calculations to return a scalar value. This is not recommended for queries that return
a large number of pre-calculated scalar fields.
NOTE: query/mutation resolvers will always be captured even if returning a scalar type.
[captureIntrospectionQueries = false] Enable capture of timings for an IntrospectionQuery.
[captureServiceDefinitionQueries = false] Enable capture of timings for a Service Definition query received from an Apollo Federated Gateway Server.
[captureHealthCheckQueries = false] Enable capture of timings for a Health Check query received from an Apollo Federated Gateway Server.
[captureFieldMetrics = false] Enable capture of metrics for every field and resolver argument seen for an Apollo query. This is intended to be used to check for any unused fields in your graphql schema.
There are two configuration options for providing callbacks that can be used to define custom attributes. As their names suggest, customResolverAttributes will put custom attributes on the resolver segment and customOperationAttributes will use the operation segment.
The customResolverAttributes callback gets passed a resolverContext object corresponding to the attributes given to a resolver in Apollo server. The customOperationAttributes callback gets passed a requestContext argument. These arguments can be used to imbue more details into custom attributes. Both callbacks must return an object that will be used to define custom attribute key-value pairs. For example,
The New Relic plugin will work with an Apollo Federated Server out of the box. Just pass the plugin to the Federated Gateway Apollo Server. With the federated server you will get:
Distributed Tracing support
Transaction naming
Operation naming
Metrics
Resolver spans are not supported for the Federated Gateway Server. We strongly recommend adding the plugin to your sub-graph servers which will generate all the data you get with the Federated Server as well as resolver spans.
Transactions are captured as web transactions, associated with the underlying framework (Express, Koa, etc.), and named based on the GraphQL operations executed.
Here's an example query and how that may be represented in NR One.
query{
libraries {
books {
title
author {
name
}}}}
post /query/<anonymous>/libraries.books
For more information on how transactions are named, including how query errors may impact naming, please see the transaction documentation.
Operation metrics are very similar to how transaction names are constructed including the operation type, operation name and deepest unique path. These metrics represent the durations of the individual queries or mutations and can be used to compare outside of the context of individual transactions which may have multiple queries.
If you would like to have a list of the top 10 slowest operations, the following query can be used to pull the data on demand or as a part of a dashboard. The 'Bar' chart type is a recommended visualization for this query.
Resolve metrics capture the duration spent resolving a particular piece of requested GraphQL data. These can be useful to find specific resolvers that may contribute to slowing down incoming queries.
If you would like to have a list of the top 10 slowest resolves, the following query can be used to pull the data on demand or as a part of a dashboard. The 'Bar' chart type is a recommended visualization for this query.
FROM Metric SELECT average(newrelic.timeslice.value) *1000WHERE appName ='[YOUR APP NAME]'WITH METRIC_FORMAT 'GraphQL/resolve/ApolloServer/{field}'
FACET field LIMIT 10
For more information on metrics and some recommended visualizations, please see the metrics documentation.
Segments and spans (when Distributed Tracing enabled) are captured for GraphQL operations, field resolution and additional work (when instrumented) that occurs as a part of field resolution such as making a query to a database.
Operation segments/spans include the operation type, operation name and deepest unique path. These represent the individual duration and attributes of a specific invocation within a transaction or trace.
The operation type and operation name are captured as attributes on a segment or span as well as the query with obfuscated arguments.
Resolve segments/spans leverage the resolution path of the individual field to best differentiate within a given trace or transaction. For example, libraries.books might be used instead of just books. These represent the individual duration and attributes of a specific field being resolved as a part of the GraphQL operation.
The field name, return type, parent type, path and arguments (disabled by default) are all captured as attributes.
The agent will notice GraphQL errors that get sent back to the client.
Depend on where the error was thrown, these will be associated with either the operation span or the specific field resolve span (Distribute Tracing enabled) to enable further debugging of issues.
Testing
The module includes a suite of unit and functional tests which should be used to
verify that your changes don't break existing functionality.
All tests are stored in tests/ and are written using
Tap with the extension .tap.js.
To run the full suite, run: npm test.
Individual test scripts include:
npm run unit
npm run integration
npm run versioned
Support
Should you need assistance with New Relic products, you are in good hands with several support diagnostic tools and support channels.
New Relic Developer: Resources for building a custom observability applications
New Relic University: A range of online training for New Relic users of every level
Privacy
At New Relic we take your privacy and the security of your information seriously, and are committed to protecting your information. We must emphasize the importance of not sharing personal data in public forums, and ask all users to scrub logs and diagnostic information for sensitive information, whether personal, proprietary, or otherwise.
We define “Personal Data” as any information relating to an identified or identifiable individual, including, for example, your name, phone number, post code or zip code, Device ID, IP address, and email address.
We encourage your contributions to improve the New Relic Apollo Server plugin! Keep in mind that when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project.
If you have any questions, or to execute our corporate CLA (which is required if your contribution is on behalf of a company), drop us an email at opensource@newrelic.com.
If you would like to contribute to this project, review these guidelines.
To all contributors, we thank you! Without your contribution, this project would not be what it is today. We also host a community project page dedicated to the New Relic Apollo Server plugin.
A note about vulnerabilities
As noted in our security policy, New Relic is committed to the privacy and security of our customers and their data. We believe that providing coordinated disclosure by security researchers and engaging with the security community are important means to achieve our security goals.
If you believe you have found a security vulnerability in this project or any of New Relic's products or websites, we welcome and greatly appreciate you reporting it to New Relic through HackerOne.
License
The New Relic Apollo Server plugin is licensed under the Apache 2.0 License.
The New Relic Apollo Server plugin also uses source code from third-party libraries. You can find full details on which libraries are used and the terms under which they are licensed in the third-party notices document.
FAQs
Apollo Server plugin that adds New Relic Node.js agent instrumentation.
The npm package @newrelic/apollo-server-plugin receives a total of 43,070 weekly downloads. As such, @newrelic/apollo-server-plugin popularity was classified as popular.
We found that @newrelic/apollo-server-plugin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 11 Dec 2023
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.