使用Node.js应用的应用洞察力。

问题描述 投票:0回答:1

我目前正试图为一个预置的node.js后台设置Application Insights。根据 文件 应该是可能的。

Application Insights 可以从任何互联网连接的应用程序收集遥测数据,无论其是否在内部或云中运行。使用以下步骤开始查看这些数据。

我发现了几个关于IIS on-premises的问题。但没有关于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隧道打到页面。我看到了我的页面,没有错误,一切正常。

我打了一堆端点,所以一定会有一些流量,甚至错误日志。但是我的应用洞察力并没有在地图上显示任何应用。

enter image description here

这是我的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.comen-usazureazure-monitorappip-addresses

这里有更多的文件 https:/docs.microsoft.comen-usazureazure-monitorappnodejs。

node.js azure azure-application-insights
1个回答
1
投票

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

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

你可以找到更多的方法,如 trackEvent , trackException...这里 https:/github.comMicrosoftApplicationInsights-node.js。

© www.soinside.com 2019 - 2024. All rights reserved.