Links
Comment on page

Laravel Octane

How to monitor a Laravel application served by Octane
By default Inspector register a shutdown function to transfer data from your application to Inspector at the end of each request lifecycle.
Since Octane runs your application in a long running process the shutdown functions will never be called until the Octane server is stopped.
You have to manually flush the data collected by Inspector at end of the HTTP request life cycle.
You can do this by extending the original WebRequestMonitoring middleware provided by the Inspector package, and overwriting the terminate method to flush the data.

Create the new middleware

Use the command below to create the new InspectorOctaneMiddleware:
php artisan make:middleware InspectorOctaneMiddleware

Flush monitoring data

Overwrite the terminate method to add the flush statement:
<?php
namespace App\Http\Middleware;
use Inspector\Laravel\Middleware\WebRequestMonitoring;
class InspectorOctaneMiddleware extends WebRequestMonitoring
{
/**
* Handle tasks after the response has been sent to the browser.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response $response
* @return void
*/
public function terminate($request, $response)
{
parent::terminate($request, $response);
inspector()->flush();
}
}
The terminate method will run after the response has been sent to the browser, and the data will be sent asynchronously in the background.

Registering the middleware

Register the new middleware in App\Http\Kernel instead of WebRequestMonitoring class:
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
...,
//\Inspector\Laravel\Middleware\WebRequestMonitoring::class,
\App\Http\Middleware\InspectorOctaneMiddleware::class,
],
'api' => [
...,
//\Inspector\Laravel\Middleware\WebRequestMonitoring::class,
\App\Http\Middleware\InspectorOctaneMiddleware::class,
],
Now Inspector is able to recognize the end of a transaction and monitor your application properly.

No configuration is needed for artisan commands and background jobs. They will continue to be monitored as usual.