Code Monkey home page Code Monkey logo

serverless-plugin-tracing's Introduction

serverless-plugin-tracing

npm version CircleCI Coverage Status

Enables AWS X-Ray (https://aws.amazon.com/xray/) for the entire Serverless stack or individual functions.

Update: as of 2.0.0, the plugin uses Cloud Formation to update TracingConfig and no longer makes additional AWS SDK calls. No change to YAML contract: stays same as in 1.x. Tested with [email protected].

Note: this plugin is currently Beta.

Note: 1.x was tested to work well with [email protected]. Some older versions of serverless may not work due to outdated Javascript SDK that does not support TracingConfig.

npm install --save-dev serverless-plugin-tracing

Example serverless.yml:

service: my-great-service

provider:
  name: aws
  stage: test
  tracing: true # enable tracing
  iamRoleStatements:
    - Effect: "Allow" # xray permissions (required)
      Action:
        - "xray:PutTraceSegments"
        - "xray:PutTelemetryRecords"
      Resource:
        - "*"

plugins:
  - serverless-plugin-tracing

functions:
  mainFunction: # inherits tracing settings from "provider"
    handler: src/app/index.handler
  healthCheckFunction:
    tracing: false # overrides provider settings (opt out)

Output after serverless deploy:

Serverless: Tracing ENABLED for function
  "my-great-service-test-mainFunction"
Serverless: Tracing DISABLED for function
  "my-great-service-test-healthcheck"

Important: in addition to using the plugin, you need to enable capturing traces in the code as well:

const awsXRay = require('aws-xray-sdk');
const awsSdk = awsXRay.captureAWS(require('aws-sdk'));

The plugin only controls the checkbox that be viewed in AWS Console: go to AWS Lambda -> select a Lambda function -> Configuration tab -> Advanced settings -> "Enable active tracing". If tracing ends up being true for a function, the checkbox will be checked for that function.

serverless-plugin-tracing's People

Contributors

alex-murashkin avatar alexanderms avatar dougmoscrop avatar joeyvandijk avatar olecom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

serverless-plugin-tracing's Issues

Specifying resources without wild cards

Can you please give an example in the README of specifying resources for the iamRoleStatements clause without a wildcard? Our policies don't allow us to use wildcards, but when I specify the functions explicitly themselves, it is silently not working.

Thanks for your help.

QUESTION: Is the call to captureAWS via the aws-xray-sdk still required in v2.0?

I noticed that for v2.0, you added a comment at the top of the README which states this plugin now enables tracing via CloudFormation, instead of making calls to the AWS sdk.

However, a few lines later in the README, you state that we still need to include the following code:

const awsXRay = require('aws-xray-sdk');
const awsSdk = awsXRay.captureAWS(require('aws-sdk'));

Is that call to captureAWS still a requirement via code, or just a remnant of previous requirement before v2.0, and should be removed from the README?

Enabling on specific stages only

It would be handy if we could pass a stage variable into this plugin to be enabled only at that stage. Currently, I have to reference an external file in order to check the current stage and return true/false for the tracing property.

Fails to set Tracing when a custom function name is used

Nice plugin. There is a small issue that occurs when a custom name element is used with the serverless functions. In this scenario, Tracing is not set.

It should be a pretty easy fix to check for the 'name' element before calling toggleTracing. Something like:

this.toggleTracing((
service.functions[functionName].name ||
${service.service}-${stage}-${functionName}),
(service.functions[functionName].tracing === true)
|| (providerLevelTracingEnabled && service.functions[functionName].tracing !== false)
);

Thanks

Invoking locally leads to an error.

Error:

{
    "errorMessage": "Missing AWS Lambda trace data for X-Ray. Expected _X_AMZN_TRACE_ID to be set.",
    "errorType": "Error"
}

Invoked by running: sls invoke local -f activateTrigger --path trigger.json

Is there any way to bypass tracing all together if it is invoked locally?

Errors when deploying since today

Since today I'm receiving these errors when deploying an application with the tracing plugin enabled:

Serverless: Tracing ENABLED for function "func1"
Serverless: Tracing ENABLED for function "func2"
Serverless: Tracing ENABLED for function "func3"
Serverless: Tracing ENABLED for function "func4"
Serverless: Tracing ENABLED for function "func5"
 
  Serverless Error ---------------------------------------
 
  The function could not be updated due to a concurrent update operation.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless
 
  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           8.7.0
     Serverless Version:     1.23.0

Any ideas why this is happening? Maybe throttling would help?

P.S: I replaced the func names ;)

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

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>
      `,
    }
  }
}

Passing ids from one lambda to another?

Is there a simple way to get the Trace ID from one function and then pass it to a lambda.invoke() to call a second function, to tie chaining lambdas together?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.