Http Requests Monitoring
Monitor your application behaviour during incoming http requests.
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.
Basically you can attach the middleware in the
App\Http\Kernel
class in web
and api
middleware groups, so in two lines of code you'll intercept all incoming http requests:/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
...,
\Inspector\Laravel\Middleware\WebRequestMonitoring::class,
],
'api' => [
...,
\Inspector\Laravel\Middleware\WebRequestMonitoring::class,
]
]
In alternative you can create a middleware key so you can attach it in a specific route or group:
/*
* Create a new middleware key (inspector) in your \App\Http\Kernel class.
*/
protected $routeMiddleware = [
...,
'inspector' => \Inspector\Laravel\Middleware\WebRequestMonitoring::class,
];
Use "inspector" middleware key in you routes:
/*
* Attach the "inspector" middleware in your routes
*/
Route::middleware('inspector')->group(function () {
...
});
If you need to implement a custom logic to ignore requests, the middleware is also designed to be extended.
Overwriting the
shouldRecorded()
method, you can implement a custom condition to decide which routes should be ignored by Inspector. Run the artisan command below to create a new middleware class:
php artisan make:middleware InspectorFilterMonitoringMiddleware
In the new middleware class extend the Inspector middleware and override the
shouldRecorded()
method to implement your condition. Return back a boolean value to enable or disable monitoring:<?php
use \Inspector\Laravel\Middleware\WebRequestMonitoring;
class InspectorFilterMonitoringMiddleware extends WebRequestMonitoring
{
/**
* Determine if Inspector should record current request.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function shouldRecorded($request): bool
{
// Only requests from your IP addresses should be recorded
return in_array($request->ip(), ['xxx.xxx.xxx.xxx']);
}
}
Returning
true
the current http request will be recorded in your dashboard, returning false
the transaction will be completely ignored.The last step is to use this new custom middleware in the
App\Http\Kernel
class instead of the original Inspector middleware:use App\Http\Middleware\InspectorFilterMonitoringMiddleware;
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
...,
//\Inspector\Laravel\Middleware\WebRequestMonitoring::class,
InspectorFilterMonitoringMiddleware::class,
],
'api' => [
...,
//\Inspector\Laravel\Middleware\WebRequestMonitoring::class,
InspectorFilterMonitoringMiddleware::class,
]
]
You may hide sensible data from HTTP requests body like passwords. Inspector 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 inspector config file:'hidden_parameters' => [
'password',
'password_confirmation',
// Other fields here...
],
You can specify nested fields using the dot notation like
user.password
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 inspector reports transactions in your dashboard.
*/
Event::listen(RouteMatched::class, function(RouteMatched $event) {
$name = $event->route->getActionName();
if(inspector()->hasTransaction()) {
inspector()->transaction()->name = $name;
} else {
inspector()->startTransaction($name);
}
});
}
This will change the way Inspector reports transactions in your dashboard from
GET /path/{id}
to YourController@yourMethod
.Last modified 3mo ago