Skip to content

A Laravel package that provides configurable application query capturing & monitoring.

License

Notifications You must be signed in to change notification settings

YorCreative/Laravel-Query-Watcher

Repository files navigation



Laravel Query Watcher

GitHub stars GitHub issues GitHub forks PHPUnit

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.

[
    // Do you want to capture queries?
    'enabled' => env('QUERY_WATCH_ENABLED', true), 
    
    // Token used for Authenticating Private Broadcast Channel
    'token' => env('QUERY_WATCH_TOKEN', 'change_me'), 
    'scope' => [
        'time_exceeds_ms' => [
            // Do you want to capture everything or only slow queries?
            'enabled' => env('QUERY_WATCH_SCOPE_TIME_ENABLED', true), 
            
            // The number of milliseconds it took to execute the query.
            'threshold' => env('QUERY_WATCH_SCOPE_TIME_THRESHOLD', 500), 
        ],
        'context' => [
            'auth_user' => [
                // Do you want to know context of the authenticated user when query is captured?
                'enabled' => env('QUERY_WATCH_SCOPE_CONTEXT_AUTH_ENABLED', true),
                 
                // 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.
                'ttl' => env('QUERY_WATCH_SCOPE_CONTEXT_AUTH_TTL', 300), 
            ],
            'trigger' => [
                // Do you want to know what triggered the query?
                // i.e Console command or Request
                'enabled' => env('QUERY_WATCH_SCOPE_TRIGGER_ENABLED', true), 
            ],
        ],
        'ignorable_tables' => [
            // 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 potentially get infinite capture loops.
            'jobs',
            'failed_jobs'
        ],
        'ignorable_statements' => [
            // Do you want to ignore specific SQL statements?
            'create' 
        ]
    ],
    'listener' => [ 
        // Channel notifications are queued
        // Define what connection to use.
        'connection' => 'sync',
        
        //  Define what queue to use 
        'queue' => 'default',
        
        // Do you want to delay the notifications at all? 
        'delay' => null, 
    ],
    'channels' => [ // Where to send notifications?
        'discord' => [ 
            // Do you want discord webhook notifications?
            'enabled' => env('QUERY_WATCH_CHANNEL_DISCORD_ENABLED', false),
            
            // Discord Web-hook URL 
            'hook' => env('DISCORD_HOOK', 'please_fill_me_in'), 
        ],
        'slack' => [ 
            // Do you want Slack webhook notifications?
            'enabled' => env('QUERY_WATCH_CHANNEL_SLACK_ENABLED', false),
            
            // Slack Web-hook URL
            'hook' => env('SLACK_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';
    }

Slack Notification Channel

To utilize Slack Notifications, you will need to create a webhook for one of your Slack Channels. Once you have your webhook url, add the following variable to your .env file.

SLACK_HOOK=<hook>

Once you have done this, you can enable Slack Notifications in the configuration file.

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=<hook>

Once you have done this, you can enable Discord Notifications in the configuration file.

Wiki Documentation

Testing

composer test

Credits