From 264474ed5c34db8721c338454b2a51871cc952e2 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Sun, 8 Sep 2024 15:38:52 +0330 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20sample=20rate=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- config/telescope.php | 12 ++++++++++++ src/Http/Controllers/EntryController.php | 3 ++- src/Telescope.php | 24 +++++++++++++++++++++++- src/TelescopeServiceProvider.php | 3 ++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/config/telescope.php b/config/telescope.php index dba9e2303..d5ed2dad4 100644 --- a/config/telescope.php +++ b/config/telescope.php @@ -18,6 +18,18 @@ 'enabled' => env('TELESCOPE_ENABLED', true), + /* + |-------------------------------------------------------------------------- + | Telescope Sample Rate + |-------------------------------------------------------------------------- + | + | This option may be used to set sample rate based (between 0 and 100) + | Useful when your application having alot of RPS (Also manages database size) + | + */ + + 'sample_rate' => env('TELESCOPE_SAMPLE_RATE', 100), + /* |-------------------------------------------------------------------------- | Telescope Domain diff --git a/src/Http/Controllers/EntryController.php b/src/Http/Controllers/EntryController.php index 84fd53519..a9f834ca7 100644 --- a/src/Http/Controllers/EntryController.php +++ b/src/Http/Controllers/EntryController.php @@ -6,6 +6,7 @@ use Illuminate\Routing\Controller; use Laravel\Telescope\Contracts\EntriesRepository; use Laravel\Telescope\Storage\EntryQueryOptions; +use Laravel\Telescope\Telescope; abstract class EntryController extends Controller { @@ -65,7 +66,7 @@ public function show(EntriesRepository $storage, $id) */ protected function status() { - if (! config('telescope.enabled', false)) { + if (! Telescope::isEnabled()) { return 'disabled'; } diff --git a/src/Telescope.php b/src/Telescope.php index be99d5b20..8e7d7a05d 100644 --- a/src/Telescope.php +++ b/src/Telescope.php @@ -14,6 +14,7 @@ use Laravel\Telescope\Contracts\EntriesRepository; use Laravel\Telescope\Contracts\TerminableRepository; use Laravel\Telescope\Jobs\ProcessPendingUpdates; +use Illuminate\Support\Facades\Context; use Throwable; class Telescope @@ -120,6 +121,27 @@ class Telescope */ public static $shouldRecord = false; + /** + * Checks if telescope is enabled or not (also uses sample rate config) + * + * @return bool + */ + public static function isEnabled() + { + if (Context::has('telescope_enabled')) { + return Context::get('telescope_enabled'); + } + $telescope_enabled = true; + if (! config('telescope.enabled')) { + return false; + } + if (config('telescope.sample_rate') < rand(0, 100)) { + return false; + } + Context::add("telescope_enabled", $telescope_enabled); + return $telescope_enabled; + } + /** * Register the Telescope watchers and start recording if necessary. * @@ -128,7 +150,7 @@ class Telescope */ public static function start($app) { - if (! config('telescope.enabled')) { + if (! static::isEnabled()) { return; } diff --git a/src/TelescopeServiceProvider.php b/src/TelescopeServiceProvider.php index 9bd067ad5..2b2ca2ddd 100644 --- a/src/TelescopeServiceProvider.php +++ b/src/TelescopeServiceProvider.php @@ -8,6 +8,7 @@ use Laravel\Telescope\Contracts\EntriesRepository; use Laravel\Telescope\Contracts\PrunableRepository; use Laravel\Telescope\Storage\DatabaseEntriesRepository; +use Laravel\Telescope\Telescope; class TelescopeServiceProvider extends ServiceProvider { @@ -21,7 +22,7 @@ public function boot() $this->registerCommands(); $this->registerPublishing(); - if (! config('telescope.enabled')) { + if (! Telescope::isEnabled()) { return; }