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

[Feature Request] Add support for X-Ray on SAM Local #217

Closed
hugoprudente opened this issue Dec 5, 2017 · 26 comments
Closed

[Feature Request] Add support for X-Ray on SAM Local #217

hugoprudente opened this issue Dec 5, 2017 · 26 comments

Comments

@hugoprudente
Copy link

Hi Team,

When trying to use the AWS SAM Local with Node.js per example it fails with the following error:

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

This happens as the X-Ray Lambda already have the traces as per documentation: http://docs.aws.amazon.com/xray/latest/devguide/xray-services-lambda.html

So when using SAM Local this Trace Header is a missing.

@PaulMaddox
Copy link
Contributor

Thanks for opening this.

I believe the steps we need to take to add this to SAM Local are:

  1. Check if Tracing: Active is set in the SAM template for the function being invoked
  2. If so, run the AWS X-Ray daemon as a sidecar container in invoke.go
  3. Set the correct environment variables (e.g. _X_AMZN_TRACE_ID) in env.go

@svenemtell
Copy link
Contributor

It would be nice to use X-Ray from lambdas run using AWS SAM Local BUT more important to me would be a way to silently discard X-Ray calls in the code.
I want to be able to use the same code in production as when testing using AWS SAM Local and for that to work now I have written something like this (which I don't want to do):

if (process.env.AWS_SAM_LOCAL) {
AWS = require('aws-sdk');
} else {
// Only run AWS X-Ray when NOT running AWS SAM Local
AWSXRay = require('aws-xray-sdk-core');
AWS = AWSXRay.captureAWS(require('aws-sdk'));
}

@sanathkr sanathkr added type/feature Feature request and removed feature request labels Jan 7, 2018
@rvypandey
Copy link

+1

This will be a very valuable tool for us!

@Willis0826
Copy link

Could we using sam local with X-Ray now?

@metaskills
Copy link
Contributor

It appears not yet. I've been using the process.env check above as mentioned till then. Not a big issue for me either way.

@jabalsad
Copy link

The primary use-case I have for enabling x-ray integration is really just so that I don't have to have different configuration in my local dev environment vs my production environment. I'd like to be able to have my code enable x-ray, and the SAM local simulation not dying or raising errors on me. I'm less opinionated about whether the requests actually get submitted to the x-ray service upstream, and care more about just being able to emulate it locally.

{"level":"info","ts":"2020-01-30T01:52:08.225Z","msg":"Starting MyService HTTP server..."}
START RequestId: a4e6c4c6-1e57-1961-65c4-d482d910f26a Version: $LATEST
{"level":"debug","ts":"2020-01-30T01:52:08.237Z","msg":"Created new Resource: {\"id\":\"4KML2o7ZZXR\",\"field\":\"111122223333\",\"name\":\"integrationtestconfig\",\"destinationArn\":\"arn:aws:s3:::some-bucket\"}"}
2020-01-30T01:52:08Z [Error] Suppressing AWS X-Ray context missing panic: failed to begin subsegment named 'dynamodb': segment cannot be found.
END RequestId: a4e6c4c6-1e57-1961-65c4-d482d910f26a
REPORT RequestId: a4e6c4c6-1e57-1961-65c4-d482d910f26a	Init Duration: 159.52 ms	Duration: 6.39 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 27 MB
Invalid API Gateway Response Keys:
[snip snip]

@destinybonavita
Copy link

Any update on this?

@jfuss
Copy link
Contributor

jfuss commented Oct 13, 2020

We are not looking to support X-Ray locally at this time.

Closing.

@jfuss jfuss closed this as completed Oct 13, 2020
@hugoprudente
Copy link
Author

Not planing to have the feature and closing it instead moving it to a backlog to see how the priority should grow it's not the best way to handle the issue.

You are simply saying here that the community feedback and customers using the aws-sam-cli doesn't matter.

@awsjeffg
Copy link

awsjeffg commented Nov 3, 2020

@hugoprudente thanks for the honest feedback, we appreciate it. I realize it is frustrating to have filed this issue 3 years ago and have it closed after all this time. We decided to close this issue as part of an effort on the team currently maintaining SAM cli to do a better job of communicating with community members about what our intentions are, even if the answer isn't the one you would prefer.

Having said that, let's talk about it. If sam cli were to "support" X-Ray, what would your ideal dev experience be? The original report indicates you just don't want the lambda to crash - is that it, or do you want sam cli to actually behave like X-Ray in the cloud?

@awsjeffg awsjeffg reopened this Nov 3, 2020
@awsjeffg awsjeffg added the blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. label Nov 10, 2020
@hugoprudente
Copy link
Author

A first point is prevent the crash and prevent us dev to create unnecessary handling as the production will have X-Ray so the sam-cli that we use as dev/test environments also should handle it.

Supporting the feature is also necessary as what's the point to have something that I can't test my traces locally and only in production and if I need to test X-Ray locally I can't use SAM-CLI and do everything by hand so in the end it's better to do everything by hand as I have being doing instead wasting my time in the SAM-CLI as the effort of testing x-ray and adding and removing it to work with SAM-CLI doesn't make sense at all.

X-Ray already works locally publishing it to the X-RAY service or being intercepted locally by any UDP handlers so having the SDK on a similar fashion s the Lambda have so users can try simulate the best of the Lambda features locally and understand how do X-Ray will work for their use case it's just a extra plus.

From my use cases only if doesn't use X-Ray at all I have the SAM-CLI and if it has the probability of needing X-Ray in the feature I don't use it as the extra effort describing above of crating 2 lanes todo 1 thing I prefer to pay extra for the lambdas and test on the real environment than waste my developers time.

mndeveci added a commit to mndeveci/aws-sam-cli that referenced this issue Nov 23, 2020
* Release v1.0.0 (aws#2111)

* feat: Use aws-sam-cli docker images (aws#2066)

* Add Source for Docker Build Images (aws#2078)

* chore: Bump AWS SAM CLI Version (aws#2079)

* Version bump (aws#2080)

* chore: Bump AWS SAM CLI Version

* Change SAM CLI Version Number

There is a conflict betweeen PyPi documentation which asks for the
previous style
https://packaging.python.org/guides/distributing-packages-using-setuptools/#pre-release-versioning
and PEP 440 which proposes the style included in this change
https://www.python.org/dev/peps/pep-0440/#pre-releases - our MSI build
scripts failed on the pattern we were using before, this changes the
pattern.

* refactor: Build init.go with -s and -w flags to removed debug info (aws#2083)

* refactor: Bake Rapid into image on the fly (aws#2100)

* refactor: Bake Rapid into image on the fly

* force chmod on init binary in container for windows

* bake go debug bootstrap

Co-authored-by: Jacob Fuss <[email protected]>

* chore: Bump version to RC2 (aws#2104)

* Remove liblzma and libxslt from AL2 build images (aws#2109)

Discovered a regression where on Ruby 2.7, the `nokogiri` dependency
would build without errors, but would not run on local testing or on AWS
Lambda itself.

On further investigation, it appears that `nokogiri` can compile with or
without `liblzma` present, but if it is present in the build
enviornment (pre-change) and it is not present on the invoke
environment (true in AL2 runtimes), you will experience runtime failures
attempting to require `nokogiri`.

I have been able to verify that with these changes, `nokogiri` builds
correctly for Ruby 2.7 and runs both locally and on AWS Lambda.

* Build output dots (aws#2112)

* Use Low-Level Docker Client

Allows us to stream dots as a progress heartbeat. Pending unit tests and
black formatting.

* Get make pr Passing

Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>

* chore: Bump aws-lambda-builders and SAM CLI to 1.0.0 (aws#2116)

* fix: Update Python3.8 debug entrypoint (aws#2119)

* chore: readme update with screenshot (aws#2117)

* chore: readme update with screenshot

* chore: remove beta in the title

Co-authored-by: Alex Wood <[email protected]>

* feature: Lambda Code Sign integration for SAM CLI

* feature: Lambda Code Sign integration for SAM CLI (actual signing impl and unit tests)

* Add details to print_deploy_args
Add documentation for missing classes and methods

* Update couple of prompts

* Wording changes requested by UX & Docs Team

Co-authored-by: Alex Wood <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Sriram Madapusi Vasudevan <[email protected]>
mndeveci added a commit that referenced this issue Nov 23, 2020
* Code Sign Integration  (#217)

* Release v1.0.0 (#2111)

* feat: Use aws-sam-cli docker images (#2066)

* Add Source for Docker Build Images (#2078)

* chore: Bump AWS SAM CLI Version (#2079)

* Version bump (#2080)

* chore: Bump AWS SAM CLI Version

* Change SAM CLI Version Number

There is a conflict betweeen PyPi documentation which asks for the
previous style
https://packaging.python.org/guides/distributing-packages-using-setuptools/#pre-release-versioning
and PEP 440 which proposes the style included in this change
https://www.python.org/dev/peps/pep-0440/#pre-releases - our MSI build
scripts failed on the pattern we were using before, this changes the
pattern.

* refactor: Build init.go with -s and -w flags to removed debug info (#2083)

* refactor: Bake Rapid into image on the fly (#2100)

* refactor: Bake Rapid into image on the fly

* force chmod on init binary in container for windows

* bake go debug bootstrap

Co-authored-by: Jacob Fuss <[email protected]>

* chore: Bump version to RC2 (#2104)

* Remove liblzma and libxslt from AL2 build images (#2109)

Discovered a regression where on Ruby 2.7, the `nokogiri` dependency
would build without errors, but would not run on local testing or on AWS
Lambda itself.

On further investigation, it appears that `nokogiri` can compile with or
without `liblzma` present, but if it is present in the build
enviornment (pre-change) and it is not present on the invoke
environment (true in AL2 runtimes), you will experience runtime failures
attempting to require `nokogiri`.

I have been able to verify that with these changes, `nokogiri` builds
correctly for Ruby 2.7 and runs both locally and on AWS Lambda.

* Build output dots (#2112)

* Use Low-Level Docker Client

Allows us to stream dots as a progress heartbeat. Pending unit tests and
black formatting.

* Get make pr Passing

Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>

* chore: Bump aws-lambda-builders and SAM CLI to 1.0.0 (#2116)

* fix: Update Python3.8 debug entrypoint (#2119)

* chore: readme update with screenshot (#2117)

* chore: readme update with screenshot

* chore: remove beta in the title

Co-authored-by: Alex Wood <[email protected]>

* feature: Lambda Code Sign integration for SAM CLI

* feature: Lambda Code Sign integration for SAM CLI (actual signing impl and unit tests)

* Add details to print_deploy_args
Add documentation for missing classes and methods

* Update couple of prompts

* Wording changes requested by UX & Docs Team

Co-authored-by: Alex Wood <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Sriram Madapusi Vasudevan <[email protected]>

* - Update code signer param to align with tags and parameter-override params.
- Added additional unit tests

* chore: merge public develop with code signer changes

* feat: Code Signer integration tests

* add zip only if package needs to be signed

* chore: bump SAM CLI version, update sam-translator dependency and tests with 1.31.0

Co-authored-by: Alex Wood <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Jacob Fuss <[email protected]>
Co-authored-by: Sriram Madapusi Vasudevan <[email protected]>
@qingchm
Copy link
Contributor

qingchm commented Dec 7, 2020

@hugoprudente Hi there, first of all unfortunately the reason that we said that we're not looking to support it is 1) some components just cannot support x-ray locally, example: https://github.com/aws/aws-lambda-runtime-interface-emulator#level-of-support 2) local tracing with x-ray doesn't work if the x-ray resource is not created yet. We really want to support the idea of having local runs to capture more Lambda features but x-ray isn't one that we can support yet. However, we are more than willing to communicate with you to provide a workaround locally so that you don't have to waste your time modifying your templates. Here are 2 options that we will try to provide: 1) we can generate warnings instead of errors for tracing property so that the local run wouldn't fail 2) we can mock what x-ray tracing does by printing to stdout locally. Please give us a feedback on which workaround idea makes more sense to you so we can improve your experience using sam-cli!

@hugoprudente
Copy link
Author

Hi @qingchm,

I didn't know which project was behind the creation of the AWS Lambda mock. The base support for this feature should be provided there it looks like.

Regarding the X-Ray Resource is not created, which resource exactly you are referring to? As AWS X-Ray only needs the AWS X-Ray SDK in the code that is provided by AWS Lambda and bootstrapped together with the code that executes the lambda_handler in the same context.

Regarding the options that you provided here are my observations:

  1. Show the warning will continue to fail the code due to the missing embedded AWS X-Ray SDK in the lambda_handler, and if I add the SDK manually it will be different from the production, which will double the work of testing it locally.

  2. Mock the Xray, it will only be possible if you embed the X-Ray SDK on the lambda_handler executer context before the executions, which will solve the issue.


Here is a little bit more context on how the AWS X-Ray works with AWS Lambda so we can trace the best approach to support minimal features on SAM CLI.

The AWS Lambda code that executes the lambda_handler when X-Ray is enabled somehow loads the AWS X-RAY SDK Core in python will be this:

from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all

xray_recorder.configure(service='AWS SAM Local')
patch_all()

Then when I execute my lambda_handler during the AWS Lambda execution the xray_recoreder will be on my context creating my segment.

The aws_xray_sdk.core will automatically try to send the information to UDP2000 port, which may or not listen or do something with it.

With a couple of lines of doc, It's easy on how to pick that information and sent it to AWS X-RAY Api or for a mock socket server for troubleshooting.

The most important bit here is how can we bootstrap all the language supporting X-Ray to enable the X-Ray Recorder with the basic configuration and particularities like Segment not mutable.

I'm not sure if this will be resolved here, or if it will be resolved on the AWS Lambda Runtime Interface Emulator.

Either way, I will be glad to help add a wrapper to the executor to it but I'm sure that the AWS Lambda Team would be able to provide more insights on how they are wrapping it so this could be consistent, with production/edge and provide the same limitations to be as "real" as possible, as I know that some particularities that only happens on X-Ray SDK with AWS Lambda.

Looking forward to hearing from you on which project will be responsible for the lambda_handler execution so I can help you with a simple example and we can plan the roadmap on how to add this feature in the upcoming future, as I'm sure I'm not the only one that put SAM-CLI a side due to this limitation.

@qingchm
Copy link
Contributor

qingchm commented Dec 9, 2020

@hugoprudente Hi sorry for the confusion in my expressions, since I am studying on this issue just recently. But as for the first workaround, what I mean is to generate warning while skipping the requirement for the x-ray sdk. And by the creation what I mean is the daemon that the x-ray sdk communicates with. We need to integrate that into all environments as well in order for the sdk to work. Hopefully my words make better sense now. And to further address this issue, we are involving more discussions and I should be in touch with you soon!

@mgrandis
Copy link
Contributor

Hi @hugoprudente, @jabalsad, @svenemtell:
Are you still having issue running X-Ray instrumented code with the latest version of SAM CLI (1.15.0) and X-Ray SDK (npm 3.2.0)?
If so, could you provide a minimal code sample and steps to reproduce the issue?
I could not reproduce the issue (no error/crash) using:

@aws-sam-cli-stale-bot
Copy link

This issue has not received a response in 14 days.
If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@Dazza65
Copy link

Dazza65 commented May 31, 2021

I'm using 3.3.3 of the aws-sdk-xray nom package with 3.17.0 of the javascript sdk

Including the following line will generate the below error when invoking locally with v1.23.0 of the sam-cli. The lambda function works fine when deployed to AWS and captures the SSM traffic in Xray.

  const ssmClient = AWSXRay.captureAWSv3Client(new SSMClient({region: process.env.AWS_REGION} ));

START RequestId: e638f7ba-4740-4dc9-917e-5f019a7a6bd1 Version: $LATEST
2021-05-31T13:10:06.696Z        e638f7ba-4740-4dc9-917e-5f019a7a6bd1    INFO    Error: Missing AWS Lambda trace data fo    at Object.contextMissingRuntimeError [as contextMissing] (/var/task/node_modules/aws-xray-sdk-core/dist/lib/context    at async Runtime.exports.lambdaHandler [as handler] (/var/task/app.js:71:24)ddleware.js:6:22:71:25).js:81:49)
2021-05-31T13:10:06.700Z        e638f7ba-4740-4dc9-917e-5f019a7a6bd1    ERROR   Invoke Error    {"errorType":"Error","errorMessage":"Missing AWS Lambda trace data for X-Ray. Ensure Active Tracing is enabled and no subsegments are created outside the function handler.","stack":["Error: Missing AWS Lambda trace data for X-Ray. Ensure Active Tracing is enabled and no subsegments are created outside the function handler.","    at Object.contextMissingRuntimeError [as contextMissing] (/var/task/node_modules/aws-xray-sdk-core/dist/lib/context_utils.js:17:23)","    at Segment.resolveLambdaTraceData (/var/task/node_modules/aws-xray-sdk-core/dist/lib/env/aws_lambda.js:81:49)","    at Object.getSegment (/var/task/node_modules/aws-xray-sdk-core/dist/lib/context_utils.js:92:25)","    at Object.resolveSegment (/var/task/node_modules/aws-xray-sdk-core/dist/lib/context_utils.js:71:25)","    at /var/task/node_modules/aws-xray-sdk-core/dist/lib/patchers/aws3_p.js:63:67","    at /var/task/node_modules/@aws-sdk/middleware-content-length/dist/cjs/index.js:24:16","    at /var/task/node_modules/@aws-sdk/middleware-serde/dist/cjs/serializerMiddleware.js:6:12","    at async /var/task/node_modules/@aws-sdk/middleware-logger/dist/cjs/loggerMiddleware.js:6:22","    at async getData (/var/task/app.js:17:22)","    at async Runtime.exports.lambdaHandler [as handler] (/var/task/app.js:71:24)"]}
END RequestId: e638f7ba-4740-4dc9-917e-5f019a7a6bd1

@moretalk
Copy link

moretalk commented Aug 3, 2021

I can confirm the exact same error as @Dazza65 with

aws-sdk-xray-core 3.3.3
aws-sdk version 3.24.0
sam-cli v1.27.2

@seanlindo
Copy link

I'm also having this issue as well. I don't know how to use this feature without polluting my code.

@prenx4x prenx4x removed the blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. label Aug 30, 2021
@Rabadash8820
Copy link
Contributor

Based on @qingchm's earlier answer:

... unfortunately the reason that we said that we're not looking to support it is 1) some components just cannot support x-ray locally, example: https://github.com/aws/aws-lambda-runtime-interface-emulator#level-of-support ...

So I just opened this issue over on the Runtime Interface Emulator repo to get things moving on their end. If everybody goes and rants on that repo, maybe this feature will actually be implemented in the RIE, and then SAM CLI can support it as well. 😛

@jfuss
Copy link
Contributor

jfuss commented Feb 8, 2023

Is this still an issue with recent releases? I am using 1.61 locally and not seeing failures others are seeing here. I am using python but I could have a setup that doesn't trigger what others have seen. I am seeing warnings from the X-Ray Python SDK but my function still works locally (returns what I expected).

If someone tries this in recent versions, can you try to add a fake _X_AMZN_TRACE_ID to the Functions Env Vars or share a full example? The understanding from talking to teams is that should be the only thing we need to do. But I haven't been able to even fail the function locally when using X-Ray.

@valerena
Copy link
Contributor

I can still see the issue with the a newer version of SAM CLI (1.73.0). To reproduce it, it's not enough to just configure X-Ray, but you need to actually make an AWS call with X-Ray configured.

The way I've reproduced this is to use the nodejs16.x "Serverless API" example, and change this line to get an X-Ray enabled DynamoDB client:

const xray = require('aws-xray-sdk-core');
const client = new dynamodb.DocumentClient();
const docClient = xray.captureAWSClient((client).service);

The other thing to take into account for the workaround for this error is that the fake _X_AMZN_TRACE_ID needs to have a specific format (otherwise, it will still fail, but with another error).
The variable needs to be something like this: Root=1-63f3c410-4ea085a24d12dea053c79ff4;Parent=a0ba11b52fb8b69f;Sampled=1

@valerena
Copy link
Contributor

Actually I just noticed that I was using a really old version of the X-Ray library aws-xray-sdk-core. And it looks like the issue doesn't exist anymore with newer versions of it. I was using 1.x, and the current version is 3.4.1. We might have to check if this is actually fixed in other runtimes too, but at least in NodeJS now the library continues silently if the X-Ray env var is not present (unlike the old behavior, where it failed).

@mbfreder
Copy link

I tried to reproduce the issue with Node16.x, but the error doesn't show up. I also tried with the java 11, then python 3.9 runtimes just to be very sure, but I was still able to invoke the functions without any errors. Based on that, I think we can safely close this issue

@hawflau
Copy link
Contributor

hawflau commented Mar 16, 2023

Also confirmed it works in Go with aws-xray-sdk-go. I agree we can close this issue

@hawflau hawflau closed this as completed Mar 16, 2023
@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

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