1

I am currently trying to set up Application Insights for an on-premises node.js backend. According to the documentation it should be possible.

Application Insights can gather telemetry data from any internet-connected application, whether or not it's running on-premises or in the cloud. Use the following steps to start viewing this data.

I found a couple questions regarding IIS on-premises. But nothing for node.js.

This is my backend code.

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: '' })
});

I run this on a remote development server and hit the page via ssh tunneling. I see my page, no error, all good.

I hit the endpoints a bunch of times so there must be some traffic and even error logs. But my application insights does not show any application on the map.

enter image description here

This is my 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"
  }
}

Although, I don't think it's some issue with the application itself. I feel like I am missing something on the Azure portal.

Another reason could be my firewall, although it's configured to let all own requests pass. So I am not sure if that's applicable to my case.

https://learn.microsoft.com/en-us/azure/azure-monitor/app/ip-addresses

Here are more docs https://learn.microsoft.com/en-us/azure/azure-monitor/app/nodejs

The Fool
  • 16,715
  • 5
  • 52
  • 86
  • can you try to track custom event using the code below and check if this appears ? let client = appInsights.defaultClient; client.trackEvent({name: "my custom event", properties: {customProperty: "custom property value"}}); – Houssem May 13 '20 at 11:28
  • It is still not showing after sending a custom event. – The Fool May 13 '20 at 11:42
  • 1
    even in the 'Search' tab ? – Houssem May 13 '20 at 12:04
  • I actually have two custom events in the search tab!! please share more of your wizardry – The Fool May 13 '20 at 13:43

1 Answers1

1

If you want to track all request try adding trackRequest under app.get

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

you can find more methods like trackEvent , trackException.. here https://github.com/Microsoft/ApplicationInsights-node.js/

Houssem
  • 1,042
  • 7
  • 16
  • I have to use this one it seems. `trackNodeHttpRequest` – The Fool May 13 '20 at 15:42
  • even with '`trackNodeHttpRequest` ' you need to put it inside `createServer` or the get `method` ,to intercept all request and so you can track them with `appinsight` , you can check the docs with the link that i provided and the implementation of `trackNodeHttpRequest` – Houssem May 13 '20 at 16:50
  • Yes of course... I just noted this for other readers as well. In my particular case it *only* works with `trackNodeHttpRequest` but not with `trackRequest`. I still think this is not the intended way though, as it states in the official docs request logging is normally enabled by default, you have to explicitly disable it. Maybe that doesn't count for on prem applications, if thats the case, then there is something missing in the docs. – The Fool May 13 '20 at 17:16