Serverless-offline: AWS X-Ray in offline mode

Created on 8 Nov 2017  路  6Comments  路  Source: dherault/serverless-offline

How to deal with such service? I have set up it in my func but when I run the func locally, I get:

Missing AWS Lambda trace data for X-Ray. Expected _X_AMZN_TRACE_ID to be set.

Any proposal?

Most helpful comment

@dherault Can we reopen this? Just setting AWS_XRAY_CONTEXT_MISSING=LOG_ERROR results in a ton of extra logging, and there is no obvious way to suppress that without collateral damage.

The best workarounds I've found are verbose and intrusive:

let AWS
if (process.env._X_AMZN_TRACE_ID) {
  AWS = require("aws-xray-sdk").captureAWS(require("aws-sdk"))
} else {
  console.log("Serverless Offline detected; skipping AWS X-Ray setup")
  AWS = require("aws-sdk")
}

const sequelize = new Sequelize(database, username, password, {
  // ...
  dialectModulePath: !!process.env.AWS_XRAY_DAEMON_ADDRESS ? "sequelize-aws-x-ray-mysql2" : "mysql2",
  // ...
})

All 6 comments

Seconded. Right now I'm just doing a check on an environment variable (process.env.DEBUG). When I invoke the function using DEBUG=true sls invoke local -f activateTrigger --path trigger.json I don't require the aws-xray-sdk but just require regular aws-sdk.

Would be down for a more elegant solution though.

If you set this environment variable, X-Ray logs the error and the function keeps running.
AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
If you have a logger configured, X-Ray uses that format, otherwise just console output.
Reference: http://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-configuration.html

@dherault Can we reopen this? Just setting AWS_XRAY_CONTEXT_MISSING=LOG_ERROR results in a ton of extra logging, and there is no obvious way to suppress that without collateral damage.

The best workarounds I've found are verbose and intrusive:

let AWS
if (process.env._X_AMZN_TRACE_ID) {
  AWS = require("aws-xray-sdk").captureAWS(require("aws-sdk"))
} else {
  console.log("Serverless Offline detected; skipping AWS X-Ray setup")
  AWS = require("aws-sdk")
}

const sequelize = new Sequelize(database, username, password, {
  // ...
  dialectModulePath: !!process.env.AWS_XRAY_DAEMON_ADDRESS ? "sequelize-aws-x-ray-mysql2" : "mysql2",
  // ...
})

Another potential workaround here, if you're not concerned with the extra logging, is to define the AWS_XRAY_CONTEXT_MISSING environment variable in your serverless.yml.

Thanks to @aburdiak for the answer.

I have been setting this locally to get XRay to be quiet in the logs:

AWS_XRAY_LOG_LEVEL=silent

You could use process.env.IS_LOCAL to check if function was invoked locally. Serverless sets it when you run invoke local
See:
https://github.com/serverless/serverless/issues/3639
https://github.com/serverless/serverless/blob/f93b27bf684d9a14b1e67ec554a7719ca3628135/lib/plugins/invoke/invoke.test.js

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aldofunes picture aldofunes  路  3Comments

stonebraker picture stonebraker  路  3Comments

conradoramalho picture conradoramalho  路  3Comments

adambiggs picture adambiggs  路  4Comments

MEGApixel23 picture MEGApixel23  路  4Comments