-
Notifications
You must be signed in to change notification settings - Fork 155
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
Express middleware does not return trace header in response #30
Comments
Thank you for the feedback. We will add this functionality in the next release. In the meantime we would also like to know more about the use case of getting trace header on the response if you would like to share. |
Hi @haotianw465 , I have encountered the same issue, when using with Express/Connect, the res.req might not be set. The mw_utils.js assumes res.req is always there, and uses the res.req.headers field. This leads to runtime error: TypeError: Cannot read property 'headers' of undefined. Issue code. I am also more inclined to mark this issue as a bug than just enhancement. Sample error stack trace I got:
To give you some context, I am using expressJS framework to build a microservice, and then also uses the aws-serverless-express middleware to run the express microservice as a serverless API. In other words, the code is executed in AWS lambda. And above stacktrace is from cloudwatch. |
Hi @xiananfan, sorry to hear the issue you encountered. This issue is for tracking a different use case where the The issue you mentioned is for supporting running Right now this support is in our backlog. We will look into this item as soon as we can. Please stay tuned and thank you for your patience. Feel free to open a separate issue and pull requests are always welcome. |
Hi @haotianw465, thank you for the response. Can you give more contexts on what you mean by "because Lambda container sends segment for function invocation so the SDK does special handling to cooperate Lambda and it doesn't handle the function being a web app serving proxied request"? Can you link me to the "special handling" code in SDK? I am happy to work on this issue if you can point me to the right direction on what are the likely affected components? |
Sure. The SDK has a different way of managing segment context when running in a AWS Lambda container. You can see the logic here: https://github.com/aws/aws-xray-sdk-node/blob/master/packages/core/lib/env/aws_lambda.js. Originally the SDK fits these two use cases: 1. For an express app, the SDK will create a segment for each request/response cycle and the intermediate remote calls or local computations happen in that cycle will be captured as subsegments and attached to the parent segment. 2. For a Lambda function, the Lambda container will create a segment for each function invocation and send it independently. The SDK will create a facade segment for that invocation to keep the structure intact. Intermediate remote calls or local computations happen in that invocation will be captured as subsegments and attached to the facade segment. The SDK will stream out those subsegments and leave the facade segment until it is later cleaned up. The X-Ray back-end will re-assemble the real segment from Lambda container and subsegments from function code. When running an express app inside a Lambda function, there will be a conflict of Lambda segment and express middleware segment both try to represent a root component in the microservice architecture (and further the service graph in AWS X-Ray console). The SDK should support this use case properly. We have a backlog item to work together with Lambda team to provide better customer experience for such use case. But we can't guarantee any further details or ETA at this point. Please let me know if you have any additional concerns. There are some further discussion about workaround but in python aws/aws-xray-sdk-python#2. Feel free to share any thought at either place. |
Hi @haotianw465, thank you for the explanation, this makes more sense to me now. So here is my thought: So, why not just treat lambda as another computation platform like EC2, ElasticBeanstalk and ECS? In AWS documentations, there is a section about plugins, for EC2, ECS and Beanstalk. Why not creating a plugin for lambda as well? In other words, disable the special handling by default, and let the user decide whether to enable creating segment automatically or manually by plugin? Maybe during the transitional phase, still support "special handling", but start support plugin based segment initialization? |
Thank you for the valuable suggestions. We will definitely bring these feedback to Lambda team and work together to provide a better integrated experience in the very near future. I've also created an issue #45 to track the feature so the current issue thread can focus on the response header. Please feel free to leave any additional comment on the new created issue. |
This is completely blocking our ability to trace Knex + MySQL calls (we're using Does anyone have an hacks or patches that I can apply right now to have Thanks! |
Hello I see the issue is still open, currently I use X-Ray SDK in a NodeJs docker container that runs in EKS and all requests go to the containers via a Classic Load Balancer which does not support tracing. Although the traces are sent to AWS via the X-Ray Daemon and I am able to visualize them in AWS, the response does not have the Trace ID header. I was reviewing library's code and in |
Hi @sergio-dreamcode would just returning the trace ID in a response header be sufficient for your use case? We try to minimize passing around the trace header to not overload other AWS Services that do process it. I can discuss with my team to return the header by default, and not just when there's a Also, can you elaborate on your classic load balancer use case? Specifically, how will you use the returned trace header in your load balancer to include it in your service map? |
Hello,
The ExpressJs middleware does not set the X-Amz-TraceId header on the response. The Java ServletFilter does. Seems like this would be universally useful, please add.
The text was updated successfully, but these errors were encountered: