|
| 1 | +# Microsoft Application Insights for Laravel 10 |
| 2 | + |
| 3 | +A simple Laravel implementation for [Microsoft Application Insights](http://azure.microsoft.com/en-gb/services/application-insights/) |
| 4 | + |
| 5 | +This package is based on https://github.com/provisions-group/ms-application-insights-laravel |
| 6 | +The above package was for laravel 5 and had no updates so we started a new one for laravel 10 with some extra features for example queuing and handling api and web guards differently |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Update the `require` section of your application's **composer.json** file: |
| 11 | + |
| 12 | +```js |
| 13 | +"require": { |
| 14 | + ... |
| 15 | + "sahib/application-insights-laravel": "dev-master", |
| 16 | + ... |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +### Instrumentation Key |
| 21 | + |
| 22 | +The package will check your application's **.env** file for your *Instrumentation Key*. |
| 23 | + |
| 24 | +Add the following to your **.env** file: |
| 25 | + |
| 26 | +``` |
| 27 | +... |
| 28 | +MS_INSTRUMENTATION_KEY=<your instrumentation key> |
| 29 | +... |
| 30 | +``` |
| 31 | + |
| 32 | +You can find your instrumentation key on the [Microsoft Azure Portal](https://portal.azure.com). |
| 33 | + |
| 34 | +Navigate to: |
| 35 | + |
| 36 | +**Microsoft Azure** > **Browse** > **Application Insights** > *(Application Name)* > **Settings** > **Properties** |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +### Request Tracking Middleware |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +```php |
| 45 | + |
| 46 | +protected $middleware = [ |
| 47 | + ... |
| 48 | + 'AppInsightsMiddleware', |
| 49 | + ... |
| 50 | +] |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +The request will send the following additional properties to Application Insights: |
| 55 | + |
| 56 | +- **ajax** *(boolean)*: *true* if the request is an AJAX request |
| 57 | +- **ip** *(string)*: The client's IP address |
| 58 | +- **pjax** *(boolean)*: *true* if the request is a PJAX request |
| 59 | +- **secure** *(boolean)*: *true* if the request was sent over HTTPS |
| 60 | +- **route** *(string)*: The name of the route, if applicable |
| 61 | +- **user** *(integer)*: The ID of the logged in user, if applicable |
| 62 | +- **referer** *(string)*: The HTTP_REFERER value from the request, if available |
| 63 | + |
| 64 | +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**. |
| 65 | + |
| 66 | +### Exception Handler |
| 67 | + |
| 68 | +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: |
| 69 | + |
| 70 | +```php |
| 71 | +... |
| 72 | + |
| 73 | +# Delete this line |
| 74 | +use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
| 75 | + |
| 76 | +# Insert this line |
| 77 | +use Sahib\AppInsightsLaravel\Handlers\AppInsightsExceptionHandler as ExceptionHandler; |
| 78 | + |
| 79 | +... |
| 80 | +``` |
| 81 | + |
| 82 | +The exception handler will send additional properties to Application Insights, as above. |
| 83 | + |
| 84 | +### Client Side |
| 85 | + |
| 86 | +In order to register page view information from the client with Application Insights, simply insert the following code into your Blade views: |
| 87 | + |
| 88 | +```php |
| 89 | +{!! AIClient::javascript() !!} |
| 90 | +``` |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +### Custom |
| 95 | + |
| 96 | +If you want to use any of the underlying [ApplicationInsights-PHP](https://github.com/Microsoft/ApplicationInsights-PHP) functionality, you can call the methods directly from the server facade: |
| 97 | + |
| 98 | +```php |
| 99 | +... |
| 100 | +AIServer::trackEvent('Test event'); |
| 101 | +AIServer::flush();//immediate send |
| 102 | +\AIQueue::dispatch(\AIServer::getChannel()->getQueue())->delay(now()->addSeconds(3));//use laravel queue to send data later |
| 103 | +... |
| 104 | +``` |
| 105 | + |
| 106 | +See the [ApplicationInsights-PHP](https://github.com/Microsoft/ApplicationInsights-PHP) page for more information on the available methods. |
| 107 | + |
| 108 | +## Version History |
| 109 | + |
| 110 | +### dev-master |
| 111 | +- Initial commit. |
0 commit comments