最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AWS X-Ray with AWS SDK v3 for Node.sj - Stack Overflow

programmeradmin3浏览0评论

Is there any way how to use X-Ray instrumentation for AWS Node.js SDK v3? In SDK v2, AWSXray was able to capture any client and instrument it for tracing into X-Ray. I have been trying the same thing with v3 with following snippet

const { DynamoDBClient, ScanCommand } = require("@aws-sdk/client-dynamodb");
const AWSXRay = require("aws-xray-sdk");
// if unmented, this throws an exception
// AWSXRay.captureAWSClient(DynamoDBClient); 

const client = new DynamoDBClient({region: process.env.AWS_REGION});
// if unmented, this throws an exception
// AWSXRay.captureAWSClient(client);

const scan = new ScanCommand({
    TableName: 'xxx',
});
await client.send(scan) // ?

But both mented lines throw service.customizeRequests is not a function. This seems like AWS SDK s3 is not backward-patible with original AWSXRay library.

I found that SDK v3 contains XRay client, but this is just a client that can send spans and traces into AWS, not an instrumentation agent.

What's remended pattern of using X-Ray instrumentation with AWS SDK v3 for Node.js?

Is there any way how to use X-Ray instrumentation for AWS Node.js SDK v3? In SDK v2, AWSXray was able to capture any client and instrument it for tracing into X-Ray. I have been trying the same thing with v3 with following snippet

const { DynamoDBClient, ScanCommand } = require("@aws-sdk/client-dynamodb");
const AWSXRay = require("aws-xray-sdk");
// if unmented, this throws an exception
// AWSXRay.captureAWSClient(DynamoDBClient); 

const client = new DynamoDBClient({region: process.env.AWS_REGION});
// if unmented, this throws an exception
// AWSXRay.captureAWSClient(client);

const scan = new ScanCommand({
    TableName: 'xxx',
});
await client.send(scan) // ?

But both mented lines throw service.customizeRequests is not a function. This seems like AWS SDK s3 is not backward-patible with original AWSXRay library.

I found that SDK v3 contains XRay client, but this is just a client that can send spans and traces into AWS, not an instrumentation agent.

What's remended pattern of using X-Ray instrumentation with AWS SDK v3 for Node.js?

Share Improve this question edited Oct 14, 2024 at 9:36 Yves M. 31k24 gold badges109 silver badges149 bronze badges asked Jun 3, 2022 at 7:06 Martin MacakMartin Macak 3,8312 gold badges35 silver badges63 bronze badges 1
  • github./aws/aws-xray-sdk-node/tree/master/packages/… – Yves M. Commented Oct 14, 2024 at 9:39
Add a ment  | 

3 Answers 3

Reset to default 10

You need to use v3 patible x-ray-sdk capture function captureAWSv3Client

Here is working snippet:

const {DynamoDBClient, ScanCommand} = require("@aws-sdk/client-dynamodb");
const AWSXRay = require("aws-xray-sdk-core");

const dynamoClient = AWSXRay.captureAWSv3Client(
    new DynamoDBClient({})
)

const scan = new ScanCommand({
    TableName: 'xxx',
});

const response = await dynamoClient.send(scan)

It is also important to have permissions to write into X-Ray eg using SAM:

Policies:
  - AWSXrayWriteOnlyAccess

I also had this problem. It worked when I replaced:

import * as AWSXRay from 'aws-xray-sdk'

with

import AWSXRay from 'aws-xray-sdk'

If you are like me, using AWS SDK V3 and looking to integrate DynamoDB while using the popular Dynamoose library, here's how to do it:

import * as dynamoose from "dynamoose"
import * as AWSXRay from 'aws-xray-sdk'

dynamoose.aws.ddb.set(AWSXRay.captureAWSv3Client(new dynamoose.aws.ddb.DynamoDB({
    region: 'us-east-1'
})))

Make sure you have the relevant package installed:

npm install dynamoose aws-sdk @types/aws-sdk --save
发布评论

评论列表(0)

  1. 暂无评论