Laravel Real-Time Controllers monitoring – Tutorial

Valerio Barbera

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

Hi, I’m Valerio 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. They can cause negative impacts on the user experience, or block new potential customers during their on-boarding path.

To help you get faster in identifying which part of the application is in trouble, we have designed an easy to use tool to directly monitor your application controllers and methods.

Many of us continue to discover bugs and errors in the applications thanks to direct reports by users.

It’s too dangerous today.

When a user reports a problem it’s just too late. For a user that report a problem there are many others that simply stop using our application silently, moving to another product that fits their needs better.

Emerging software houses needs to hide bugs and bottlenecks from users eyes as much as possible to provide a better customer experience than the competitors.

A real-time monitoring tool help developers to address this problem, but many tools out there need a lot of resources and time to be installed and configured, making the life of developers more complicated.

Inspector is a composer package to add real-time monitoring in your Laravel application, it’s very easy to install and use, and it takes less than one minute to get started.

Install the composer package to monitor Laravel

Install our package by running the command below 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 application, it takes just a few seconds.

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

Inspector Laravel installation instruction
How to install the Inspector composer package in Laravel applications.

Put the API key into your environment file:

INSPECTOR_API_KEY=13c37c434XXXXXXXXXXXX

Http Requests Monitoring

To activate inspection when your application is executed by an incoming http request you can use the WebRequestMonitoring middleware.

Thanks to the middleware you are free to decide on which routes you want activate execution tracing, based on your routes configuration or on your personal monitoring preferences. 

WebRequestMonitoring middleware works like any other Laravel middleware you are familiar to.

Basically you can attach the middleware in the App\Http\Kernel class in one or more of your predefined middleware groups:

/**
 * The application’s route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
   'web' => [
      …,
      \Inspector\Laravel\Middleware\WebRequestMonitoring::class,
   ],   'api' => [
      …,
      \Inspector\Laravel\Middleware\WebRequestMonitoring::class,
   ]
]

Identify Controller/Method execution

If you prefer to have a clear visibility of what controller and method are executed based on any HTTP request, you can use the code snippet below inside the boot method of one of your ServiceProvider.

Maybe the best place could be the EventsServiceProvider.

use Illuminate\Routing\Events\RouteMatched;

/**
 * Register any events for your application.
 *
 * @return void
 */
public function boot() 
{
   parent::boot();  /*
   * Changes the way inspector reports transactions in your dashboard.
   */
   Event::listen(RouteMatched::class, function(RouteMatched $event) {
      $name = $event->route->getActionName();
 
      if(inspector()->isRecording()) {
         inspector()->currentTransaction()->name = $name;
      } else {
          inspector()->startTransaction($name);
      }
   });
}

This will change the way Inspector reports transactions in your dashboard from GET /path/{id} to YourController@yourMethod .

Laravel application monitoring

If you found this post interesting and want to drastically change your developers’ life for the better, you can give Inspector a try.

Inspector is an easy to use Code Execution Monitoring tool that helps developers to identify bugs and bottlenecks in their application automatically. Before customers do.

It is completely code-driven. You won’t have to install anything at the server level or make complex configurations in your cloud infrastructure.

It works with a lightweight software library that you can install in your application like any other dependency. You can try the Laravel package, it’s free.

Create an account, or visit our website for more information: https://inspector.dev/laravel

Related Posts

cloud costs savings with code execution monitoring

How to save thousands of dollars in cloud costs with Code Execution Monitoring

Hi, I’m Valerio, software engineer, co-founder and CTO at Inspector. In this article I want to show you a monitoring strategy to help you save thousands of dollars a year on cloud costs. The typical scenario where you can get the most payback from this strategy is when your application runs on multiple servers. If

The 5 Best Application Monitoring Tools for 2022

When it comes to application monitoring software, there are some great tools available to make our lives easier. We’ve compiled a list of the very best. No offense intended but the truth is customers are no longer interested in what you have to do to make your product work when they need it. They only

What Are Source Maps and How to Properly Use Them

You are debugging a web app for a client but the minified version of the Javascript and CSS code makes it impossible to understand what statements the browser is actually executing. You could break down the original code line by line in your editor putting some “console.log()” statements here and there, or try debugging it

Join the "Scalable Applications" community

Learn from other developers' experience.
Join the international community of developers to build scalable applications.

Inspector customer feedback
2020 © Inspector S.R.L. - VAT: 09552901218 - Progetto agevolato con la misura Resto al SUD RSUD0000000 - CUP C61B21012300008

How to build scalable applications

Get the e-book about the Inspector scalability journey.