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.js® is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
-- nodejs.org
This project extends the 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 Python module will allow you to send telemetry of various kinds (event, trace, exception, etc.) to the Application Insights service where they can be visualized in the Azure Portal.
Requirements
Install
npm install applicationinsights
Get an instrumentation key
Note: an instrumentation key is required before any data can be sent. Please see the "Getting an Application Insights Instrumentation Key" section of the wiki for more information. To try the SDK without an instrumentation key, set the instrumentationKey config value to a non-empty string.
Usage
Configuration
import aiModule = require("applicationInsights");
var appInsights = new aiModule.NodeAppInsights({
instrumentationKey: "<guid>"
});
Track events/metrics/traces/exceptions
appInsights.trackTrace("example trace");
appInsights.trackEvent("example event");
appInsights.trackException(new Error("example error"), "handledAt");
appInsights.trackMetric("example metric", 1);
Track all http.Server requests
appInsights.trackAllHttpServerRequests();
var port = process.env.port || 0;
var server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(port);
Track uncaught exceptions
appInsights.trackAllUncaughtExceptions();
Contributing
Development environment
npm install node-mocks-http
npm install async
- (optional) Set an environment variable to your instrumentation key
set APPINSIGHTS_INSTRUMENTATION_KEY=<insert_your_instrumentation_key_here>
node Tests\TestServer.js
Note: the startup file can also be changed to TestServer.js in the *.njsproj so that the IDE runs tests instead of the example server.
<StartupFile>Tests\TestServer.js</StartupFile>
How to integrate with Azure
-
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.
-
From the website blade click "site settings". Under the "App settings" section, enter a new key "APPINSIGHTS_INSTRUMENTATION_KEY" and paste your instrumentation key into the value field.
-
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 server.js
file and 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.