Laravel Vapor

How to make Inspector works on Laravel Vapor.

Laravel Vapor will run your applicaiton is AWS Lambda environment. AWS Lambda has different default settings than a normal LAMP server.

After data collection Inspector sends this data to the remote API using two natives PHP functions behind the scenes (proc_open, proc_close).

AWS Lambda has these two functions disabled by default.

In a normal server environment they can be enabled/disbaled changing the php.ini settings, but Vapor currently doesn’t provide a way to overwrite this configuration during deployment, so you can’t enable proc_open, proc_close functions.

Below you will find the simple implementation of a custom Inspector Transport class to allow the package to work in AWS Lambda as well.

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;
use Inspector\Configuration;
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 if you haven’t already done, using 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'll 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\Inspector\Jobs\SendInspectorChunkJob::class,
],