Laravel Real-Time performance monitoring & alerting using Inspector

Valerio Barbera
Laravel real-timemonitoring

Have you ever desired to watch your code running, instead of just imagining it?

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 can be so hard to fix, creating a negative impact on the users experience, or block new potential customers during on-boarding.

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, but 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, avoiding customer complaints or even the loss of the customer.

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

Inspector is a composer package to add real-time code execution monitoring to your Laravel application, allowing you to work on continuous code changes while catching bugs and bottlenecks in real-time before users do.

Lavarel Code Execution Monitoring: how it works

It takes less than one minute to get started. Let me show you how it works.

Install the composer package

Run the composer command below in your terminal:

composer require inspector-apm/inspector-laravel

Configure the Ingestion Key

Get a fresh Ingestion key by signing up for Inspector (https://app.inspector.dev/register) and creating a new project, it takes just a few seconds.

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

Put the API key into your environment file:

INSPECTOR_API_KEY=xxxxxxxxxxxxxxx

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 monitors:

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

But, we turned on the light in the 50% of our app executed in background. The next step is to monitor all execution cycles generated by user interactions.

Monitor Incoming HTTP Requests

To activate http requests monitoring, you can use the WebRequestMonitoring  middleware 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,
    ]
]

Navigate your application

Now when your application receive a request Inspector automatically detect the most important events and create a visual representation of what happens inside your application during its normal operation, keeping an eye on code execution.

Instantly you will see transaction streams in your 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 there might 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 database queries are automatically detected by Inspector, 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 easily identify the impact of the new segment in the transaction 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 the 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 personal monitoring.

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 makes 90% of the work behind the scene, and you can solve issues before users even know about them.

Related Posts

In-Depth Scalable Applications monitoring

If you are responsible for the development of a software product you probably know that in one way or another if you can’t scale your application, if the software is slow or crashes, you can’t move your business forward. It’s basically locked. On the other hand, adding additional app servers isn’t just a click on