Using Azure Application Insights with Node.js


Azure Application Insights are no doubt a great utility for tracking performance and diagnosis of applications on-the-go.

Application Insights can do a lot of things for you such as recording custom telemetry using TelemetryClient API for tracking Events, Exception, Metric, Trace, Dependency, Request, etc.

Setting up App Insights Client.

Setting up Node.js SDK

the only requirement at this point would be the instrumentation key (ikey) from Azure Portal. Application Insights uses the ikey to map data to your Azure resource. Before the SDK can use your ikey, you must specify the ikey in an environment variable or in your code.

iKey

Add the Node.js SDK library to your app’s dependencies

npm install applicationinsights –save

It would be the best practice to load your ikey from environment variables however for demo purpose i have hardcoded it.

Setting up App Insight client

At this point, you are ready to initialize app insight client using the ikey.

Explicitly load the library in your code.
on the top of your .js file

const appInsights = require("applicationinsights")
let client = appInsights.defaultClient;
// this config part should be set in ENVIRONMENT VARIABLES
let config = {
Id: "xxxxxxxx-xxxx-xxxx-xxxx-26728f1bb4e9",
Name: "Server-Q12",
applicationInsightsKey: "xxxxxxxx-xxxx-xxxx-xxxx-d7a116b10831"
}
view raw appinsightclient1.js hosted with ❤ by GitHub

once that is done setup the client using app insight key, adding custom properties to all events is optional and should be only done to interpret diagnostics  

appInsights.setup(config.applicationInsightsKey);
appInsights.start();
// set dimensions so reports can distinquish the origin of the logger.
appInsights.defaultClient.commonProperties = {
customProperty1: config.Id,
customProperty2: config.Name
};
client = new appInsights.TelemetryClient(config.applicationInsightsKey);
view raw appinsightclient2.js hosted with ❤ by GitHub

Consuming App Insight client

Consuming client is pretty simple and is merely a one-liner as everything hereafter is taken care of by SDK
client.trackMetric(
{
name: "custom metric:POC app-2609-100",
value: 7.5,
properties: {
testValueA: 1,
testValueB: `Apple`
}
});
view raw appinsightclient3.js hosted with ❤ by GitHub
Similarly, the rest of the processes can also be called using the same client.
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});
view raw appinsightclient4.js hosted with ❤ by GitHub
That shall be all.

Pingback for assistance, your Feedback’s are always a welcome… 🙂

Regards,
Aditya Deshpande

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s