larasahib / application-insights-laravel
Microsoft Azure Application Insights for Laravel 10
Installs: 10 925
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
This package is auto-updated.
Last update: 2025-08-18 03:08:45 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.