Application Insights for Node.js

repository·main·Indexed 43 days ago

https://github.com/microsoft/applicationinsights-node.js

An OpenTelemetry-based SDK (version 3.16.0) for monitoring Node.js backend services. It provides distributed tracing, metrics, and logs to help diagnose performance issues and dependencies like databases and caches. Features include automatic web instrumentation, custom telemetry tracking via TelemetryClient, and SDK stats for monitoring telemetry reliability.

Tokens
15.9K
Snippets
46
Records
61
Agent score
48%

What's inside Application Insights

  1. Correlate Frontend and Backend telemetry

    main

    The SDK uses W3C trace context to automatically correlate telemetry across distributed systems. To enable correlation between a frontend application (using @microsoft/applicationinsights-web) and this Node.js backend SDK, you must ensure the frontend is configured with enableCorsCorrelation: true.

    Supported headers:

    • traceparent: (Required) W3C trace context header in format 00-traceId-spanId-flags.
    • tracestate: (Optional) W3C trace state header for vendor-specific data.

    When a request arrives at the backend, the SDK automatically extracts these headers, creates a correlation context, and associates all subsequent telemetry with that context. No manual backend code is required for this process.

    // Example of frontend setup with Application Insights
    const appInsights = new ApplicationInsights({
      config: {
        connectionString: "your-connection-string",
        enableCorsCorrelation: true  // Important for cross-domain correlation
      }
    });
    appInsights.loadAppInsights();
  2. Monitor telemetry reliability with SDK Stats

    main

    The SDK stats feature allows you to monitor the health of telemetry transmission from your Node.js application to Azure Monitor. It provides visibility into:

    • Telemetry ingestion success rates
    • Throttling events
    • Transmission errors

    This is useful for identifying network problems or gaps in your telemetry data.

  3. Enable Sampling to reduce telemetry volume

    main

    To reduce the amount of data sent to Application Insights, set the samplingPercentage property on the client's config object. A value of 100 (default) sends all data, while 0 sends nothing. When using automatic correlation, all data associated with a single request is included or excluded as a unit.

    const appInsights = require("applicationinsights");
    appInsights.setup("<YOUR_CONNECTION_STRING>");
    appInsights.defaultClient.config.samplingPercentage = 33; // 33% of all telemetry will be sent
    appInsights.start();
  4. Limitations of Application Insights 3.X SDK

    main

    The 3.X SDK is based on OpenTelemetry and provides distributed tracing, metrics, and logs. However, it excludes the following features available in the 2.X SDK:

    • Autopopulation of User ID and Authenticated User ID (when using the Application Insights JavaScript SDK)
    • Autopopulation of User IP (used for location attributes)
    • Ability to override Operation Name
    • Ability to manually set User ID or Authenticated User ID
    • Propagating Operation Name to Dependency Telemetry

    Warning: This SDK is for Node.js environments only. For web/browser scenarios, use the Application Insights JavaScript SDK.

  5. Use Automatic Web Instrumentation

    main

    Web instrumentation automatically injects the Application Insights JavaScript SDK into your Node.js server responses. This is enabled by setting enableWebInstrumentation to true or using the environment variable APPLICATIONINSIGHTS_WEB_INSTRUMENTATION_ENABLED = true.

    Requirements for instrumentation to trigger:

    • Response status code is 200.
    • Response method is GET.
    • Content-Type is html.
    • Response contains both <head> and </head> tags.
    • If compressed, encoding must be gzip, br, or deflate.
    • Response does not contain current/backup Web Instrumentation CDN endpoints.

    Environment Variables:

    • APPLICATIONINSIGHTS_WEB_INSTRUMENTATION_SOURCE: Change the CDN endpoint.
    • APPLICATIONINSIGHTS_WEB_INSTRUMENTATION_CONNECTION_STRING: Change the connection string used for web instrumentation.
  6. Configure the Application Insights SDK

    main

    All SDK configuration must be completed before calling start(). In version 3.X, changes made after start() will not take effect. You can configure the SDK using method-based chaining on the appInsights.setup() object or by modifying the config property of a TelemetryClient.

    To include console.log calls in your telemetry, use setAutoCollectConsole(true, true). By default, it only collects from supported third-party loggers like winston or bunyan.

    To use a different connection string for web instrumentation than the one used for the SDK, use enableWebInstrumentation(true, "your-connection-string").

    let appInsights = require("applicationinsights");
    appInsights.setup("<YOUR_CONNECTION_STRING>")
        .setAutoCollectRequests(true)
        .setAutoCollectPerformance(true, true)
        .setAutoCollectExceptions(true)
        .setAutoCollectDependencies(true)
        .setAutoCollectConsole(true, false)
        .setAutoCollectPreAggregatedMetrics(true)
        .setSendLiveMetrics(false)
        .setInternalLogging(false, true)
        .enableWebInstrumentation(false)
        .start();
  7. Get started with Application Insights

    main

    To begin monitoring your Node.js application, follow these steps:

    1. Create a resource: Create an Application Insights resource in Azure and retrieve your Connection String.
    2. Install the SDK: Run npm install --save applicationinsights.
    3. Load the SDK early: Import the package as early as possible in your application code (before other packages) to ensure proper instrumentation of dependencies.
    4. Configure: Call appInsights.setup() with your connection string or via the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable.
    5. Start: Call appInsights.start() to begin collecting and sending telemetry.

    Note on ESM: If you are importing the 3.X SDK as an ESM module, you must follow the OpenTelemetry instrumentation hook requirements.

    // 1. Load as early as possible
    let appInsights = require('applicationinsights');
    
    // 2. Configure with connection string
    appInsights.setup('YOUR_CONNECTION_STRING');
    
    // 3. Start telemetry collection
    appInsights.start();
  8. How to file issues and get help

    main

    To report bugs, request features, or ask questions about using the Application Insights Node.js SDK, use GitHub Issues.

    1. Search first: Check existing issues on GitHub to avoid duplicates.
    2. Bugs and Features: File a new Issue for bugs or feature requests.
    3. General Support: For help and questions regarding the usage of this project, create a 'Support request' issue on the GitHub repository.
    4. OpenTelemetry: If your issue is related to OpenTelemetry specifically, contact the OpenTelemetry JavaScript community directly.
  9. Enable self-diagnostics and debug logging

    main

    Application Insights SDK logs warnings to the console by default. To diagnose issues with the SDK itself, you can enable debug logging and generate telemetry for internal logs using .setInternalLogging(true, true) and .setAutoCollectConsole(true, true) during setup.

    Alternatively, you can use environment variables:

    • APPLICATIONINSIGHTS_ENABLE_DEBUG_LOGS: Enables debug logs.
    • APPLICATIONINSIGHTS_DISABLE_WARNING_LOGS: Disables warning logs.
    • APPLICATIONINSIGHTS_LOG_DESTINATION: Set to file or file+console to write logs to a file.
    • APPLICATIONINSIGHTS_LOGDIR: Specifies the directory for log files (defaults to /tmp on *nix or USERDIR/AppData/Local/Temp on Windows).
    let appInsights = require("applicationinsights");
    appInsights.setup("<YOUR_CONNECTION_STRING>")
        .setInternalLogging(true, true) // Enable both debug and warning logging
        .setAutoCollectConsole(true, true) // Generate Trace telemetry for winston/bunyan and console logs
        .start();
  10. Install the Application Insights Node.js SDK

    main

    Add the Application Insights SDK to your Node.js project using npm. If you are using TypeScript, ensure you have the @types/node package installed to avoid build issues, as the applicationinsights package contains its own built-in typings.

    npm install --save applicationinsights
  11. Configure SDK logging via environment variables

    main

    You can control the SDK's internal logging behavior using environment variables. This is useful for debugging the SDK itself.

    Log Level

    Set APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL to one of the following values:

    • NONE
    • ERROR
    • WARN
    • INFO
    • DEBUG
    • VERBOSE
    • ALL

    Log Destination and Directory

    • APPLICATIONINSIGHTS_LOG_DESTINATION: Set to file or file+console to write logs to a file.
    • APPLICATIONINSIGHTS_LOGDIR: Set the directory where logs should be stored.

    By default, if using file, a file named applicationinsights.log is generated in the system's temporary folder (/tmp on *nix or USERDIR/AppData/Local/Temp on Windows).

    process.env.APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL = "VERBOSE";
    process.env.APPLICATIONINSIGHTS_LOG_DESTINATION = "file";
    process.env.APPLICATIONINSIGHTS_LOGDIR = "C:/applicationinsights/logs";
    
    // Application Insights SDK setup....
  12. Configure Application Insights via environment variables

    main

    You can avoid hardcoding connection strings by using the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable. If this variable is set, you can call .setup() without any arguments.

    let appInsights = require('applicationinsights');
    
    // Uses APPLICATIONINSIGHTS_CONNECTION_STRING from env
    appInsights.setup();
    
    appInsights.start();