larasahib/application-insights-laravel

Microsoft Azure Application Insights for Laravel 10

1.1.6 2025-08-18 03:07 UTC

README

A simple Laravel integration for Microsoft Application Insights

This package was originally inspired by provisions-group/ms-application-insights-laravel. That package was built for Laravel 5 and is no longer maintained.

This package is a fully maintained, standalone implementation for Laravel 10+, updated to support the latest Application Insights requirements. It provides a clean way to push telemetry from your Laravel web app and APIs directly to Application Insights, with additional features such as queue support and separate handling for API and web guards.

Installation

Update the require section of your application's composer.json file:

"require": {
	...
	"larasahib/application-insights-laravel": "1.0.4",
	...
}

Instrumentation Key / Connection String

The package will check your application's .env file for your Instrumentation Key.

โš  Note: MS_INSTRUMENTATION_KEY is deprecated. Use MS_AI_CONNECTION_STRING instead.

.env example

# Old way (deprecated)
# MS_INSTRUMENTATION_KEY=00000000-0000-0000-0000-000000000000

# New way (recommended)
MS_AI_CONNECTION_STRING=InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/

Where to find the connection string

You can get the connection string from your Azure Portal:

Application Insights โ†’ Overview โ†’ Connection String

You can find your instrumentation key on the Microsoft Azure Portal.

Navigate to:

Microsoft Azure > Browse > Application Insights > (Application Name) > Settings > Properties

Usage

Request Tracking Middleware

To monitor your application's performance with request tracking, add the middleware to your in your application, found in app/Http/Kernel.php. It has to be added after the StartSession middleware has been added.

protected $middleware = [
	'api':
		...
		'AppInsightsApiMiddleware',
		...
	'web':
		...
		'AppInsightsWebMiddleware',
		...
]

The request will send the following additional properties to Application Insights:

  • ajax (boolean): true if the request is an AJAX request
  • ip (string): The client's IP address
  • pjax (boolean): true if the request is a PJAX request
  • secure (boolean): true if the request was sent over HTTPS
  • route (string): The name of the route, if applicable
  • user (integer): The ID of the logged in user, if applicable
  • referer (string): The HTTP_REFERER value from the request, if available

The middleware is also used to estimate the time that a user has spent on a particular page. This is sent as a trace event named browse_duration.

Exception Handler

To report exceptions that occur in your application, use the provided exception handler. Replace the following line in your application's app/Handlers/Exception.php file:

...

# Delete this line
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

# Insert this line
use Larasahib\AppInsightsLaravel\Handlers\AppInsightsExceptionHandler as ExceptionHandler;

...

The exception handler will send additional properties to Application Insights, as above.

Client Side

In order to register page view information from the client with Application Insights, simply insert the following code into your Blade views:

{!! \AIClient::javascript() !!}

NOTE: Microsoft recommend that you put the script in the <head> section of your pages, in order to calculate the fullest extent of page load time on the client.

Custom

If you want to use any of the underlying ApplicationInsights-PHP functionality, you can call the methods directly from the server facade:

...
\AIServer::trackEvent('Test event');
\AIServer::flush();//immediate send
\AIQueue::dispatch(\AIServer::getChannel()->getQueue())->delay(now()->addSeconds(3));//use laravel queue to send data later
...

See the ApplicationInsights-PHP page for more information on the available methods.

๐Ÿ“ฆ Version History

โœ… dev-master

  • Initial commit with basic Laravel integration.
  • Included dependency on microsoft/application-insights.

๐Ÿ“ฆ 1.0.1

  • First stable release.
  • Basic support for tracking requests and exceptions using Microsoft Application Insights PHP SDK.
  • Registered Laravel service provider and middleware for web & API.

๐Ÿ“ฆ 1.0.2

  • Refactored and renamed internal classes to avoid naming conflicts and improve maintainability.
  • Improved configuration publishing and service bindings.

๐Ÿ“ฆ 1.0.3

  • Added queue support via AppInsightsTelemetryQueue.
  • Enabled asynchronous event logging using Laravel queues.

๐Ÿ“ฆ 1.0.4

  • Minor fixes to config merging and bootstrapping.
  • Added ability to set instrumentation key via environment variable.

๐Ÿ“ฆ 1.0.5

  • Fixed service provider registration issues in Laravel 10.
  • Added support for Laravel's config:cache.

๐Ÿ“ฆ 1.0.6

  • Deprecated usage of microsoft/application-insights SDK.
  • Prepared for a complete rewrite using a custom telemetry client.

๐Ÿš€ 1.0.7

  • โœ… Replaced dependency on the deprecated Application Insights SDK with a custom-built HTTP client.

  • โœ… Implemented custom Telemetry_Client class compatible with Application Insights ingestion API.

  • โœ… Support for tracking:

    • Requests
    • Exceptions
    • Custom Events
    • Traces (Logs)
  • โœ… Introduced flush() mechanism with NDJSON batching.

  • โœ… Removed reliance on outdated or unsupported packages.

  • โœ… Maintained backward compatibility with existing service provider and class interfaces.

  • ๐Ÿงช Enhanced logging and error handling for production robustness.