繁体   English   中英

将 Application Insights 与本地 Node.js 应用程序一起使用

[英]Use Application Insights with on-premises Node.js application

我目前正在尝试为本地 node.js 后端设置 Application Insights。 根据文档,它应该是可能的。

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 隧道访问页面。 我看到我的页面,没有错误,一切都好。

我打了很多次端点,所以一定有一些流量,甚至是错误日志。 但我的应用洞察并没有显示 map 上的任何应用。

在此处输入图像描述

这是我的 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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM