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
This project provides a Visual Studio Application Insights SDK for Node.js. The SDK sends telemetry about the performance and usage of your live Node.js application to the Application Insights service. There you can analyze charts of request rates, response times, failures and dependencies, and diagnose issues using powerful search and aggregation tools.
The SDK provides automatic collection of incoming HTTP request rates and responses, performance counters (CPU, memory, RPS), and unhandled exceptions. In addition, you can add custom calls to track dependencies, metrics, or other events.
Requirements
Install
npm install applicationinsights
Get an instrumentation key
Create an Application Insights resource where your telemetry will be displayed. This provides you with an instrumentation key that identifies the resource. (You can try the SDK without sending telemetry: set the instrumentation key to a non-empty string.)
Usage
This will enable request monitoring, unhandled exception tracking, and system performance monitoring (CPU/Memory/RPS).
import appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>").start();
The instrumentation key can also be set in the environment variable APPINSIGHTS_INSTRUMENTATIONKEY. If this is done, no argument is required when calling appInsights.setup()
or appInsights.getClient()
.
Customized Usage
Disabling auto-collection
import appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>")
.setAutoCollectRequests(false)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.start();
Custom monitoring
import appInsights = require("applicationinsights");
var client = appInsights.getClient();
client.trackEvent("custom event", {customProperty: "custom property value"});
client.trackException(new Error("handled exceptions can be logged with this method"));
client.trackMetric("custom metric", 3);
client.trackTrace("trace message");
Learn more about the telemetry API.
Using multiple instrumentation keys
import appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>").start();
var otherClient = appInsights.getClient("<other_instrumentation_key>");
otherClient.trackEvent("custom event");
Examples
Tracking dependency
import appInsights = require("applicationinsights");
var client = appInsights.getClient();
var startTime = Date.now();
var endTime = Date.now();
var elapsedTime = endTime - startTime;
var success = true;
client.trackDependency("dependency name", "command name", elapsedTime, success);
Manual request tracking of all "GET" requests
var http = require("http");
var appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>")
.setAutoCollectRequests(false)
.start();
appInsights.client.commonProperties = {
environment: process.env.SOME_ENV_VARIABLE
};
appInsights.client.trackEvent("server start");
var port = process.env.port || 1337
var server = http.createServer(function (req, res) {
if(req.method === "GET") {
appInsights.client.trackRequest(req, res);
}
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello World\n");
}).listen(port);
var start = +new Date;
server.on("listening", () => {
var end = +new Date;
var duration = end - start;
appInsights.client.trackMetric("StartupTime", duration);
});
Contributing
Development environment
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.