Laravel Http Requests Monitoring

Http requests monitoring

Introduction

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 monitoring, based on your routes configuration or on your personal monitoring preferences.

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

!> To get more information on how middlewares works in Laravel, take a look at Laravel's official documentation.

Option 1 - Attach to the middleware group (recommended)

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' => [
        ...,
        \Palzin\Laravel\Middleware\WebRequestMonitoring::class,
    ],

    'api' => [
        ...,
        \Palzin\Laravel\Middleware\WebRequestMonitoring::class,
    ]
]

Usually web and api middleare groups wrap your entire application, so in two lines of code you can monitoring all incoming http requests.

Option 2 - Use as middleware key

In alternative you can create a middleware key so you can attach it in a specific route or group:

/*
 * Create a new middleware key (ultimate) in your \App\Http\Kernel class.
 */
protected $routeMiddleware = [
    ...,
    
    'ultimate' => \Palzin\Laravel\Middleware\WebRequestMonitoring::class,
];

Use "ultimate" middleware key in you routes:

/*
 * Attach the "ultimate" middleware in your routes
 */
Route::prefix('app')->middleware('ultimate')->group(function () {
    ...
});

Ignore Web Requests

?> Filtering by URLs is supported by default via a simple configuration parameter. Learn more here.

Palzin Monitor provide a basic strategy to turn off monitoring in certain part of your application, but the middleware is designed to allows you to extend it and add your own strategy overwriting shouldRecorded method that should return a boolean to determine if the current request should be recorded or not.

Run the artisan command below to create a new middleware class:

php artisan make:middleware PalzinFilterMonitoringMiddleware

In the new middleware class extend the Palzin Monitor middleware and override the shouldRecorded method to implement your filtering strategy:

<?php

use \Palzin\Laravel\Middleware\WebRequestMonitoring;


class PalzinFilterMonitoringMiddleware extends WebRequestMonitoring
{
    /**
     * Determine if Palzin Monitor should record current request.
     *
     * @param \Illuminate\Http\Request $request
     * @return bool
     */
    protected function shouldRecorded($request): bool
    {
        return $request->userAgent() === 'Reliable User Agent';
    }
}

If the method return true the web transaction will be recorded in your dashboard, if it return false the transaction will be completely ignored.

x> Remember to attach your new PalzinFilterMonitoringMiddleware class to the App\Http\Kernel:class instead of the default Palzin Monitor middleware.

Hide sensible contents

You may hide sensible data from HTTP reuqests body like passwords. Palzin Monitor is able to detect that parameters in your request's body masking their content with a simple "******".

Simply add fields to the hidden_parameters array in the in Palzin Monitor config file:

'hidden_parameters' => [
    'password',
    'password_confirmation',
    
    // Other fields here...
],

You can specify nested fields using the dot notation like user.password

Identify Controller@Method execution

If you prefer to have a clear visibility of what controller and method are executed during an HTTP request instead of the request path, 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 Palzin Monitor reports transactions in your dashboard.
     */
    Event::listen(RouteMatched::class, function(RouteMatched $event) {
        $name = $event->route->getActionName();
    
        if(ultimate()->hasTransaction()) {
            ultimate()->currentTransaction()->name = $name;
        } else {
            ultimate()->startTransaction($name);
        }
    });
}

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

Last updated: 1 year ago

Want to get started with Palzin Monitor? We offer a no-strings-attached
15 days trial. No credit card required.

It takes less than a minutes to setup your first monitoring.