What is applicationinsights?
The applicationinsights npm package is a powerful tool for monitoring and diagnosing performance issues in your applications. It provides a range of features for collecting telemetry data, tracking custom events, and monitoring dependencies, among other capabilities.
What are applicationinsights's main functionalities?
Telemetry Client
The Telemetry Client allows you to track custom events and metrics. This example demonstrates how to set up the client and track a custom event.
const appInsights = require('applicationinsights');
appInsights.setup('YOUR_INSTRUMENTATION_KEY').start();
const client = appInsights.defaultClient;
client.trackEvent({ name: 'my custom event', properties: { customProperty: 'custom value' } });
Request Tracking
Request Tracking automatically collects and sends telemetry data for incoming HTTP requests. This example shows how to set up a basic HTTP server with request tracking enabled.
const appInsights = require('applicationinsights');
appInsights.setup('YOUR_INSTRUMENTATION_KEY').start();
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8080);
Dependency Tracking
Dependency Tracking allows you to monitor the performance and success of external dependencies. This example demonstrates how to manually track a dependency.
const appInsights = require('applicationinsights');
appInsights.setup('YOUR_INSTRUMENTATION_KEY').start();
const client = appInsights.defaultClient;
client.trackDependency({ target: 'http://example.com', name: 'GET /', data: 'http://example.com', duration: 100, resultCode: 200, success: true });
Exception Tracking
Exception Tracking captures and sends telemetry data for exceptions. This example shows how to track an exception.
const appInsights = require('applicationinsights');
appInsights.setup('YOUR_INSTRUMENTATION_KEY').start();
const client = appInsights.defaultClient;
try {
throw new Error('Test error');
} catch (e) {
client.trackException({ exception: e });
}
Other packages similar to applicationinsights
winston
Winston is a versatile logging library for Node.js. While it focuses more on logging rather than telemetry, it can be used to log events, errors, and other information. It does not provide the same level of built-in telemetry and performance monitoring as applicationinsights.
log4js
Log4js is another logging library for Node.js, inspired by the Java log4j library. It provides a variety of appenders for logging to different destinations. Like winston, it focuses on logging rather than telemetry and performance monitoring.
newrelic
New Relic is a comprehensive application performance monitoring tool that provides similar functionalities to applicationinsights, including telemetry, error tracking, and performance monitoring. It is a commercial product with a broader set of features and integrations.
datadog
Datadog is a monitoring and analytics platform for developers, IT operations teams, and business users. It offers extensive features for monitoring applications, including telemetry, performance metrics, and log management. It is a commercial product with a wide range of integrations and capabilities.
Application Insights for Node.js
Node is a popular, lightweight platform for building fast and scalable network applications. This project extends Application Insights API surface to support Node.js. Application Insights is a service that allows developers to keep their application available, performing and succeeding. This node module will automatically send request telemetry, exceptions and logs for requests to the Application Insights service where they can be visualized in the Azure Portal.
Set-Up
Access to the source code is available from GitHub.
To use the Node Request Monitoring Module, you need an Application Insights Resource. The Application Insights Resource has an instrumentation key (iKey) that is unique to your application for monitoring.
If your Node site is running as an Azure Website, an Application Insights resource already exists for your application. See part one if your Node site is an Azure website, or part two if your Node site is not an Azure website.
Installing From Node Package Manager
Navigate to the folder where you want to install the module then open a command window and type:
npm install applicationinsights
After you install the node package, it will create new folder called node_modules
and a new folder called applicationinsights
within that folder.
It will also create an ai.config.json
file where you will manually insert your instrumentation key or iKey (detailed in steps 3 and 4 below).
Depending on what you want to track, you need to specify that you want to use the module in your project using a require statement:
require('applicationinsights');
Part One (Node site running as an Azure website)
-
Click on your website tile and scroll down to the Console tile. Type the command (as shown above) to install the module from the Node Package Manager.
-
Scroll to the bottom of your website blade to the Extensions tile. Click the Add button and select Visual Studio Online and add the extension. You may need to refresh the current blade for it to appear on your list of extensions.
-
Next, scroll to the top and in the Summary tile, click on the section that says Application Insights. Find your Node website in the list and click on it. Then click on the Properties tile and copy your instrumentation key.
-
Go back to your Extensions tile and click on Visual Studio Online to open up the VSO blade. Click the Browse button to open VSO to edit your files.
-
Once you open VSO, click on the ai.config.json
file and paste the instrumentation key into the iKey field.
-
Next, in the server.js
file, enter the require statement as stated above.
-
Open your website and click on a link to generate a request.
-
Return to your project tile in the Azure Portal. You can view your requests in the Monitoring tile.
Part Two (Node site running as a NON-Azure website)
Creating an Application Insights resource in the Azure Portal
-
First, create a new Application Insights resource in the Azure portal by clicking New --> Application Insights
.
-
Enter a name for your new Application Insights resource and click create. A new tile will appear on your dashboard.
-
Expand your app by clicking on the tile on your dashboard, then click on the Properties tile to open your application's Properties blade to obtain the instrumentation key (iKey). Click on the clipboard next to the iKey to copy it.
-
Copy and paste your iKey into the iKey variable in your ai.config.json
file.
-
Open your server.js
file that was generated when you installed the module, and entire the require statement as stated above.
-
Run your application and generate a request.
-
Return to your project tile in the Azure Portal and you can view your requests in the Requests tile in your application's blade. (In my example, you can see that I have generate 10 requests and it took 1 ms to process them).