awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
8.07k stars 357 forks source link

Problems with 'aws-xray-sdk-core' #628

Open micro-jumbo opened 1 day ago

micro-jumbo commented 1 day ago

Hi,

I have this small lambda function:

import {ListBucketsCommand, S3Client} from "@aws-sdk/client-s3";

export const handle = async () => {
    const s3Client = new S3Client();
    const rest = await s3Client.send(new ListBucketsCommand());
    rest.Buckets?.forEach(bucket => {
        console.log(bucket.Name);
    });
    console.log('Hello world');
}

which works great.

But when I want to capture calls to S3 with X-Ray traces using the aws-xray-sdk-core library:

import {captureAWSv3Client} from 'aws-xray-sdk-core';
...
    const s3Client = captureAWSv3Client(new S3Client());
....

it starts failing with error:

2024-10-14T20:11:17.555Z    n/a FATAL   ReferenceError: Error resolving module '/var/task/assert' from '/var/task/index.mjs'
  at <anonymous> (/var/task/index.mjs:1:4249)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:16634)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:89512)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:91308)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:91325)
INIT_REPORT Init Duration: 62.39 ms Phase: init Status: error   Error Type: Runtime.ExitError
2024-10-14T20:11:18.415Z    n/a FATAL   ReferenceError: Error resolving module '/var/task/assert' from '/var/task/index.mjs'
  at <anonymous> (/var/task/index.mjs:1:4249)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:16634)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:89512)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:91308)
      at <anonymous> (/var/task/index.mjs:1:458)
      at <anonymous> (/var/task/index.mjs:6:91325)
INIT_REPORT Init Duration: 934.90 ms    Phase: invoke   Status: error   Error Type: Runtime.ExitError
START RequestId: 40310a38-3d40-4569-a631-e8e103a5105c Version: $LATEST
RequestId: 40310a38-3d40-4569-a631-e8e103a5105c Error: Runtime exited with error: exit status 1
Runtime.ExitError
END RequestId: 40310a38-3d40-4569-a631-e8e103a5105c
REPORT RequestId: 40310a38-3d40-4569-a631-e8e103a5105c  Duration: 975.40 ms Billed Duration: 976 ms Memory Size: 128 MB Max Memory Used: 19 MB  
XRAY TraceId: 1-670d7ae5-67eb6e644cea659b394342d3   SegmentId: 44f97b661735d32b Sampled: true   

Lambda function is created using cdk-lambda-llrt cdk construct:

import {LlrtBinaryType, LlrtFunction} from "cdk-lambda-llrt";

class MyLambda extends LlrtFunction {
    constructor(scope: Construct, id: string) {
        super(scope, id, {
            entry: path.resolve(__dirname, './handlers/my-lambda.ts'),
            handler: 'handle',
            llrtBinaryType: LlrtBinaryType.FULL_SDK,
            tracing: Tracing.ACTIVE,
            architecture: cdk.aws_lambda.Architecture.ARM_64,
        });
        this.addToRolePolicy(new cdk.aws_iam.PolicyStatement({
            actions: ['s3:*'],
            resources: ['*'],
        }));
    }
}

Any idea what the problem might be and how to solve it?

nabetti1720 commented 1 day ago

The error message is a bit odd, but it means that the Node.js assert module is not supported by LLRT at this time. The modules that are supported at this time are listed below. https://github.com/awslabs/llrt?tab=readme-ov-file#compatibility-matrix

micro-jumbo commented 1 day ago

I just checked the call chain and aws-xray-sdk-core calls cls-hooked, which uses assert.

What would be the best path to follow if I wanted to provide a shim for the assert (or any other) module to llrt?

richarddavison commented 21 hours ago

Hi, thanks for your report. It's a bit hard to tell, but for one the assert module is not supported. I also suspect that aws-xray-sdk-core might use a lot of node APIs not yet implemented. I'll take a closer look once I get the opportunity