Laravel Real-Time performance monitoring & alerting using Inspector— [Part 1] Get started

Hi, I’m Valerio Barbera software engineer and Laravel — certified developer from Naples, Italy.

I’m the creator of Inspector, the real time monitoring dashboard for Laravel developers and teams.

In the last few year I have built a lot of API back-end, booking & payment systems and other business critical applications. All of them execute the 50% of their job live during users’ session and the other 50% in background where it’s even more tricky to know if all services are working fine or something is broken.

Inspector is a composer package to add real-time monitoring in Laravel applications, allowing you to work on continuous code changes while catching any side effects for your users in real time before they stumble into the problem.

It’s very easy to install and use, and it takes just two minutes to get started.

Let me show you how it works.

Install the composer package

Run the composer command in your terminal:

composer require inspector-apm/inspector-laravel

Configure the API key

Get a fresh API key by signing up for Inspector and creating a new project, it takes 30 seconds.

Select and copy your API key:

Put the API key into your environment file:

INSPECTOR_API_KEY=9a304b04b8XXXXXXXXXXXX1f

By default Inspector start to monitor:

  • Database interactions
  • Queued Jobs execution
  • Artisan commands
  • Email sent
  • Unhandled Exceptions

We turned on the light in the 50% of our app executed in background. The next step is to monitor all transactions generated by user interactions.

Monitor Incoming Web Requests

To activate web requests inspection, you can use the Inspector middleware WebRequestMonitoring as an independent component so 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,
    ]
]

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:

Enrich your timeline

You can add custom segments in your timeline to measure the impact that an hidden code block has on a transaction's 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 measured automatically, but it could be interesting to monitor and measure the execution of the http request and activate alerting if somethings goes wrong. Simply use the Inspector facade:

use Inspector\Laravel\Facades\Inspector;

class TagUserAsActive extends Job
{
    protected $user;
    // Monitring an external HTTP requests
    public function handle()
    {
        try {
        $segment = Inspector::startSegment('http');
            $this->guzzle->post('/mail-marketing/add_tag', [
                'email' => $htis->user->email,
                'tag' => 'active',
            ]);
        } catch (GuzzleException $exception) {
            Inspector::reportException($exception)
        } finally {
            $segment->end();
        }
    }
}

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

Errors & Exceptions Alerting

By default, every 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. It’s an unfortunate fact of life for developers which often also depends on problems caused by the connection between our application and other services.

However, Inspector makes my job easier. It automates the detection of unknown issues so I no longer need to manually check the status of my apps continuously or wait reports directly from users. If something goes wrong I’ll receive a notification in real time, and after each release I can stay informed about the impact of the latest code refactor.

If your code fires an exception but you don’t want to block the execution, you can report the error to inspector manually for private monitoring about availability of the external system.

use Inspector\Laravel\Facades\Inspector;

try {
   // Your dangerous external call here...
} catch (GuzzleException $exception) {
   Inspector::reportException($exception)
}

Furthermore, if the http request fails, you will be alerted in real time in your inbox to take a look at the error as it appears.

You even get access to detailed information gathered by Inspector in real time:

Conclusion

When a customer reports to you that something isn’t working, it forces you to drop whatever you are doing and start trying to reproduce the scenario, then recapture and reanalyze the logs in your own toolset.

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, productivity and customers happiness.

By delegating bug discovery to an automatic monitoring tool, it solves 90% of the problems in the half the time, before users even know about them.


If you enjoyed this article you should sign up for Inspector. It’s a real time monitoring system for Laravel developers and teams.

Try now It’s free: https://www.inspector.dev

No Comments Yet