Blog Detail

30

Aug
Configurable App Query Capturing & Monitoring in Laravel cover image

arrow_back Configurable App Query Capturing & Monitoring in Laravel

Laravel Query Watcher is a Laravel package that provides configurable application query capturing & monitoring.

Installation

install the package via composer:

composer require YorCreative/Laravel-Query-Watcher

Publish the package assets.

php artisan vendor:publish --provider="YorCreative\QueryWatcher\QueryWatcherServiceProvider"

Usage

Configuration

Adjust the configuration file to suit 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
            ],
        ],
    ],
    '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 be broadcast on a private channel as the primary monitoring method. The QueryEvent that is broadcasting is using your application’s 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=<hook>

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

For more details, Visit Github.

Published at : 30-08-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project