node.jsazureazure-web-app-serviceazure-application-insights

Native Node.js Metrics in Application Insights


I have installed the applicationinsights-native-metrics package mentioned here: https://learn.microsoft.com/en-us/azure/azure-monitor/app/nodejs

I assume my app is generating metrics mentioned in the article but I can't quite figure out how to view them in Application Insights. Any pointers would be greatly appreciated.

It was suggested to me that I should be seeing a new namespace in the Metrics Explorer in Application Insights called "Node.js Extended Metrics" but I don't see this.

The documentation says you just need to install the package. After that didn't work, I tried explicitly requiring it in the app. Neither worked.


Solution

  • By using the below code and applicationinsights-native-metrics package, I generated the metrics as mentioned in this document.

    I ran the below commands to install the packages.

    npm install applicationinsights
    npm install applicationinsights-native-metrics
    

    server.js:

     let appInsights = require("applicationinsights");
     let http = require("http");
     const connectionString = '<ConnectionString>';
     appInsights.setup(connectionString).start(); 
     const port = process.env.PORT || 3000;
     let client = appInsights.defaultClient;
     client.trackEvent({ name: "my custom event", properties: { customProperty: "custom property value" } });
     client.trackException({ exception: new Error("handled exceptions can be logged with this method") });
     client.trackMetric({ name: "custom metric", value: 3 });
     client.trackTrace({ message: "trace message" });
     client.trackDependency({
       target: "http://dbname",
       name: "select customers proc",
       data: "SELECT * FROM Customers",
       duration: 231,
       resultCode: 0,
       success: true,
       dependencyTypeName: "ZSQL"
     });
     client.trackRequest({
       name: "GET /customers",
       url: "http://myserver/customers",
       duration: 309,
       resultCode: 200,
       success: true
     });
     http.createServer((req, res) => {
       client.trackNodeHttpRequest({ request: req, response: res });
       if (req.url === '/customers') {
         res.writeHead(200, { 'Content-Type': 'application/json' });
         res.end(JSON.stringify({ message: "Customers endpoint" }));
       } else {
         res.writeHead(404, { 'Content-Type': 'text/plain' });
         res.end("Not Found");
       }
     }).listen(port, () => {
       console.log('Server is running on port '+port);
     });
    

    package.json:

    {
      "name": "nodeappinsights",
      "version": "1.0.0",
      "description": "",
      "main": "server.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.24",
        "@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.5",
        "@opentelemetry/auto-instrumentations-node": "^0.47.1",
        "@opentelemetry/instrumentation": "^0.52.0",
        "@opentelemetry/sdk-node": "^0.52.0",
        "@opentelemetry/sdk-trace-node": "^1.25.0",
        "applicationinsights": "^2.9.5",
        "applicationinsights-native-metrics": "^0.0.10",
        "express": "^4.19.2"
      }
    }
    

    After running below command,

    node server.js
    

    I could be able to view the logs in Transaction search of Application Insights in Azure Portal as below,

    enter image description here enter image description here

    In Application Insights, go to Monitoring section, select Metrics, in Metric Namespace under Custom select azure.applicationinsights as below,

    enter image description here

    After updating the above metric namespace,you could be able to see the below Metrics.

    enter image description here