【发布时间】:2020-05-13 10:52:55
【问题描述】:
我目前正在尝试为本地 node.js 后端设置 Application Insights。根据documentation 应该是可能的。
Application Insights 可以从任何连接到 Internet 的应用程序收集遥测数据,无论它是在本地还是在云中运行。使用以下步骤开始查看此数据。
我发现了一些关于本地 IIS 的问题。但是对于 node.js 来说什么都没有。
这是我的后端代码。
let appInsights = require('applicationinsights');
appInsights.setup("<here I put the instrumentation key>")
.setAutoDependencyCorrelation(true)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(true, true)
.setAutoCollectExceptions(true)
.setAutoCollectDependencies(true)
.setAutoCollectConsole(true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(true)
.setDistributedTracingMode(appInsights.DistributedTracingModes.AI)
.start();
const port = 8080;
const express = require('express');
const http = require('http');
const app = express();
const server = http.Server(app);
server.listen(port, () => console.log('listenting on port', port));
app.use(express.json());
app.get('/', (req, res) => {
res.json({ message: 'Hello, Azure.' })
});
app.get('/error', (req, res) => {
try {
const wrong = 1
wrong = 2
} catch (err) {
return res.status(500).json({ error: err.message })
}
res.json({ message: '' })
});
我在远程开发服务器上运行它并通过 ssh 隧道访问页面。我看到了我的页面,没有错误,一切都很好。
我多次点击端点,所以肯定有一些流量甚至错误日志。但我的应用洞察力并没有在地图上显示任何应用。
这是我的 package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"eslint": "^7.0.0",
"eslint-config-google": "^0.14.0"
},
"dependencies": {
"applicationinsights": "^1.7.5",
"express": "^4.17.1"
}
}
不过,我认为这不是应用程序本身的问题。我觉得我在 Azure 门户上遗漏了一些东西。
另一个原因可能是我的防火墙,尽管它被配置为允许所有自己的请求通过。所以我不确定这是否适用于我的情况。
https://docs.microsoft.com/en-us/azure/azure-monitor/app/ip-addresses
这里有更多文档https://docs.microsoft.com/en-us/azure/azure-monitor/app/nodejs
【问题讨论】:
-
您可以尝试使用下面的代码跟踪自定义事件并检查是否出现?让客户端 = appInsights.defaultClient; client.trackEvent({name: "我的自定义事件", properties: {customProperty: "自定义属性值"}});
-
发送自定义事件后仍未显示。
-
即使在“搜索”标签中?
-
我实际上在搜索选项卡中有两个自定义事件!!请分享更多你的魔法
标签: node.js azure azure-application-insights