【问题标题】:Use Application Insights with on-premises Node.js application将 Application Insights 与本地 Node.js 应用程序一起使用
【发布时间】: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


【解决方案1】:

如果您想跟踪所有请求,请尝试在app.get 下添加trackRequest

app.get('/', (req, res) => {
           appInsights.defaultClient.trackRequest({
          url: req.url
      });
  res.json({ message: 'Hello, Azure.' })
});

您可以找到更多方法,例如 trackEventtrackException.. 这里 https://github.com/Microsoft/ApplicationInsights-node.js/

【讨论】:

  • 看来我得用这个了。 trackNodeHttpRequest
  • 即使使用 'trackNodeHttpRequest ' 你也需要把它放在 createServer 或 get method 中,以拦截所有请求,因此你可以使用 appinsight 跟踪它们,你可以检查带有我提供的链接的文档和trackNodeHttpRequest的实现@
  • 是的,当然...我只是为其他读者注意到了这一点。在我的特殊情况下,它适用于trackNodeHttpRequest,但不适用于trackRequest。我仍然认为这不是预期的方式,因为它在官方文档中指出,请求日志记录通常默认启用,您必须明确禁用它。也许这不包括在 prem 应用程序中,如果是这样,那么文档中缺少一些东西。
猜你喜欢
  • 2016-02-22
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2021-12-22
相关资源
最近更新 更多