将AWS SDK v3与aws-xray-sdk结合使用并在部署时出现segment.addNewSubsegmentWithoutSampling错误

问题描述 投票:0回答:1

封装版本:

  • @aws-sdk/客户端-*:3.398.0
  • aws-xray-sdk:3.5.1

我一直在为 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

amazon-web-services aws-sdk aws-sdk-nodejs aws-xray
1个回答
0
投票

请确保包含 AWS X-Ray Node SDK 的所有 lambda 层都使用相同版本(>= 3.4.0,其中引入了此 API)。请参阅此 GitHub 问题,了解此问题的更多详细信息和解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.