Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: sample rate added #1516

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 24 additions & 1 deletion src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;
use Illuminate\Support\Testing\Fakes\EventFake;
use Laravel\Telescope\Contracts\EntriesRepository;
Expand Down Expand Up @@ -120,6 +121,28 @@ 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')) {
$telescope_enabled = false;
}
else if (config('telescope.sample_rate') < rand(0, 100)) {
return $telescope_enabled = false;
}
Context::add('telescope_enabled', $telescope_enabled);

return $telescope_enabled;
}

/**
* Register the Telescope watchers and start recording if necessary.
*
Expand All @@ -128,7 +151,7 @@ class Telescope
*/
public static function start($app)
{
if (! config('telescope.enabled')) {
if (! static::isEnabled()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/TelescopeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function boot()
$this->registerCommands();
$this->registerPublishing();

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

Expand Down
Loading