or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdcontext-correlation.mdindex.mdinitialization.mdlegacy-api.mdtelemetry-client.mdtelemetry-types.md
tile.json

initialization.mddocs/

Core Initialization

Primary initialization functions for setting up Application Insights monitoring with OpenTelemetry integration. These functions manage the lifecycle of telemetry collection and provide the modern API for Azure Monitor integration.

Capabilities

useAzureMonitor Function

Initializes Azure Monitor OpenTelemetry integration with automatic instrumentation and telemetry collection.

/**
 * Initialize Azure Monitor OpenTelemetry integration
 * @param options - Configuration options for Azure Monitor
 */
function useAzureMonitor(options?: AzureMonitorOpenTelemetryOptions): void;

interface AzureMonitorOpenTelemetryOptions {
  /** Connection string for Azure Monitor resource */
  connectionString?: string;
  /** Sets the state of exception tracking (enabled by default) */
  enableAutoCollectExceptions?: boolean;
  /** OTLP Trace Exporter Configuration */
  otlpTraceExporterConfig?: OTLPExporterConfig;
  /** OTLP Metric Exporter Configuration */
  otlpMetricExporterConfig?: OTLPExporterConfig;
  /** OTLP Log Exporter Configuration */
  otlpLogExporterConfig?: OTLPExporterConfig;
  /** Sets the state of performance tracking (enabled by default) */
  enableAutoCollectPerformance?: boolean;
  /** Enable automatic dependency collection */
  enableAutoCollectDependencies?: boolean;
  /** Enable automatic request collection */
  enableAutoCollectRequests?: boolean;
  /** Instrumentation options for specific libraries */
  instrumentationOptions?: InstrumentationOptions;
  /** Resource configuration for OpenTelemetry */
  resource?: Resource;
  /** Custom span processors */
  spanProcessors?: SpanProcessor[];
  /** Custom log record processors */
  logRecordProcessors?: LogRecordProcessor[];
  /** Custom metric readers */
  metricReaders?: MetricReader[];
  /** Sampling configuration */
  sampling?: SamplingOptions;
}

interface OTLPExporterConfig {
  /** Enable/Disable OTLP Exporter */
  enabled?: boolean;
  /** OTLP endpoint URL */
  url?: string;
  /** Headers to include in OTLP requests */
  headers?: { [key: string]: string };
  /** Timeout for OTLP requests */
  timeoutMillis?: number;
}

interface InstrumentationOptions {
  /** Console Instrumentation Config */
  console?: InstrumentationConfig & { 
    logSendingLevel?: SeverityNumber 
  };
  /** HTTP instrumentation configuration */
  http?: InstrumentationConfig;
  /** Azure SDK instrumentation configuration */
  azureSdk?: InstrumentationConfig;
  /** Database instrumentation configurations */
  mongodb?: InstrumentationConfig;
  mysql?: InstrumentationConfig;
  redis?: InstrumentationConfig;
  postgre?: InstrumentationConfig;
}

// OpenTelemetry type definitions
interface SpanProcessor {
  // OpenTelemetry span processor interface
}

interface LogRecordProcessor {
  // OpenTelemetry log record processor interface  
}

interface MetricReader {
  // OpenTelemetry metric reader interface
}

interface Resource {
  // OpenTelemetry resource interface
}

interface SamplingOptions {
  // OpenTelemetry sampling configuration
}

interface InstrumentationConfig {
  /** Enable/disable the instrumentation */
  enabled?: boolean;
  /** Additional configuration options */
  [key: string]: any;
}

enum SeverityNumber {
  // OpenTelemetry severity number enumeration
}

Usage Examples:

import { useAzureMonitor } from "applicationinsights";

// Basic initialization with connection string
useAzureMonitor({
  connectionString: "InstrumentationKey=12345678-1234-1234-1234-123456789abc;IngestionEndpoint=https://westus2-2.in.applicationinsights.azure.com/;LiveEndpoint=https://westus2.livediagnostics.monitor.azure.com/"
});

// Initialize with environment variable
useAzureMonitor();  // Uses APPLICATIONINSIGHTS_CONNECTION_STRING

// Advanced configuration
useAzureMonitor({
  connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
  enableAutoCollectExceptions: true,
  enableAutoCollectPerformance: true,
  enableAutoCollectDependencies: true,
  enableAutoCollectRequests: true,
  instrumentationOptions: {
    console: {
      enabled: true,
      logSendingLevel: SeverityNumber.INFO
    },
    http: {
      enabled: true,
      ignoreUrls: [/\/health$/]
    }
  },
  otlpTraceExporterConfig: {
    enabled: true,
    url: "https://my-otlp-endpoint.com/v1/traces"
  }
});

shutdownAzureMonitor Function

Cleanly shuts down Azure Monitor telemetry collection and flushes any remaining telemetry.

/**
 * Shutdown Azure Monitor OpenTelemetry integration
 * Flushes remaining telemetry and cleans up resources
 * @returns Promise that resolves when shutdown is complete
 */
function shutdownAzureMonitor(): Promise<void>;

Usage Examples:

import { shutdownAzureMonitor } from "applicationinsights";

// Graceful shutdown on process exit
process.on('SIGINT', async () => {
  console.log('Shutting down...');
  await shutdownAzureMonitor();
  process.exit(0);
});

// Shutdown in application cleanup
async function cleanup() {
  await shutdownAzureMonitor();
  console.log('Application Insights shutdown complete');
}

flushAzureMonitor Function

Forces immediate transmission of all queued telemetry without shutting down the system.

/**
 * Try to send all queued telemetry if present
 * Forces immediate transmission without shutdown
 * @returns Promise that resolves when flush is complete
 */
function flushAzureMonitor(): Promise<void>;

Usage Examples:

import { flushAzureMonitor } from "applicationinsights";

// Flush before critical operations
async function criticalOperation() {
  // Ensure all telemetry is sent before proceeding
  await flushAzureMonitor();
  
  // Perform critical operation
  await performCriticalTask();
}

// Periodic flush in long-running processes
setInterval(async () => {
  try {
    await flushAzureMonitor();
    console.log('Telemetry flushed successfully');
  } catch (error) {
    console.error('Failed to flush telemetry:', error);
  }
}, 60000); // Flush every minute

Initialization Best Practices

Early Initialization: Call useAzureMonitor() as early as possible in your application, before importing other modules:

// app.ts
import { useAzureMonitor } from "applicationinsights";

// Initialize first
useAzureMonitor({
  connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
});

// Then import other modules
import express from "express";
import { myBusinessLogic } from "./business-logic";

Environment Configuration: Use environment variables for connection strings:

# .env
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=...

Process Lifecycle Management: Ensure proper cleanup on application shutdown:

async function gracefulShutdown() {
  console.log('Starting graceful shutdown...');
  
  // Stop accepting new requests
  server.close();
  
  // Flush remaining telemetry
  await flushAzureMonitor();
  
  // Shutdown telemetry collection
  await shutdownAzureMonitor();
  
  process.exit(0);
}

process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT', gracefulShutdown);