npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hearme-app/apollo-server-plugin

v0.0.1

Published

Apollo Server plugin that adds New Relic Node.js agent instrumentation.

Downloads

9

Readme

NewRelic GraphQL Plugin

This is a fork of New Relic's official Apollo Server plugin.

Why?

The current state of the above-mentioned official plugin does not work with modern Typescript-powered NestJS + GraphQL projects. Because of some breaking changes regarding at least type definitions in the latest version of the apollo-server library, the project couldn't be used as a GraphQLModule plugin.

Apart from this, if you use typescript without the esModuleInterop config turned on, the default export from the official plugin could not be used.

This Fork was created to tackle these issues and make it usable with NestJs/GraphQL.

Usage With NestJS GraphQL.

This plugin expects the Node.js agent newrelic npm package has already been installed in your application.

Assuming you have your newrelic interceptors set up, you can install the library with:

npm install @hearme-app/apollo-server-plugin

Then use it with the GraphQLModule like so:

// app.module.ts
import ApolloServerPluginCreator from '@hearme-app/apollo-server-plugin';

// imported from `nestjs/graphql`
GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  plugins: [
    ApolloServerPluginCreator({
      captureScalars: true, 
    }),
  ],
}),
/**
 * Rest of your setup
 * /

Getting started

The @hearme-app/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:

import ApolloServerPluginCreator from '@hearme-app/apollo-server-plugin';

// imported from `nestjs/graphql`
GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  plugins: [
    ApolloServerPluginCreator(),
  ],
}),
/**
 * Rest of your setup
 * /

Usage

The New Relic plugin is known to work with the following Apollo Server modules:

  • apollo-server (>= 3.6)
  • 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.

import ApolloServerPluginCreator from '@hearme-app/apollo-server-plugin';

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  plugins: [
    ApolloServerPluginLandingPageLocalDefault(),
    ApolloServerPluginCreator(),
  ],
}),

Configuration

Configuration may be passed into the default exported function to override specific values. To override configuration, invoke the exported function prior to passing to GraphqlModule. The configuration object and all properties are optional.

import ApolloServerPluginCreator from '@hearme-app/apollo-server-plugin';

const plugin = ApolloServerPluginCreator({
  captureScalars: true,
  captureHealthCheckQueries: true,
  captureIntrospectionQueries: true,
  captureServiceDefinitionQueries: true,
})
  • [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.

Transactions

Transaction Documentation

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.

Metrics

Metrics Documentation

Two new metrics have been introduced to understand the behavior of your GraphQL operations within and across transactions.

Operation Metrics

/GraphQL/operation/ApolloServer/[operation-type]/[operation-name]/[deepest-unique-path]

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.

FROM Metric SELECT average(newrelic.timeslice.value) * 1000 WHERE appName = '[YOUR APP NAME]' WITH METRIC_FORMAT 'GraphQL/operation/ApolloServer/{operation}' FACET operation LIMIT 10

Field Resolve Metrics

/GraphQL/resolve/ApolloServer/[field-name]

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) * 1000 WHERE 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

Segments and Spans

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

/GraphQL/operation/ApolloServer/[operation-type]/[operation-name]/[deepest-unique-path]

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.

For more information on collected attributes, see the segments and spans documentation

Field Resolve Segments/Spans

/GraphQL/resolve/ApolloServer/[path]

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.

For more information on collected attributes, including enabling args capture, see the segments and spans documentation

Errors

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, Privacy, CLA, License, etc.

As mentioned above, this a fork of newrelic-node-apollo-server-plugin. Please refer to the original repo for more information.