Amazon web services AWS lambda节点在使用X射线时显示错误
我有以下代码:Amazon web services AWS lambda节点在使用X射线时显示错误,amazon-web-services,aws-lambda,aws-xray,Amazon Web Services,Aws Lambda,Aws Xray,我有以下代码: var AWSXRay = require('aws-xray-sdk'); var AWS = AWSXRay.captureAWS(require('aws-sdk')); const client = AWSXRay.captureAWSClient(new AWS.DynamoDB.DocumentClient({region : 'eu-west-1'})); exports.handler = function(event, context,
var AWSXRay = require('aws-xray-sdk');
var AWS = AWSXRay.captureAWS(require('aws-sdk'));
const client = AWSXRay.captureAWSClient(new AWS.DynamoDB.DocumentClient({region : 'eu-west-1'}));
exports.handler = function(event, context, callback) {
AWSXRay.captureFunc('annotations', function(subsegment){
subsegment.addAnnotation('User', **);
subsegment.addAnnotation('Name', **);
});
var params = {
TableName: "****",
** all params **
};
client.query(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
callback(null,data);
}
});
}
执行上述代码时,会引发以下错误:
Response:
{
"errorMessage": "service.customizeRequests is not a function",
"errorType": "TypeError",
"stackTrace": [
"Object.<anonymous> (/var/task/index.js:5:24)",
"Module._compile (module.js:570:32)",
"Object.Module._extensions..js (module.js:579:10)",
"Module.load (module.js:487:32)",
"tryModuleLoad (module.js:446:12)",
"Function.Module._load (module.js:438:3)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)"
]
}
响应:
{
“errorMessage”:“service.customizeRequests不是函数”,
“errorType”:“TypeError”,
“stackTrace”:[
“对象。(/var/task/index.js:5:24)”,
“模块编译(Module.js:570:32)”,
“Object.Module._extensions..js(Module.js:579:10)”,
“Module.load(Module.js:487:32)”,
“tryModuleLoad(module.js:446:12)”,
“Function.Module._load(Module.js:438:3)”,
“Module.require(Module.js:497:17)”,
“需要(内部/module.js:20:19)”
]
}
以下是功能的日志:
Function Logs:
START RequestId: Version: $LATEST
module initialization error: TypeError
at Object.captureAWSClient (/var/task/node_modules/aws-xray-sdk-core/lib/patchers/aws_p.js:55:11)
at Object.<anonymous> (/var/task/index.js:5:24)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
END
函数日志:
开始请求ID:版本:$LATEST
模块初始化错误:TypeError
在Object.captureAWSClient(/var/task/node_modules/aws xray sdk core/lib/patchers/aws_p.js:55:11)
反对。(/var/task/index.js:5:24)
在模块处编译(Module.js:570:32)
在Object.Module.\u extensions..js(Module.js:579:10)
在Module.load(Module.js:487:32)
在tryModuleLoad时(module.js:446:12)
在Function.Module.\u加载(Module.js:438:3)
at Module.require(Module.js:497:17)
根据需要(内部/module.js:20:19)
结束
我应该如何解决这个问题?在此处发布短期解决方案以提高可见性
const ddbClient = AWSXray.captureAWSClient(new AWS.DynamoDB({...}));
const client = new AWS.DynamoDB.DocumentClient({
service: ddbClient
});
client.service = ddbClient;
请参阅此处的一些推理和讨论客户端没有.service公共变量。也就是说,这一个对我有用:
import * as AWS from 'aws-sdk'
import * as AWSXRay from 'aws-xray-sdk'
import { DocumentClient } from 'aws-sdk/clients/dynamodb'
export class DynDBAccess {
private readonly docClient: DocumentClient // = new AWS.DynamoDB.DocumentClient(),
constructor(
private enableAWSX:boolean,
){
if(this.enableAWSX)
{
logger.info('AWSX: enable')
const ddbClient = AWSXRay.captureAWSClient(new AWS.DynamoDB({
////Uncomment below to use serverless-dynamodb-local
//region: 'localhost',
//endpoint: 'http://localhost:8001',
//accessKeyId: 'DEFAULT_ACCESS_KEY', // needed if you don't have aws credentials at all in env
//secretAccessKey: 'DEFAULT_SECRET',
}));
this.docClient = new AWS.DynamoDB.DocumentClient({
service: ddbClient
});
}
...
这里的解决方案对我不起作用。虽然他们删除了错误,但我也没有看到AWS X-Ray中记录了任何DynamoDB调用 我没有尝试更新DocumentClient以设置插入指令的服务,而是尝试捕获所有内容,这对我来说很有效
import * as uninstrumentedAWS from 'aws-sdk'
import * as AWSXRay from 'aws-xray-sdk'
const AWS = AWSXRay.captureAWS(uninstrumentedAWS);
const client = new AWS.DynamoDB.DocumentClient();
await client.put({ TableName: "helloTable", Item: { "_id": (new Date()).toISOString() }}).promise();
这可能是由DynamoDB文档客户机如何实例化真正的低级客户机引起的问题。如果将客户机更改为其他服务(如S3或EC2),是否会看到相同的错误?如果没有,有一个可能对你有用的解决方法,但如果没有,请告诉我:是的,谢谢你为我工作了。。。。谢谢……@haotian465答案中提到的解决方法仍然有效。根据代码,可以配置服务变量。这种方法同样有效:)