Skip to content
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

Capturing Subsegment doesn't work with async AWS Lambdas #58

Closed
khaledosman opened this issue Oct 17, 2018 · 2 comments
Closed

Capturing Subsegment doesn't work with async AWS Lambdas #58

khaledosman opened this issue Oct 17, 2018 · 2 comments

Comments

@khaledosman
Copy link

I'm trying to capture the request and response for every API request and save it as metadata to a segment.. since attaching metadata to the main segment doesn't work I created a custom subsegment as follows:

import { APIGatewayEvent, Callback, Context, Handler } from 'aws-lambda'
import { getUserLocation } from './api/get-user-location'
import { handleFindNearbyUsers } from './api/handle-find-nearby-users'
import { handleSendSMS } from './api/handle-send-sms'
import { lambdaResponse } from './helpers/lambda-response'

const awsXRay = require('aws-xray-sdk')
awsXRay.captureAWS(require('aws-sdk'))
awsXRay.captureHTTPsGlobal(require('http'))
awsXRay.captureHTTPsGlobal(require('https'))

export const handle: Handler = async (event: APIGatewayEvent, context: Context) => {
  // const key = event.headers['x-api-key']
  const path: string = event.resource
  let response
  if (path === '/user-location') {
    response = await getUserLocation(event)
  } else if (path === '/send-sms') {
    response = await handleSendSMS(event)
  } else if (path === '/nearby-users') {
    response = await handleFindNearbyUsers(event)
  } else {
    response = lambdaResponse(new Error('Path not found'), 404)
  }

  console.log('request', event)
  console.log('response', response)

  capture(event, response)

  return response

}

function capture (event, response) {
  awsXRay.captureFunc('Metadata', (subsegment) => {
    if (subsegment) {
      subsegment.http = new awsXRay.middleware.IncomingRequestData({
        method: event.httpMethod,
        url: event.path,
        connection: {
          secure: event.headers['CloudFront-Forwarded-Proto'] === 'https'
        },
        headers: {
          'x-forwarded-for': event.headers['X-Forwarded-For'],
          'user-agent': event.headers['User-Agent'],
          'host': event.headers.Host
        }
      })
      subsegment.addMetadata('request', event)
      subsegment.addMetadata('response', response)
      // subsegment.close()
    }
  })
}

I tried both captureFunc and captureAsyncFunc, However I don't see the subsegment sent with the trace. What's the correct way to do this?

@haotianw465
Copy link
Contributor

This are some known issues for keep tracing context for async/await: #60 and we are still in the progress of researching and finding solutions. Do you have any error logs emitted by the SDK? If you turn on APIGateway tracing then you get http information for free: https://aws.amazon.com/blogs/aws/apigateway-xray/.

@awssandra
Copy link
Contributor

Closing in favor of #60

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

No branches or pull requests

3 participants