具有 Node.JS Express 和请求的 AWS X-Ray
AWS X-Ray with Node.JS Express and request
所以我正在尝试使用我的 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),但我收到一条错误消息:
"Failed to get the current sub/segment from the context."
谁能告诉我我做错了什么?
您应该将 app.use(AWSXRay.express.openSegment("MyApp"))
代码移动到 app.use("/", indexRouter)
上方的 app.js
然后将 app.use(AWSXRay.express.closeSegment())
移到 app.use("/", indexRouter)
下面。
如果您查看所提供的 link 中引用的代码,您会注意到 openSegment
和 closeSegment
在路线之外,而不是在路线之内(因为您目前拥有它们)
link中的代码供参考:
var app = express();
//...
var AWSXRay = require('aws-xray-sdk');
app.use(AWSXRay.express.openSegment('defaultName')); //required at the start of your routes
app.get('/', function (req, res) {
res.render('index');
});
app.use(AWSXRay.express.closeSegment()); //Required at the end of your routes / first in error handling routes
所以我正在尝试使用我的 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),但我收到一条错误消息: "Failed to get the current sub/segment from the context."
谁能告诉我我做错了什么?
您应该将 app.use(AWSXRay.express.openSegment("MyApp"))
代码移动到 app.use("/", indexRouter)
app.js
然后将 app.use(AWSXRay.express.closeSegment())
移到 app.use("/", indexRouter)
下面。
如果您查看所提供的 link 中引用的代码,您会注意到 openSegment
和 closeSegment
在路线之外,而不是在路线之内(因为您目前拥有它们)
link中的代码供参考:
var app = express();
//...
var AWSXRay = require('aws-xray-sdk');
app.use(AWSXRay.express.openSegment('defaultName')); //required at the start of your routes
app.get('/', function (req, res) {
res.render('index');
});
app.use(AWSXRay.express.closeSegment()); //Required at the end of your routes / first in error handling routes