0
0
AzureHow-ToBeginner · 4 min read

How to Use Azure Application Insights for Monitoring

To use Azure Application Insights, first create an Application Insights resource in the Azure portal, then add the provided Instrumentation Key to your application code or configuration. This enables automatic collection of telemetry data like requests, exceptions, and performance metrics for monitoring and troubleshooting.
📐

Syntax

Using Application Insights involves these main parts:

  • Instrumentation Key: A unique key from your Application Insights resource to link your app.
  • SDK Setup: Add the Application Insights SDK to your app to send telemetry.
  • Telemetry Data: Automatically or manually send data like requests, exceptions, and custom events.
javascript
const appInsights = require("applicationinsights");
appInsights.setup("<INSTRUMENTATION_KEY>").start();

const client = appInsights.defaultClient;

// Track a custom event
client.trackEvent({ name: "my custom event" });
💻

Example

This example shows how to add Application Insights to a Node.js app and send a custom event.

javascript
const appInsights = require("applicationinsights");

// Replace with your actual Instrumentation Key
appInsights.setup("12345678-1234-1234-1234-123456789abc").start();

const client = appInsights.defaultClient;

// Track a custom event
client.trackEvent({ name: "UserSignup" });

console.log("Application Insights initialized and event sent.");
Output
Application Insights initialized and event sent.
⚠️

Common Pitfalls

Common mistakes when using Application Insights include:

  • Not setting the correct Instrumentation Key, so no data is sent.
  • Forgetting to start the SDK after setup, which disables telemetry.
  • Sending too much custom telemetry, which can increase costs.
  • Not configuring sampling, leading to excessive data volume.
javascript
/* Wrong way: Missing start() call disables telemetry */
const appInsights = require("applicationinsights");
appInsights.setup("<INSTRUMENTATION_KEY>"); // Missing .start()

/* Right way: Always call start() */
appInsights.setup("<INSTRUMENTATION_KEY>").start();
📊

Quick Reference

StepDescription
Create ResourceMake an Application Insights resource in Azure portal.
Get KeyCopy the Instrumentation Key from the resource overview.
Add SDKInstall and configure the Application Insights SDK in your app.
Start SDKCall setup() with key and start() to enable telemetry.
Send DataUse automatic or manual telemetry calls to send data.
MonitorView telemetry data in Azure portal for insights.

Key Takeaways

Create an Application Insights resource and get its Instrumentation Key.
Add and start the Application Insights SDK in your application code.
Use automatic telemetry or send custom events to monitor your app.
Avoid missing start() call and incorrect keys to ensure data is sent.
Use sampling to control data volume and manage costs.