Logo

dev-resources.site

for different kinds of informations.

Application Insights SDK for Node.js part 5 : Out of box telemetries - Exception, Performance and Live Metrics Stream

Published at
1/27/2020
Categories
applicationinsights
azure
node
Author
kenakamu
Categories
3 categories in total
applicationinsights
open
azure
open
node
open
Author
8 person written this
kenakamu
open
Application Insights SDK for Node.js part 5 : Out of box telemetries - Exception, Performance and Live Metrics Stream

In the previous article, I looked into Application Insights out of box telemetries about how to start using them and console collector as first example.

In this article, I look into Exception and Performance

Exception

I need to log whenever exceptions happened inside the application which terminates the application. Exception module uses process on following events to handle them.

One caveat is that framework such as Express swallowing (or handling) the exception nicely, which doesn't crash the application. This means most errors won't logged in Application Insights as it won't be reached to "UncaughtException".

One example I can break it is this. In the following endpoint, I get data from Azure Storage and it has own promise. If I raise an error inside the callback, then express cannot handle it and Application Insight caught it.

In this example, I use "res" object of tableSerivce and treat it as if res object in express get function. At a result, node.js cannot find "send" function in res object and crashed.

app.get("/:p/:r", (req, res) => {
    tableService.retrieveEntity("mytable", req.params.p, req.params.r, (err, result, res) => {
        if (!err) {
            res.send(`${result.PartitionKey._} - ${result.RowKey._}`);
        } else {
            res.send("No data");
        }
    });
});
Debugger listening on ws://127.0.0.1:32350/75b11dd4-c65e-4093-9342-3170ec21812e
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
TypeError: res.send is not a function
    at c:\Users\kenakamu\Desktop\mynode\index.js:110:17

I see the exception is logged in Application Insights as well.
Alt Text

How Application Insights does log when node.js crashes

When unhandledexception happens, node.js application will be shut down. process.on gives me some window to operate something important.

SDK uses following method in order.

this._client.trackException({ exception: error });
this._client.flush({ isAppCrashing: true });

As I explained in the first post, Application Insights SDK has feature to flush cached data into disk. When isAppCrashing is true, then the flush function synchronously writes cache to the disk storage. It later pickup the log when the application start running.

So if I don't see the exception log in Application Insights, it could be following reasons:

  • The application is still stop working so no one pickup the log
  • The application is up and running but the log is not picked up yet
  • SDK failed to flush log by some reason.

I usually wait several minutes after the application restart to see the log.

Explicit Exception logging

As I said, framework such as Express handles exception nicely so that the application won't crash. However, as a developer, I should handle exception by myself as much as possible by using try catch appropriately and track exception by using SDK.

Simply call trackException like example below. (the sample comment clearly said when to use this as well :))

client.trackException({exception: new Error("handled exceptions can be logged with this method")});

Performance

Performance.ts module collects following metrics.

  • CPU
  • Memory
  • Network
  • DependencyRate
  • ExceptionRate

Some of these counters will be auto collected when LiveMetricsCounter is enabled. The constructor has default parameters which affect the collection.

  • CollectionInteravl: one minutes by default
  • EnableLiveMetricsCounters: false by default
constructor(client: TelemetryClient, collectionInterval = 60000, enableLiveMetricsCounters = false) {
    if (!AutoCollectPerformance.INSTANCE) {
        AutoCollectPerformance.INSTANCE = this;
    }

    this._isInitialized = false;
    this._client = client;
    this._collectionInterval = collectionInterval;
    this._enableLiveMetricsCounters = enableLiveMetricsCounters;
}

When enable function is called from start or setAutoCollectPerformance function, Performance module set handlers for each metrics and call setInterval to collect them all.

TrackMetric

One important thing to know about this module is that it uses trackMetric function to track data, which typically goes to "Custom Metrics" but if I specify one of performance counter name, then it automatically goes to performanceCounters.

For example, if I add code like below and call the endpoint,

app.get("/cpu100", (req, res) => {
    client.trackMetric({name: "\\Processor(_Total)\\% Processor Time", value: 100});
    res.send("cpu100");
});

I can query the result not from customMetircs but from performanceCounters.
Alt Text

Live Metric Stream

If I want to monitor the application (near) real-time, then I can use Live Metric Stream. I need to explicitly enable it as it's disabled by default as I explained above. By default, it doesn't show anything.
Alt Text

I call setSendLiveMetrics(true) during setup.

var appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>")
    .setAutoCollectConsole(true, true)
    .setSendLiveMetrics(true)
    .start();

Once I restart the application and go back to Live Metric Stream, I can see live metrics from my application.
Alt Text

Native Performance

In addition to Performance, I can use NativePerformance.ts module to track additional information such as:

  • GarbageCollection
  • EventLoop
  • HeapUsage

These are very deep information when troubleshooting, which I can explicitly enable by calling setAutoCollectPerformance and pass true for second argument.
Alt Text

Summary

Exception and Performance are very important when monitor and troubleshooting the application. Live Metric is also nice to have when I need to watch it live!

I look into HttpRequests and HttpDependencies in next article.

applicationinsights Article's
30 articles in total
Favicon
Optimize and Monitor Power Apps Like a Pro with Application Insights
Favicon
Application Insights - 5 Tips to Improve Your Observability
Favicon
Announcing the Tracetest Integration with Azure App Insights
Favicon
Application Insights: Display Heatmap of Page Views per calendar week
Favicon
Using Application Insights with Bicep to monitor Azure Static Web Apps and Azure Functions
Favicon
Using Application Insights For Better Application Logging
Favicon
kusto & concat string array
Favicon
Week 12 2021 - Tips I learned this week
Favicon
Application Insights for Worker Service using Serilog
Favicon
Expose data from Application Insights
Favicon
Using Fiddler for Application Insights codeless java agent with docker on WSL2
Favicon
A quick intro: Application Insights for your Angular Java stack
Favicon
Monitorando uma aplicação VueJS com o Application Insights
Favicon
Cell CMS — Criando logs robustos e monitorando uma API
Favicon
Global Error Handler in Angular + Application Insights
Favicon
Monitor application via Application Insights and send alert: Part 4 Offline Scenario?
Favicon
Monitor application via Application Insights and send alert: Part 2 Understand Telemetry Types and use advanced monitoring
Favicon
Monitor application via Application Insights and send alert: Part 1 Set Rule and Action in Unified Alert
Favicon
Web App Monitoring with Application Insights
Favicon
Application Insights SDK for Node.js part 5 : Out of box telemetries - Exception, Performance and Live Metrics Stream
Favicon
Application Insights SDK for Node.js part 4 : Out of box telemetries - Basics and Console
Favicon
Application Insights SDK for Node.js part 3 : Application Map
Favicon
Application Insights SDK for Node.js part 1 : Basic usage
Favicon
Application Insights SDK for Node.js part 2 : Track events
Favicon
Bulk add Application Insights Availability Test IPs to Azure App Service Access Restrictions using Az PowerShell
Favicon
Deploying Azure Functions with Application Insights via ARM
Favicon
Gated Release
Favicon
Adding Application Insights to Azure Functions
Favicon
What the heck is Business Intelligence?
Favicon
Monitor application via Application Insights and send alert: Part 3 Optimize telemetry and alerts

Featured ones: