Skip to content

Commit

Permalink
✨ feat: sample rate added
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammed Hussein Karimi <[email protected]>
  • Loading branch information
mhkarimi1383 committed Sep 8, 2024
1 parent 87ad880 commit 264474e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
12 changes: 12 additions & 0 deletions config/telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -65,7 +66,7 @@ public function show(EntriesRepository $storage, $id)
*/
protected function status()
{
if (! config('telescope.enabled', false)) {
if (! Telescope::isEnabled()) {
return 'disabled';
}

Expand Down
24 changes: 23 additions & 1 deletion src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -128,7 +150,7 @@ class Telescope
*/
public static function start($app)
{
if (! config('telescope.enabled')) {
if (! static::isEnabled()) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/TelescopeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,7 +22,7 @@ public function boot()
$this->registerCommands();
$this->registerPublishing();

if (! config('telescope.enabled')) {
if (! Telescope::isEnabled()) {
return;
}

Expand Down

0 comments on commit 264474e

Please sign in to comment.