【发布时间】:2019-10-26 12:14:17
【问题描述】:
所以我正在尝试使用我的 express 应用程序实现 X-Ray。 我有我的 app.js 文件并在内部引用了一个路由器文件。
我的项目结构:project/
routes/
index.js
app.js
app.js:
var indexRouter = require("./routes/index")
...
app.use("/", indexRouter)
...
index.js:
const AWSXRay = require("aws-xray-sdk")
const request = require("request")
router.post("/xray", async function(req, res, next) {
let app = req.app
app.use(AWSXRay.express.openSegment("MyApp"))
try {
console.log(AWSXRay.getSegment()) // this succesfully gets segment
AWSXRay.captureAsyncFunc("send", function(subsegment) {
request.get("http://www.google.com", { XRaySegment: subsegment }, function() {
res.json({
success: "success"
})
subsegment.close()
})
} catch (err) {
next(err)
}
app.use(AWSXRay.express.closeSegment())
})
我正在关注自动模式示例:通过 AWS 文档 (https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/index.html) 中的异步函数调用捕获,但我收到一条错误消息: “无法从上下文中获取当前子/段。”
谁能告诉我我做错了什么?
【问题讨论】:
标签: node.js amazon-web-services express request aws-xray