5.0.0 • Published 3 years ago

@tomkoufakis/graphql-resolvers-xray-tracing v5.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

GraphQL Middleware to add X-Ray tracing for resolvers

npm Greenkeeper badge Build Status Coverage Status

To enable X-Ray subsegment creation for GraphQL resolvers, add this package as a dependency of your project and use code like this:

const traceResolvers = require('@tomkoufakis/graphql-resolvers-xray-tracing');
const schema = makeExecutableSchema( ... );
traceResolvers(schema);

After enabling X-Ray tracing, you should see new subsegments in your X-Ray traces like this:

Image of X-Ray trace

Local Development

If you would like to run your GraphQL server without tracing the resolvers (such as during local development), you can use environment variables to conditionally wrap them. For example, the AWS Lambda runtime injects the AWS_LAMBDA_FUNCTION_NAME which you can use so that the resolvers are only traced when running on Lambda:

const traceResolvers = require('@tomkoufakis/graphql-resolvers-xray-tracing');
const schema = makeExecutableSchema( ... );
if (process.env.AWS_LAMBDA_FUNCTION_NAME) {
  traceResolvers(schema);
}

AWS Segment Size Limitation

AWS has a 64K upload limit when submitting segements to AWS see AWSXRay concepts segments.

If you try and submit more than this limit you will see the following aws error message" ERROR Segment too large to send: {<traceinformation...}"

One approach to remove this error, is to reduce the size of the batch upload (eg.. setStreamingThreshold(0) which will send each subsegment on close). See SDK - nodejs - setStreamingThreshold.

Runtime Options

To add the ability to enable/disable segment creation at runtime, pass in an options object like so:

const traceResolvers = require('@tomkoufakis/graphql-resolvers-xray-tracing');
const schema = makeExecutableSchema( ... );

const traceOptions = {
  enabled: true
};

traceResolvers(schema, traceOptions);

traceOptions, if declared at the module level, can then be used to programatically turn on/off segment creation.

Origin Story

Note: this is a fork of https://www.npmjs.com/package/@lifeomic/graphql-resolvers-xray-tracing