From ddc7d60ab8d8366dfea08fbad4a5ebfc07000a60 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 11:15:38 +0000 Subject: [PATCH 1/7] Install Laravel Pulse --- composer.json | 3 +- composer.lock | 142 ++++++++++++++++++++++++++++- config/pulse.php | 232 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 config/pulse.php diff --git a/composer.json b/composer.json index f215724c9..93a7c26cb 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "laravel-notification-channels/twitter": "^8.0", "laravel/framework": "^10.0", "laravel/horizon": "^5.12", + "laravel/pulse": "^1.0@beta", "laravel/sanctum": "^3.2", "laravel/socialite": "^5.6", "laravel/tinker": "^2.8", @@ -98,6 +99,6 @@ "php-http/discovery": true } }, - "minimum-stability": "stable", + "minimum-stability": "beta", "prefer-stable": true } diff --git a/composer.lock b/composer.lock index 7be706475..6ec3c7662 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6bc023008bb9338e03cc811733726d1b", + "content-hash": "aa8aa5fdbefcb23df8f63f901b603642", "packages": [ { "name": "abraham/twitteroauth", @@ -1525,6 +1525,58 @@ ], "time": "2024-02-05T11:56:58+00:00" }, + { + "name": "doctrine/sql-formatter", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/doctrine/sql-formatter.git", + "reference": "25a06c7bf4c6b8218f47928654252863ffc890a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/sql-formatter/zipball/25a06c7bf4c6b8218f47928654252863ffc890a5", + "reference": "25a06c7bf4c6b8218f47928654252863ffc890a5", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4" + }, + "bin": [ + "bin/sql-formatter" + ], + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\SqlFormatter\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeremy Dorn", + "email": "jeremy@jeremydorn.com", + "homepage": "https://jeremydorn.com/" + } + ], + "description": "a PHP SQL highlighting library", + "homepage": "https://github.com/doctrine/sql-formatter/", + "keywords": [ + "highlight", + "sql" + ], + "support": { + "issues": "https://github.com/doctrine/sql-formatter/issues", + "source": "https://github.com/doctrine/sql-formatter/tree/1.1.3" + }, + "time": "2022-05-23T21:33:49+00:00" + }, { "name": "dragonmantank/cron-expression", "version": "v3.3.3", @@ -2924,6 +2976,92 @@ }, "time": "2024-02-21T19:25:27+00:00" }, + { + "name": "laravel/pulse", + "version": "v1.0.0-beta12", + "source": { + "type": "git", + "url": "https://github.com/laravel/pulse.git", + "reference": "fa9aedb2de38360412b1f7843c33a8142ec9901c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pulse/zipball/fa9aedb2de38360412b1f7843c33a8142ec9901c", + "reference": "fa9aedb2de38360412b1f7843c33a8142ec9901c", + "shasum": "" + }, + "require": { + "doctrine/sql-formatter": "^1.1", + "guzzlehttp/promises": "^1.0|^2.0", + "illuminate/auth": "^10.34|^11.0", + "illuminate/cache": "^10.34|^11.0", + "illuminate/config": "^10.34|^11.0", + "illuminate/console": "^10.34|^11.0", + "illuminate/contracts": "^10.34|^11.0", + "illuminate/database": "^10.34|^11.0", + "illuminate/events": "^10.34|^11.0", + "illuminate/http": "^10.34|^11.0", + "illuminate/queue": "^10.34|^11.0", + "illuminate/redis": "^10.34|^11.0", + "illuminate/routing": "^10.34|^11.0", + "illuminate/support": "^10.34|^11.0", + "illuminate/view": "^10.34|^11.0", + "livewire/livewire": "^3.2", + "nesbot/carbon": "^2.67|^3.0", + "php": "^8.1" + }, + "conflict": { + "nunomaduro/collision": "<7.7.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^7.7", + "mockery/mockery": "^1.0", + "orchestra/testbench": "^8.16|^9.0", + "pestphp/pest": "^2.0", + "pestphp/pest-plugin-laravel": "^2.2", + "phpstan/phpstan": "^1.11", + "predis/predis": "^1.0|^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Pulse\\PulseServiceProvider" + ], + "aliases": { + "Pulse": "Laravel\\Pulse\\Facades\\Pulse" + } + } + }, + "autoload": { + "psr-4": { + "Laravel\\Pulse\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.", + "homepage": "https://github.com/laravel/pulse", + "keywords": [ + "laravel" + ], + "support": { + "issues": "https://github.com/laravel/pulse/issues", + "source": "https://github.com/laravel/pulse" + }, + "time": "2024-02-13T14:50:59+00:00" + }, { "name": "laravel/sanctum", "version": "v3.3.3", @@ -12625,7 +12763,7 @@ } ], "aliases": [], - "minimum-stability": "stable", + "minimum-stability": "beta", "stability-flags": [], "prefer-stable": true, "prefer-lowest": false, diff --git a/config/pulse.php b/config/pulse.php new file mode 100644 index 000000000..9defa3a92 --- /dev/null +++ b/config/pulse.php @@ -0,0 +1,232 @@ + env('PULSE_DOMAIN'), + + /* + |-------------------------------------------------------------------------- + | Pulse Path + |-------------------------------------------------------------------------- + | + | This is the path which the Pulse dashboard will be accessible from. Feel + | free to change this path to anything you'd like. Note that this won't + | affect the path of the internal API that is never exposed to users. + | + */ + + 'path' => env('PULSE_PATH', 'pulse'), + + /* + |-------------------------------------------------------------------------- + | Pulse Master Switch + |-------------------------------------------------------------------------- + | + | This configuration option may be used to completely disable all Pulse + | data recorders regardless of their individual configurations. This + | provides a single option to quickly disable all Pulse recording. + | + */ + + 'enabled' => env('PULSE_ENABLED', true), + + /* + |-------------------------------------------------------------------------- + | Pulse Storage Driver + |-------------------------------------------------------------------------- + | + | This configuration option determines which storage driver will be used + | while storing entries from Pulse's recorders. In addition, you also + | may provide any options to configure the selected storage driver. + | + */ + + 'storage' => [ + 'driver' => env('PULSE_STORAGE_DRIVER', 'database'), + + 'database' => [ + 'connection' => env('PULSE_DB_CONNECTION', 'pulse'), + 'chunk' => 1000, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Pulse Ingest Driver + |-------------------------------------------------------------------------- + | + | This configuration options determines the ingest driver that will be used + | to capture entries from Pulse's recorders. Ingest drivers are great to + | free up your request workers quickly by offloading the data storage. + | + */ + + 'ingest' => [ + 'driver' => env('PULSE_INGEST_DRIVER', 'storage'), + + 'buffer' => env('PULSE_INGEST_BUFFER', 5_000), + + 'trim' => [ + 'lottery' => [1, 1_000], + 'keep' => '7 days', + ], + + 'redis' => [ + 'connection' => env('PULSE_REDIS_CONNECTION'), + 'chunk' => 1000, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Pulse Cache Driver + |-------------------------------------------------------------------------- + | + | This configuration option determines the cache driver that will be used + | for various tasks, including caching dashboard results, establishing + | locks for events that should only occur on one server and signals. + | + */ + + 'cache' => env('PULSE_CACHE_DRIVER'), + + /* + |-------------------------------------------------------------------------- + | Pulse Route Middleware + |-------------------------------------------------------------------------- + | + | These middleware will be assigned to every Pulse route, giving you the + | chance to add your own middleware to this list or change any of the + | existing middleware. Of course, reasonable defaults are provided. + | + */ + + 'middleware' => [ + 'web', + Authorize::class, + ], + + /* + |-------------------------------------------------------------------------- + | Pulse Recorders + |-------------------------------------------------------------------------- + | + | The following array lists the "recorders" that will be registered with + | Pulse, along with their configuration. Recorders gather application + | event data from requests and tasks to pass to your ingest driver. + | + */ + + 'recorders' => [ + Recorders\CacheInteractions::class => [ + 'enabled' => env('PULSE_CACHE_INTERACTIONS_ENABLED', true), + 'sample_rate' => env('PULSE_CACHE_INTERACTIONS_SAMPLE_RATE', 1), + 'ignore' => [ + ...Pulse::defaultVendorCacheKeys(), + ], + 'groups' => [ + '/^job-exceptions:.*/' => 'job-exceptions:*', + // '/:\d+/' => ':*', + ], + ], + + Recorders\Exceptions::class => [ + 'enabled' => env('PULSE_EXCEPTIONS_ENABLED', true), + 'sample_rate' => env('PULSE_EXCEPTIONS_SAMPLE_RATE', 1), + 'location' => env('PULSE_EXCEPTIONS_LOCATION', true), + 'ignore' => [ + // '/^Package\\\\Exceptions\\\\/', + ], + ], + + Recorders\Queues::class => [ + 'enabled' => env('PULSE_QUEUES_ENABLED', true), + 'sample_rate' => env('PULSE_QUEUES_SAMPLE_RATE', 1), + 'ignore' => [ + // '/^Package\\\\Jobs\\\\/', + ], + ], + + Recorders\Servers::class => [ + 'server_name' => env('PULSE_SERVER_NAME', gethostname()), + 'directories' => explode(':', env('PULSE_SERVER_DIRECTORIES', '/')), + ], + + Recorders\SlowJobs::class => [ + 'enabled' => env('PULSE_SLOW_JOBS_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_JOBS_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_JOBS_THRESHOLD', 1000), + 'ignore' => [ + // '/^Package\\\\Jobs\\\\/', + ], + ], + + Recorders\SlowOutgoingRequests::class => [ + 'enabled' => env('PULSE_SLOW_OUTGOING_REQUESTS_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_OUTGOING_REQUESTS_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_OUTGOING_REQUESTS_THRESHOLD', 1000), + 'ignore' => [ + // '#^http://127\.0\.0\.1:13714#', // Inertia SSR... + ], + 'groups' => [ + // '#^https://api\.github\.com/repos/.*$#' => 'api.github.com/repos/*', + // '#^https?://([^/]*).*$#' => '\1', + // '#/\d+#' => '/*', + ], + ], + + Recorders\SlowQueries::class => [ + 'enabled' => env('PULSE_SLOW_QUERIES_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_QUERIES_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_QUERIES_THRESHOLD', 1000), + 'location' => env('PULSE_SLOW_QUERIES_LOCATION', true), + 'highlighting' => env('PULSE_SLOW_QUERIES_HIGHLIGHTING', true), + 'ignore' => [ + '/(["`])pulse_[\w]+?\1/', // Pulse tables... + '/(["`])telescope_[\w]+?\1/', // Telescope tables... + ], + ], + + Recorders\SlowRequests::class => [ + 'enabled' => env('PULSE_SLOW_REQUESTS_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_REQUESTS_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_REQUESTS_THRESHOLD', 1000), + 'ignore' => [ + '#^/pulse$#', // Pulse dashboard... + '#^/telescope#', // Telescope dashboard... + ], + ], + + Recorders\UserJobs::class => [ + 'enabled' => env('PULSE_USER_JOBS_ENABLED', true), + 'sample_rate' => env('PULSE_USER_JOBS_SAMPLE_RATE', 1), + 'ignore' => [ + // '/^Package\\\\Jobs\\\\/', + ], + ], + + Recorders\UserRequests::class => [ + 'enabled' => env('PULSE_USER_REQUESTS_ENABLED', true), + 'sample_rate' => env('PULSE_USER_REQUESTS_SAMPLE_RATE', 1), + 'ignore' => [ + '#^/pulse$#', // Pulse dashboard... + '#^/telescope#', // Telescope dashboard... + ], + ], + ], +]; From 562cb5dcc328b924b7736033532e598fa8231be4 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 11:17:01 +0000 Subject: [PATCH 2/7] Add pulse database connection --- config/database.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/config/database.php b/config/database.php index 137ad18ce..35c0862c4 100644 --- a/config/database.php +++ b/config/database.php @@ -93,6 +93,25 @@ // 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'), ], + 'pulse' => [ + 'driver' => 'mysql', + 'url' => env('PULSE_DATABASE_URL'), + 'host' => env('PULSE_DB_HOST', '127.0.0.1'), + 'port' => env('PULSE_DB_PORT', '3306'), + 'database' => env('PULSE_DB_DATABASE', 'pulse'), + 'username' => env('PULSE_DB_USERNAME', 'pulse'), + 'password' => env('PULSE_DB_PASSWORD', ''), + 'unix_socket' => env('PULSE_DB_SOCKET', ''), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', + 'prefix_indexes' => true, + 'strict' => true, + 'engine' => null, + 'options' => extension_loaded('pdo_mysql') ? array_filter([ + PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), + ]) : [], + ], ], /* From 9fd97dd7f37fd8bcacc671a438ece41e5b6838b4 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 11:18:45 +0000 Subject: [PATCH 3/7] Add pulse database migration --- .../2023_06_07_000001_create_pulse_tables.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 database/migrations/2023_06_07_000001_create_pulse_tables.php diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php new file mode 100644 index 000000000..f75b51e49 --- /dev/null +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -0,0 +1,84 @@ +shouldRun()) { + return; + } + + Schema::create('pulse_values', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('timestamp'); + $table->string('type'); + $table->mediumText('key'); + match ($this->driver()) { + 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), + 'sqlite' => $table->string('key_hash'), + }; + $table->mediumText('value'); + + $table->index('timestamp'); // For trimming... + $table->index('type'); // For fast lookups and purging... + $table->unique(['type', 'key_hash']); // For data integrity and upserts... + }); + + Schema::create('pulse_entries', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('timestamp'); + $table->string('type'); + $table->mediumText('key'); + match ($this->driver()) { + 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), + 'sqlite' => $table->string('key_hash'), + }; + $table->bigInteger('value')->nullable(); + + $table->index('timestamp'); // For trimming... + $table->index('type'); // For purging... + $table->index('key_hash'); // For mapping... + $table->index(['timestamp', 'type', 'key_hash', 'value']); // For aggregate queries... + }); + + Schema::create('pulse_aggregates', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('bucket'); + $table->unsignedMediumInteger('period'); + $table->string('type'); + $table->mediumText('key'); + match ($this->driver()) { + 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), + 'sqlite' => $table->string('key_hash'), + }; + $table->string('aggregate'); + $table->decimal('value', 20, 2); + $table->unsignedInteger('count')->nullable(); + + $table->unique(['bucket', 'period', 'type', 'aggregate', 'key_hash']); // Force "on duplicate update"... + $table->index(['period', 'bucket']); // For trimming... + $table->index('type'); // For purging... + $table->index(['period', 'type', 'aggregate', 'bucket']); // For aggregate queries... + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('pulse_values'); + Schema::dropIfExists('pulse_entries'); + Schema::dropIfExists('pulse_aggregates'); + } +}; From 270fdf3da9fc6751e320d957585fc9e03bc86fd2 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 11:20:11 +0000 Subject: [PATCH 4/7] Fix md directive conflict with pulse class name @md --- resources/macros/blade.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/resources/macros/blade.php b/resources/macros/blade.php index 1d82d40ef..7a1c31617 100644 --- a/resources/macros/blade.php +++ b/resources/macros/blade.php @@ -3,6 +3,11 @@ use Illuminate\Support\Facades\Blade; Blade::directive('md', function ($expression) { + + if (empty($expression)) { + return ""; + } + return ""; }); From 01f739d00088d385fa34c05ff3d62d144b7e7e48 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 12:08:13 +0000 Subject: [PATCH 5/7] Add Pulse authorization --- app/Providers/AuthServiceProvider.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 1364a8637..6e2150f47 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -13,6 +13,7 @@ use App\Policies\UserPolicy; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Notifications\DatabaseNotification as Notification; +use Illuminate\Support\Facades\Gate; class AuthServiceProvider extends ServiceProvider { @@ -26,5 +27,8 @@ class AuthServiceProvider extends ServiceProvider public function boot(): void { + Gate::define('viewPulse', function (User $user) { + return $user->isAdmin(); + }); } } From 6834e878c1c568dc27382651015d62b5df518661 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 22:04:39 +0000 Subject: [PATCH 6/7] Disable pulse in Unit Test --- .../migrations/2023_06_07_000001_create_pulse_tables.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php index f75b51e49..2666a0f98 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -1,6 +1,7 @@ shouldRun()) { + if (! $this->shouldRun() || ! Config::get('pulse.enabled')) { return; } @@ -77,6 +78,10 @@ public function up(): void */ public function down(): void { + if (! $this->shouldRun() || ! Config::get('pulse.enabled')) { + return; + } + Schema::dropIfExists('pulse_values'); Schema::dropIfExists('pulse_entries'); Schema::dropIfExists('pulse_aggregates'); From 749f1989af9e444ea85389257255e80f9d732c48 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 20 Feb 2024 22:14:39 +0000 Subject: [PATCH 7/7] Disable pulse in Unit Test --- phpunit.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml b/phpunit.xml index 66d1728d8..e353914af 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -30,5 +30,6 @@ +