Laravel Query Watcher
A Laravel package that provides configurable application query capturing & monitoring.
Installation
install the package via composer:
composer require YorCreative/Laravel-Query-Watcher
Publish the packages assets.
php artisan vendor:publish --provider="YorCreative\QueryWatcher\QueryWatcherServiceProvider"
Usage
Configuration
Adjust the configuration file to suite your application.
[
'enabled' => true, // Do you want to capture queries?
'token' => env('QUERY_WATCH_TOKEN', 'change_me'), // Token used for Authenticating Private Broadcast Channel
'scope' => [
'time_exceeds_ms' => [
'enabled' => true, // Do you want to capture everything or only slow queries?
'threshold' => 500, // The number of milliseconds it took to execute the query.
],
'context' => [
'auth_user' => [
'enabled' => true, // Do you want to know context of the authenticated user when query is captured?
'ttl' => 300, // How long do you want the session_id/authenticated user cached for?
// without this cache, your application will infinite loop because it will capture
// the user query and loop.
// See closed Issue #1 for context.
],
'trigger' => [
'enabled' => true, // Do you want to know what triggered the query?
// i.e Console command or Request
],
],
'ignorable_tables' => [
'jobs' // Do you want to capture queries on specific tables?
// If you are utilizing the database queue driver, you need to
// ignore the jobs table or you'll get infinite capture loops.
],
'ignorable_statements' => [
'create' // Do you want to ignore specific SQL statements?
]
],
'listener' => [ // Channel notifications are queued
'connection' => 'sync', // Define what connection to use.
'queue' => 'default', // Define what queue to use
'delay' => null, // Do you want to delay the notifications at all?
],
'channels' => [ // Where to send notifications?
'discord' => [
'enabled' => false, // Do you want discord webhook notifications?
'hook' => env('DISCORD_HOOK', 'please_fill_me_in'),
],
]
]
Broadcasting
All captured queries will broadcast on a private channel as the primary monitoring method. The QueryEvent that is broadcasting is using your applications broadcast configuration.
/**
* Get the channels the event should broadcast on.
*
* @return PrivateChannel
*/
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel('query.event.'. config('querywatcher.token'));
}
/**
* @return string
*/
public function broadcastAs(): string
{
return 'query.event';
}
Discord Notification Channel
Get a webhook URL from discord in the channel you want to receive your notifications in by reading Discords Introduction to Webhook Article . Once you have your webhook url, add the following variable to your .env
file.
DISCORD_HOOK=
Once you have done this, you can enable Discord Notifications in the configuration file.
Screenshots
Testing
composer test