inspector-apm/inspector-laravel

Laravel Octane (Swoole) support

Closed this issue · 3 comments

HTTP requests monitoring not working when using Laravel Octane (Swoole).
Similiar issue DataDog/dd-trace-php#704.

Hi @esseplore-it,
thank you for your report.

To make Inspector work using Octane, you simply need to manually flush data to Inspector, since Octane runs the application in a long running process.

  1. Create a new middleware that extends the Inspector original WebRequestMonitoring middleware:
php artisan make:middleware InspectorFlushMiddleware
  1. Override the terminate method to flush data when the request is completed:
<?php

namespace App\Http\Middleware;

use Inspector\Laravel\Middleware\WebRequestMonitoring;

class InspectorFlushMiddleware extends WebRequestMonitoring
{
    public function terminate($request, $response)
    {
        parent::terminate($request, $response);

        // Explicitly flush data to Inspector after the request is completed
        inspector()->flush();
    }
}
  1. Use this middleware in your App\Http\Kernel class instead of the original Inspector middleware:
    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            ...,
            InspectorFlushMiddleware::class,
        ],

        'api' => [
            ...,
            InspectorFlushMiddleware::class,
        ],
    ];

Let me know if this fix your issue, so we can add the solution to the official documentation.

Thanks! It's working now.
But, Servers KPIs return so many hostnames. Is it correct behavior?
image

It depends by the design of your system.

Take a look at this article, it could be helpful for this scenario:

https://inspector.dev/how-to-monitor-your-laravel-application-by-services-not-by-hostnames/