Laravel Real-Time performance monitoring & alerting using Inspector

Valerio Barbera

Hi, I’m Valerio Barbera software engineer from Italy and CTO at Inspector.

As product owner I learned on my skin how an application issue could create a negative impact on the users on-boarding process. Also the engagement is one of the most important KPI that I continually check.

I publish new code changes almost every day and unfortunately it’s quite impossible to anticipate all the problems that could happen after every release. Furthermore users don’t spend their time to report bugs, they just stop using our application, checking for another one that fits their needs better.

In most of the projects I’ve worked on the 50% of the drawbacks for users were caused by simple code mistakes, often in the code executed in background where it’s even more tricky to know if all is working fine or something is broken, and the more the application grows the more difficult it is to avoid these incidents.

When I started to share my idea behind Inspector I realized that many developers know the problem, they spend a lot of time to investigate strange behaviors inside their application, but they didn’t know that there’s a solution to eliminate this complexity in two minutes.

Be the first to know if my application is in trouble “before” users stumble into the problem, drastically reduced the negative impacts on their experience, giving me the right foundation to run a successful new users acquisition process and to continually increase engagement with less interruptions as possible.

Inspector is a composer package to add real-time monitoring in Laravel applications, allowing you to work on continuous code changes while catching exceptions and performance issues in real time before users stumble into the problem.

Lavarel performance monitoring: how it works

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 (https://app.inspector.dev/register) and creating a new project, it takes 30 seconds.

You’ll see installation instructions directly in the app screen:

Put the API key into your environment file:

INSPECTOR_API_KEY=9a304b04b8XXXXXXXXXXXX1f

Test everything is working

Execute our test command to check if your app send data to inspector correctly:

php artisan inspector:test

Go to https://app.inspector.dev/home to explore your demo data.


By default Inspector Lavarel monitoring system monitors:

  • 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 App\Http\Kernel class:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
       …,
       \Inspector\Laravel\Middleware\WebRequestMonitoring::class,
    ],    'api' => [
       …,
       \Inspector\Laravel\Middleware\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

Inspector monitors database query, background jobs, artisan commands by default, but could be many critical statements in your code that need to be monitored in terms of performance and error:

  • Http calls to external services
  • Function that deals with files (pdf, excel, images)

Thanks to our 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.

Let me show you a real life example.

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:

class TagUserAsActive extends Job
{
    /** 
     * @var User $user 
     */
    protected $user;

    // Monitring an external HTTP requests
    public function handle()
    {
        inspector()->addSegment(function () {            
            $this->guzzle->post('/mail-marketing/add_tag', [
                'email' => $this->user->email,
                'tag' => 'active',
            ]);        
        }, 'http');
    }
}

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

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.

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 Lavarel monitoring tool, it solves 90% of the problems in the half the time, before users even know about them.

Related Posts

How to accelerate application performance with smart SQL queries.

Why so many performance issues are caused by the database? We often forget that each request is not independent of other requests. If one request is slow, it’s unlikely to affect the others… right? Database is a shared resource used by all processes that runs in your application. Even just one poorly designed access can

Application performance monitoring tools – Why, when, what!

Responsible for the performance of your company’s IT operations? Read this guide to find out if you could benefit from application performance monitoring. Hi, I’m Valerio, software engineer from Italy and CTO at Inspector. In this article I reported my experience working on large applications, and how monitoring cames in my journey when I started

What is Laravel framework, and why is it so popular?

Hi, I’m Valerio, Software engineer and CTO at Inspector. If you are a developer, you’ve probably been pushed by everyone to learn a framework to develop your applications. But in every developer’s journey comes the moment to take a decision. It’s almost always the same moment we finally find the business idea we want to

Stop losing customers and money
due to technical problems in your applications

Monitor performance of your code execution in real-time and identify bugs and bottlenecks before your users do.
2020 © Inspector S.R.L - VAT: 09552901218