Integrating with Laravel Queues and Horizon

If you are making use of the Laravel Queues feature, then instrumenting the duration of individual jobs inside the workers can increase insight into the performance of your background jobs.

Instrumentation of worker jobs requires manual integration the moment. You need to hook into the before, after and failing events, for example from an AppServiceProvider or from a dedicated Tideways service provider:

<?php
declare(strict_types=1);

namespace App\Providers;

use Tideways\Profiler;

class TidewaysProvider extends ServiceProvider
{
    private $sampleRate = 10;
    private $service = 'worker';

    public function boot()
    {
        if (!class_exists('Tideways\Profiler')) {
            return;
        }

        Queue::before(function (JobProcessing $event) {
            // Assumption: API Key is set via php.ini or environment variable
            // Set 'api_key' option otherwise
            Profiler::start(array(
                'sample_rate' => $this->sampleRate
            ));
            Profiler::setServiceName($this->service);
            Profiler::setTransactionName($event->job->resolveName());
        });

        Queue::after(function (JobProcessed $event) {
            Profiler::stop();
        });

        Queue::failing(function (JobFailed $event) {
            Profiler::logException($event->exception);
        });
    }

    public function register()
    {
    }
}
The API Key must be set via php.ini or environment variable if not provided to Profiler::start.

Register the TidewaysProvider in config/app.php file by adding it to the providers key in the array:

<?php

'providers' => [
    // Other Service Providers
    App\Providers\TidewaysProvider::class,
],
Still need help? Email [email protected]