The Fool
The Fool

Reputation: 20597

Use Application Insights with on-premises Node.js application

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

Upvotes: 1

Views: 3395

Answers (1)

Houssem
Houssem

Reputation: 1072

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/

Upvotes: 1

Related Questions