aws/aws-xray-sdk-node

Invoking lambda from another lambda not invoking AWS Xray

MostafaaRamadan opened this issue · 1 comments

In short, I have a lambda that is being invoked from another lambda. The first lambda listens to the kinesis stream, acts as a filter(no tracing enabled in the first lambda), then invokes the second lambda(tracing enabled). the second lambda prints the trace id, with sampled value 0 and not showing any traces in x-ray. But when I test the second manually from the console works fine and shows traces in x-ray.

The goal for not enabling sampling in the first lambda, cause If I do so, I lost a lot of message that I am interested in, so I make it act as a filter, then enable tracing in the second one.

Second lambda specs:
Env: nodejs 16
Tracing is enabled.

Code:

var AWSXRay = require('aws-xray-sdk-core');
var AWS = AWSXRay.captureAWS(require('aws-sdk'));
AWS.config.update({region: process.env.AWS_REGION});
exports.handler = async (event) => {
    const traceId = process.env._X_AMZN_TRACE_ID;
    console.log('traceId:', traceId);
    const response = {
        statusCode: 202,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Any reason why this is happening?

Hi @MostafaaRamadan,
The reason you are not seeing any traces in X-Ray is because active tracing is not enabled in your first Lambda, and the second Lambda is using the parent sampling decision (which would be 0 as you mentioned, causing traces in your second Lambda function to not be sampled).

I understand you only want your first Lambda function to act as a filter, so perhaps a possible solution would be to enable active tracing in the first Lambda function and make all subsegments generated from it be unsampled so that you can only see the parent Lambda facade segment.

let facade = xrayContext.getSegment();

let unsampledSubsegment = facade.addNewSubsegmentWithoutSampling('unsampled-subsegment');

This will only generate 1 Lambda segment whose sampling decision (sampled=1) will be used by the second Lambda function. All subsegments generated within the first Lambda will be unsampled and will not appear on the service map. You can also disable any additional instrumentation in your first Lambda function if you are using any to remove subsegments you are not interested in. Can you try that out and see if you can now see traces?