Laravel Background Processes Analytics with Inspector

Author profile picture

@valerio-barberaValerio Barbera

Software engineer from Italy, creator of Inspector.dev, passionate bassist.

Hi, I'm Valerio, software engineer from Italy and CTO at Inspector.dev
In this article I would show you how to turn on analytics in the dark side of your application: "background Jobs and scheduled Artisan commands execution".
Laravel provide a great architecture for queued Jobs and scheduled cron tasks. I myself use them a lot. They allow us to execute some piece of code asynchronously in the background out of a classic HTTP request cycle.
Often they are a cause of concern for developers because their execution is not related to direct user interaction. They can go wrong for days and there would be no way of knowing it until we manually check their results typically inside logs.
If something goes wrong during an HTTP request it will appear red bubbles or messages that inform user immediately of the problem. It's quite easy to discover relevant errors before release the software in production by automated tests, or using the application yourself.
If a queued Jobs or a scheduled Artisan command fails it will do it silently, without anyone noticing.
Inspector is a composer package to add real-time monitoring in Laravel applications. It's very easy to install and use, and it takes less than one minute to get started.
Let me show you how it works.

Installing Inspector

Install our composer package via composer:
composer require inspector-apm/inspector-laravel

Configure the API key

Get a fresh API key by signing up for Inspector (https://app.inspector.dev/register) and creating a new application, it takes just a few seconds.
Select and copy the API key:
Put the API key in your environment file:
INSPECTOR_API_KEY=13c371c434XXXXXXXXXXXX1f
By default Inspector will monitor all background tasks:
Queued JobsScheduled Artisan commandsNotificationsEmailsUnhandled Exceptions
You can also activate HTTP request monitoring. Take a look on our official documentation: https://docs.inspector.dev/laravel/http-requests-monitoring
Instantly you will see transaction streams in your project's dashboard:
and for each transaction you can monitor what your application is executing in real-time:

Add custom segments

Thanks to Inspector you are able to put everything you want in your timeline getting a real-time feedback about the execution of a code block inside your Job or in an Artisan command:
  • Http calls to external services
  • (Data sync, Payment, etc)
  • Function that deals with files (pdf, excel, images)
  • Algorithms
Thanks to the package you can add custom segments in your timeline in addition to those detected by default, to measure the impact that an hidden code block has on a transaction's performance.
Suppose to run an external http request in your code that by default is not present in the timeline.
Use Inspector facade:
<?php

\Inspector::addSegment(function () use ($guzzleClient) {
    
    $guzzleClient->get('https://www.example.com');
    
}, 'http');
This will produce a new segment in the timeline and now you can understand what your code is executing and its performance in real time:

Exceptions Alerting

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.
I wish that every change I make to my code could be perfect. But the reality is, this is not always the case. Some errors appear immediately after an update, while others pop up unpredictably. 
However, Inspector automates the detection of unknown issues especially in background tasks where it's even more tricky to investigate, so I no longer need to manually check the status of my apps continuously or wait reports directly from users. 
Inspector allows you to report an exception manually if you want be aware of it:
<?php

try {
    
    throw new Exception('Fragile code.');

} catch (Exception $exception) {
    Inspector::reportException($exception);
}
If something goes wrong in your code you will be alerted in real time in your inbox and the exception will be monitored for all subsequent occurrences:

Conclusion

When background Jobs or scheduled commands are involved getting a true picture of what's happening can require hours or, based on my experience, even days. Inspector can make a huge difference in terms of efficiency and productivity.
By delegating monitoring to an automous tool you can concrete avoid to lost customers and money due to unexpected technical problems in your applications.
Thank you so much for reading it. Don't hesitate to share your thoughts on the comment below or drop in live chat on our website - https://www.inspector.dev!
Previously published at https://www.inspector.dev/laravel-background-jobs-commands-monitoring-with-inspector/

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!