serverless-plugin-tracing icon indicating copy to clipboard operation
serverless-plugin-tracing copied to clipboard

Node.js User: 'aws-xray-sdk' package should be installed to use this plugin

Open geoseong opened this issue 5 years ago • 0 comments

Good Day.

I just noticed that installing npm install --save-dev serverless-plugin-tracing is not enough to use this plugin.

I have struggled for serveral days to find the way how to run my application without error.

so I just found the cause that aws-xray-sdk package is not installed in my project.

after I installed aws-xray-sdk, my project started working well and I can see the Service map in AWS X-Ray screen.

Am I only person who have this problem?

I don't know about details but just let you guys know.

Here's my serverless.yml and handler.js. and I am using next.js and node.js 8 with Koa.

serverless.yml

service: mom # NOTE: update this with your service name

provider:
  name: aws
  runtime: nodejs8.10
  stage: ${self:custom.stageName}
  region: ap-northeast-2
  tracing: true # enable tracing
  environment:
    NODE_PATH: "./:/opt/node_modules"
    stageName: ${self:custom.stageName}
  iamRoleStatements:
    - Effect: "Allow" # xray permissions (required)
      Action:
        - "xray:PutTraceSegments"
        - "xray:PutTelemetryRecords"
      Resource:
        - "*"

custom:
  stageName: NextJS

package:
  individually: true
  exclude:
    - .cache
    - .serverless
    - .vscode
    - serverless.yml

functions:
  frontendAdmin:
    handler: handler.index
    name: ideasam-mom-frontendAdmin # optional, Deployed Lambda name
    description: MOM - match admin page  # optional, Description to publish to AWS
    # environment: ${self:custom.environment}
    memorySize: 512
    package:
      exclude:
        - frontend/test
        - frontend/"**.test.js"
        - frontend/"__snapshots__/"
      include:
        - frontend/.next/**
    events:
      # - http:
      #     path: /admin
      #     method: get
      #     cors: true
      - http:
          path: /admin/{addr+}
          method: get
          cors: true
    layers:
      - arn:aws:lambda:ap-northeast-2:***:layer:***:1
...

plugins:
  - serverless-offline
  - serverless-plugin-tracing

handler.js

'use strict'

const debugging = process.env.DEBUG || process.env.DEBUG === 'true'
let layerPath = ''
if (debugging) {
  layerPath = require('./layerPath').layerPath(__dirname.substring(0, __dirname.lastIndexOf('/')))
}

const serverless = require(layerPath + 'serverless-http')
const server = require('./server_koa')

module.exports.index = async (event, context) => {
  try {
    /**
    * @author 박태성
    * @name AWS_X-Ray
    * @description AWS X-Ray 적용을 위한 코드. 디버깅 모드에서는 지원되지 않음. 
      aws-xray-sdk를 추가로 인스톨해야 Prod에서 사용 가능.
    */
    if (!debugging) {
      const awsXRay = require('aws-xray-sdk')
      const awsSdk = awsXRay.captureAWS(require('aws-sdk'))
    }
    const appServer = await server.createServer(event)
    return await serverless(appServer)(event, context)
  } catch (e) {
    console.log('error in handler.js', e)
    return {
      statusCode: 500,
      headers: {
        'content-type': 'text/html',
      },
      body: `
        <html>
          <body style="margin: 0px; padding: 1rem; background-color: #ff0081;">
            <div style="color: #fff;">${e.message}</div>
          </body>
        </html>
      `,
    }
  }
}

geoseong avatar Mar 05 '19 08:03 geoseong