Skip to content

[Feature] AWS XRay Support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

martinjlowm
Copy link

@martinjlowm martinjlowm commented Oct 13, 2018

Depends on #1

Adds AWS XRay support by pushing a handler segment prior to invoking the target handler.

@martinjlowm martinjlowm changed the title Feature/xray support [Feature] AWS XRay Support Oct 13, 2018
@martinjlowm
Copy link
Author

The implementation can be tested using the following piece of code as a regular lambda invocation (lambda.invoke({ ...params }).promise()) to set tracing headers for the targeted proxy handler.

const AWSXRay = require('aws-xray-sdk');
const fetch = require('node-fetch');

const promisedInvoke = async (params) => {
  const headers = {
    'Content-Type': 'application/json',
  };

  let subsegment;
  if (process.env.ENABLE_TRACING) {
    const ns = AWSXRay.getNamespace();
    if (ns.active) {
      const parent = AWSXRay.resolveSegment();
      subsegment = parent.addNewSubsegment(params.FunctionName);
      const traceId = parent.segment ? parent.segment.trace_id : parent.trace_id;
      headers['X-Amzn-Trace-Id'] = 'Root=' + traceId + ';Parent=' + subsegment.id +
        ';Sampled=' + (subsegment.segment.notTraced ? '0' : '1');
    }
  }

  const fetchResult = await fetch(params.FunctionName, {
    method: 'POST',
    headers,
    body: JSON.stringify(params),
  });

  if (subsegment) {
    subsegment.close();
  }     

  if (fetchResult.status === 200) {
    return await fetchResult.json();
  } else {
    throw new Error(fetchResult.statusText);
  }
};

const lambdaInstance = {
  invoke: params => {
    return { promise: () => promisedInvoke(params) };
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant