封装版本:
我一直在为 AWS 服务编写自定义帮助程序类,并具有以下配置:
基本辅助类(常见功能和其他功能)
{{AWS_SERVICE}} 类扩展基本帮助程序
像这样:
export class S3Helper extends BaseHelper {
private client: S3Client;
private httpHelper: HTTPHelper;
/**
* Creates a new instance of S3Helper.
* @param logger {ILogger} - Injected logger.
* @param client {S3} - Optional injected S3 client. If not supplied, a new S3 client will be created.
* @param options {ClientConfiguration} - Optional S3 configuration. Only used if the service parameter is not supplied.
*/
constructor(logger: ILogger, client?: S3Client, options: S3ClientConfig = {}, httpHelper?: HTTPHelper) {
super(logger);
this.client = client || AWSXray.captureAWSv3Client(new S3Client(options));
this.httpHelper = httpHelper || new HTTPHelper(logger);
}
}
所有 Lambda 函数都启用了 X 射线跟踪,因此这不是我的问题。在 Node 18.x 上部署和运行时,(在本例中)从 Param 存储读取时会出现问题。
{
"level": "ERROR",
"msg": "segment.addNewSubsegmentWithoutSampling is not a function",
"errorName": "TypeError",
"errorMsg": "segment.addNewSubsegmentWithoutSampling is not a function",
"errorStack": "TypeError: segment.addNewSubsegmentWithoutSampling is not a function\n at /var/task/index.js:6948:30\n at /var/runtime/node_modules/@aws-sdk/middleware-content-length/dist-cjs/index.js:26:16\n at /var/runtime/node_modules/@aws-sdk/middleware-serde/dist-cjs/serializerMiddleware.js:13:12\n at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:22\n at async SSMHelper.GetParameter (/var/task/index.js:890311:20)\n at async Runtime.handler (/var/task/index.js:906957:27)"
}
我尝试了多个不同版本的 AWS SDK、XRAY SDK、不同的组合等等。当旧的遗留代码位于 AWS SDK v2 上并使用时,一切正常
captureAWSClient
请确保包含 AWS X-Ray Node SDK 的所有 lambda 层都使用相同版本(>= 3.4.0,其中引入了此 API)。请参阅此 GitHub 问题,了解此问题的更多详细信息和解决方案。