node.js - 使用 prometheus 监控 node.js axios 请求
问题描述
我有带有对外部 api 的 axios 请求的 node.js 微服务,我需要用 prometheus 监控它们。如我所见,prometheus 用于监控快速请求:
app.use((req, res, next) => {
const responseTimeInMs = Date.now() - res.locals.startEpoch;
httpRequestDurationMs
.labels(req.method, req.route.path, res.statusCode)
.observe(responseTimeInMs);
next();
});
但是我发现无法将它与 axios 一起使用(例如):
function getData() {
return axios.get(url)
.then (res) => {
[should put metrics somewhere here]
}
}
希望有人可以帮助解决这个问题。
解决方案
从prom-client文档 - 您可以启动计时器并在完成时调用 return 方法:
const end = gauge.startTimer();
xhrRequest(function(err, res) {
end(); // Sets value to xhrRequests duration in seconds
});
与仪表、直方图、摘要或任何其他对象一起使用...
推荐阅读
- jquery - 使用 jquery modal 时,如何在单击按钮之前停止处理?
- python - Pandas:导入多个数据帧,更改标题并将它们附加到循环中
- ubuntu - 尝试克隆项目时,带有外部 nginx 的自托管 GitLab 出现 502 错误
- python - 将 unet 答案转换回 .nii 文件
- laravel - Laravel Phpunit 测试一个请求,根据请求给出输出
- jersey - 将大型 HSSFWorkbook 写入 ServletOutputStream
- r - 将 R 中的表与 R 中的 2 个公共列合并
- javascript - Angular 属性数据返回为空
- javascript - 将网络摄像头设备连接到 PC
- flutter - Flutter:“GameNameEnum”类型的值不能分配给“GameNameEnum”类型的变量