PHPackages                             sormagec/application-insights-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. sormagec/application-insights-laravel

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

sormagec/application-insights-laravel
=====================================

Microsoft Azure Application Insights for Laravel 10+ - A fully maintained package for pushing telemetry to Azure Application Insights

3.0.1(3mo ago)1385MITPHPPHP &gt;=8.1

Since Nov 2Pushed 3mo agoCompare

[ Source](https://github.com/bsormagec/application-insights-laravel)[ Packagist](https://packagist.org/packages/sormagec/application-insights-laravel)[ Docs](https://github.com/bsormagec/application-insights-laravel)[ RSS](/packages/sormagec-application-insights-laravel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (44)Used By (0)

Microsoft Azure Application Insights for Laravel 10+
====================================================

[](#microsoft-azure-application-insights-for-laravel-10)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9aaa57041d75b603f4d61fadfa1798ec41b6541efeca234e902720fd1cd3a20b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f726d616765632f6170706c69636174696f6e2d696e7369676874732d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sormagec/application-insights-laravel)[![Total Downloads](https://camo.githubusercontent.com/d48ea79e7f1120253db3bd143ca5339ac0d137945f767caa68f04d0a448385d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f726d616765632f6170706c69636174696f6e2d696e7369676874732d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sormagec/application-insights-laravel)[![License](https://camo.githubusercontent.com/2a685c43180b632dbad91d9fe2c9a4c8bcb7608b95d0225520cc5825d5a6bfb4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736f726d616765632f6170706c69636174696f6e2d696e7369676874732d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sormagec/application-insights-laravel)

A fully maintained Laravel package for Microsoft Azure Application Insights integration. Push telemetry from your Laravel web app and APIs directly to Application Insights with queue support and separate handling for API and web routes.

> **Note:** This is a fork of the original [larasahib/application-insights-laravel](https://github.com/GitSahib/application-insights-laravel) package with bug fixes, improvements, and continued maintenance.

Features
--------

[](#features)

- ✅ **Request Tracking** - Automatic HTTP request monitoring
- ✅ **Exception Tracking** - Automatic error reporting to Azure
- ✅ **Slow Query Tracking** - Automatic slow database query monitoring
- ✅ **Failed Job Tracking** - Automatic queue job failure reporting
- ✅ **Mail Tracking** - Track sent emails
- ✅ **Custom Events** - Track custom events and metrics
- ✅ **Trace Logging** - Send log messages to Application Insights
- ✅ **Queue Support** - Async telemetry via Laravel queues (Redis, etc.)
- ✅ **Client-Side JS** - Browser telemetry collection (PageView, BrowserTimings, errors)
- ✅ **Laravel 10+** - Full support for modern Laravel versions

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 10.0
- Guzzle &gt;= 7.0

Installation
------------

[](#installation)

```
composer require sormagec/application-insights-laravel
```

The package will auto-register via Laravel's package discovery.

Configuration
-------------

[](#configuration)

### 1. Publish the configuration file

[](#1-publish-the-configuration-file)

```
php artisan vendor:publish --tag=config --provider="Sormagec\AppInsightsLaravel\Providers\AppInsightsServiceProvider"
```

### 2. Set your Connection String

[](#2-set-your-connection-string)

Add to your `.env` file:

```
# Get this from Azure Portal > Application Insights > Overview > Connection String
MS_AI_CONNECTION_STRING=InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://.in.applicationinsights.azure.com/

# Queue delay in seconds (0 = sync, >0 = async via queue)
MS_AI_FLUSH_QUEUE_AFTER_SECONDS=5

# Enable debug logging (0 = disabled, 1 = enabled)
MS_AI_ENABLE_LOGGING=0

# Max query parameters to include in telemetry
MS_AI_MAX_QUERY_PARAMS=10

# Slow query threshold in milliseconds (queries slower than this are tracked)
MS_AI_DB_SLOW_MS=500

# Feature toggles (all enabled by default)
MS_AI_FEATURE_DB=true
MS_AI_FEATURE_JOBS=true
MS_AI_FEATURE_MAIL=true
MS_AI_FEATURE_HTTP=true

# Application Map & Correlation (NEW in v3.0.0)
MS_AI_CLOUD_ROLE_NAME=MyApp          # Node name in Application Map
MS_AI_CLOUD_ROLE_INSTANCE=production # Instance name (auto-detects Azure slot)
MS_AI_APP_VERSION=1.0.0              # Application version

# User & Session Tracking
MS_AI_TRACK_AUTH_USER=true           # Track authenticated user IDs
MS_AI_TRACK_SESSION=true             # Track session IDs
MS_AI_DETECT_SYNTHETIC=true          # Detect bots and health checks
```

### Azure App Service Configuration

[](#azure-app-service-configuration)

For proper Application Map rendering and slot differentiation:

**Production slot:**

```
MS_AI_CLOUD_ROLE_NAME=MyApp
MS_AI_CLOUD_ROLE_INSTANCE=production
```

**Staging slot:**

```
MS_AI_CLOUD_ROLE_NAME=MyApp
MS_AI_CLOUD_ROLE_INSTANCE=staging
```

> **Tip:** If running on Azure App Service, the slot name is automatically detected from `WEBSITE_SLOT_NAME` environment variable.

### Where to find the Connection String

[](#where-to-find-the-connection-string)

Azure Portal → Application Insights → Overview → Connection String

Usage
-----

[](#usage)

### Request Tracking Middleware

[](#request-tracking-middleware)

Add the middleware to your `app/Http/Kernel.php`:

```
protected $middlewareGroups = [
    'web' => [
        // ... other middleware
        \Sormagec\AppInsightsLaravel\Middleware\AppInsightsWebMiddleware::class,
    ],

    'api' => [
        // ... other middleware
        \Sormagec\AppInsightsLaravel\Middleware\AppInsightsApiMiddleware::class,
    ],
];
```

**Tracked Properties:**

- `ajax` - Whether the request is AJAX
- `ip` - Client IP address
- `pjax` - Whether the request is PJAX
- `secure` - Whether HTTPS was used
- `route` - Route name (if available)
- `user` - User ID (if authenticated)
- `referer` - HTTP referer

### Exception Handler

[](#exception-handler)

Replace the base exception handler in `app/Exceptions/Handler.php`:

```
