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

To help you get faster in identifying which part of the application is in trouble, we have designed an easy and ready to use way 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, because 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:

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 below inside the boot method of one of your ServiceProvider. Maybe the best place could be 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 .

New to Inspector?

Are you looking for a “code-driven” monitoring tool instead of having to install things at the server level?

Get a monitoring environment specifically designed for software developers avoiding any server or infrastructure configuration.

Thanks to Inspector, you will never have the need to install things at the server level or make complex configuration in your cloud infrastructure to monitor your application in real-time.

Inspector works with a lightweight software library that you can install in your application like any other dependencies. In case of Laravel you have our official Laravel package at your disposal. Developers are not always comfortable installing and configuring software at the server level, because these installations are often managed by external teams, and they are out of the software development lifecycle.

Visit our website for more details: https://inspector.dev/laravel/

Related Posts

How to configure HTTPS in Laravel Homestead

How to enable HTTPS in Laravel Homestead

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. In this article I’ll show you how to enable HTTPS for your local applications served by Homestead. I met this need because I am working to implement browser notifications for Inspector using Pusher/Beams. But Beams requires that the application be necessarily served over HTTPS.

Laravel cron scheduling and its secrets

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. Laravel tasks scheduling is one of the most useful features of the framework.The official documentation clearly explains what it is for: In the past, you may have written a cron configuration entry for each task you needed to schedule on your server. However, this

Laravel validation and custom rules in Inspector

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. Data validation is one of the fundamental features in any application and it is something developers manipulate almost every day. The value a software provides to users is often a function of the quality of data it is able to manage. Laravel ships with

How to build scalable applications

Read the best news, tips and other direct in your inbox