Works on Laravel Vapor

How to make Inspector works on Laravel Vapor.

As opposed to a normal LAMP server, AWS Lambda execution environment has default settings that you need to adapt to.

Related to Inspector, after data collection the package sends data packets to the remote API asynchronously using two natives PHP functions behind the scenes (proc_open, proc_close).

Thanks to these functions Inspector start a process at OS level that will send monitoring data to our API silently in background, with zero impact on your application performance.

AWS Lambda have these two functions disabled by default.

In a normal server environment they can be enabled/disbaled changing the php.ini settings, but in AWS Lambda environment they seams disabled and Vapor currently doesn’t provide a way to overwrite this configuration during deployment, so we can’t unlock proc_open, proc_close to be used by the Inspector package.

Set a custom transport

The package design allows developers to inject a custom transport implementation at runtime. In the boot method of your ApplicationServiceProvider you can use the code below:

$this->app->inspector->setTransport(function ($configuration) {
return new QueueTransport($configuration);
});

The callback will receive an instance of the Inspector\Configuration class, that contains the Ingestion Key, the URL of our remote API, and all other information needed to build the appropriate HTTP call.

QueueTransport implementation

To keep the data sending process “asynchronously” the QueueTransport class should schedule a job to send monitoring data in background:

namespace App\Inspector\Transports;
use Exception;
use Inspector\Transports\AbstractApiTransport;
use Inspector\Transports\TransportInterface;
use App\Inspector\Jobs\SendInspectorChunkJob;
class QueueTransport extends AbstractApiTransport implements TransportInterface
{
/**
* @param string $data
* @throws Exception
*/
protected function sendChunk($data)
{
dispatch(new SendInspectorChunkJob($this->config, $data));
}
}

SendInspectorChunkJob receives the data in its constructor to be sent when the job is scheduled for execution. Here is the implementation of the Job:

namespace App\Inspector\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Inspector\Transports\AbstractApiTransport;
use Inspector\Transports\TransportInterface;
class SendInspectorChunkJob implements ShouldQueue
{
use Queueable;
/**
* Monitoring Data
*
* @var string
*/
protected $data;
/**
* Inspector configuration.
*
* @var Configuration
*/
protected $configuration;
/**
* SendInspectorChunkJob constructor.
*
* @param Configuration $configuration
* @param string $data
*/
public function __construct(Configuration $configuration, string $data)
{
$this->data = $data;
$this->configuration = $configuration;
}
/**
* Use the original CurlTransport.
*
* @param \Inspector\Configuration $configuration
* @throws \Inspector\Exceptions\InspectorException
*/
public function handle()
{
$transport = new \Inspector\Transports\CurlTransport($this->configuration);
$transport->sendChunk($this->data);
}
}

Avoid the infinite loop

The last thing to do is to inform Inspector to ignore the SendInspectorChunkJob from the monitored jobs, otherwise when the job is executed it will run a new transaction itself that generate another job and so on… the infine loop start.

Inspector provides a configuration property where you can specify the job classes that you want exclude from monitoring.

Publish the inspector config file with if you haven’t already done, with the command below:

php artisan vendor:publish --provider="Inspector\Laravel\InspectorServiceProvider"

Now you should have the inspector.php file in your config directory.

At the end of this file you should find the “ignore_jobs” property. Add SendInspectorChunkJob to the array of jobs to ignore:

/*
|--------------------------------------------------------------------------
| Job classes to ignore
|--------------------------------------------------------------------------
|
| Add at this list the job classes that you don't want monitoring
| in your Inspector dashboard.
|
*/
'ignore_jobs' => [
\App\Jobs\SendInspectorChunkJob::class,
],