Inspector logo

Laravel

Build Status Latest Stable Version

Install

Run the composer command in your terminal:

composer require inspector-apm/inspector-laravel

Configure the API key

First put the Inspector API key in your environment file:

INSPECTOR_API_KEY=[api key]

Get a fresh API key by signing up for Inspector (https://app.inspector.dev/register) and creating a new project.

By default Inspector will report:

  • Queued Jobs execution
  • Artisan commands
  • Unhandled Exceptions

Test everything is working

Create a test route using the code below:

Route::get('test', function () {
    throw new \Exception('Test'));
})

Open this route in you browser to test connection between your app and Inspection API.

Monitor Web Requests

To activate web requests inspection, you can use the Inspector middleware WebRequestMonitoringso you are free to decide which routes need to be monitored based on your routes configuration or on your personal monitoring preferences.

Attach the middleware in the Http\Kernel class:

use Inspector\Laravel\Middleware\WebRequestMonitoring;

/**
 * The application's route middleware groups.
 *
 * @var  array
 */
protected $middlewareGroups = [
    'web' => [
        ...,
        WebRequestMonitoring::class,
    ],

    'api' => [
        ...,
        WebRequestMonitoring::class,
    ]
]

Report Errors & Exceptions manually

By default, every unhandled exception fired in your Laravel app will be reported automatically to be sure you're alerted for unpredictable errors in real time.

If your code fires an exception of which you want be aware, but you don't want to block the execution:

use Inspector\Laravel\Facades\Inspector;

try {

    // Your dangerous code...

} catch(LogicException $exception) {
    // Report an exception intentionally to collect diagnostics data
    Inspector::reportException($excetion);
}

If something goes wrong in your code you will be alerted in real time in your inbox to take a look at the error as it appears.

Full configuration

If you want full control of the package behaviour publish the configuration file:

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

That will add config/inspector.php in your Laravel configuration directory.

You can set the environment variables below:

INSPECTOR_API_KEY=[api key]
INSPECTOR_ENABLE=
INSPECTOR_QUERY=
INSPECTOR_QUERY_BINDINGS=
INSPECTOR_USER=
INSPECTOR_EMAIL=
INSPECTOR_JOB=
INSPECTOR_UNHANDLED_EXCEPTIONS=

Add Custom Segments

You can add custom segments in your timeline to measure the impact that a hidden code block has on a transaction performance.

Suppose you have a queued job that executes some database checks and an http request to an external service in background.

Job and queries are reported automatically, but it could be interesting to monitor and measure the execution of the http request. Simply use the Inspectorfacade:

use Inspector\Laravel\Facades\Inspector;

class TagUserAsActive extends Job
{
    protected $user;

    protected $guzzle;

    /**
     * Monitoring & Measure an external http request
     */
    public function handle()
    {
        try {
            // Add http call to your timeline
            $segment = Inspector::startSegment('http');
            $this->guzzle->post('[mail-marketing-url]/add_tag', [
                'email' => $htis->user->email,
                'tag' => 'active',
            ]);

        } catch (GuzzleException $exception) {
            Inspector::reportException($exception)
        } finally {
            // End segment
            $segment->end();
        }
    }
}

You will see the impact of the new segment in your timeline.

Filter out Web Requests and Commands

You can customize the list of excluded web requests. Check ignore_url field in your config/inspector.php file:

/*
 |--------------------------------------------------------------------------
 | Web request url to ignore
 |--------------------------------------------------------------------------
 |
 | Add at this list the url schemes that you don't want monitoring
 | in your Inspector dashboard. You can also use wildcard expression (*).
 |
 */
'ignore_url' => [
    'telescope*',
    'vendor/telescope*',
    'horizon*',
    'vendor/horizon*',
],

In the same way you can exclude from your dashboard some artisan commands customizing the ignore_commands field in config/inspector.php file:

/*
 |--------------------------------------------------------------------------
 | Artisan command to ignore
 |--------------------------------------------------------------------------
 |
 | Add at this list all command signature that you don't want monitoring
 | in your Inspector dashboard.
 |
 */
'ignore_commands' => [
    'migrate:rollback',
    'migrate:fresh',
    'migrate:reset',
    'migrate:install',
    'package:discover',
    'queue:listen',
    'queue:work',
    'horizon',
    'horizon:work',
    'horizon:supervisor',
],