Skip to content

Commit

Permalink
Refactor Telescope configuration classes (#505)
Browse files Browse the repository at this point in the history
* Refactor Telescope configuration classes

* Refactor request handling and server setup in Telescope

* Update Telescope configuration and usage

* Remove unused import and empty line in LogAspect.php and EntryController.php

* Optimize code

---------

Co-authored-by: Deeka Wong <[email protected]>
Co-authored-by: guandeng <[email protected]>
  • Loading branch information
3 people committed Dec 21, 2023
1 parent 11c8e08 commit bfbfa99
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 158 deletions.
5 changes: 2 additions & 3 deletions migrations/2020_08_03_064816_telescope_entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/
use FriendsOfHyperf\Telescope\Telescope;
use Hyperf\Database\Migrations\Migration;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Schema\Schema;

use function Hyperf\Config\config;

class TelescopeEntries extends Migration
{
/**
Expand Down Expand Up @@ -68,6 +67,6 @@ public function down(): void
*/
public function getConnection(): string
{
return config('telescope.database.connection', 'default');
return Telescope::getConfig()->getDatabaseConnection();
}
}
2 changes: 1 addition & 1 deletion publish/telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
// 'api/*'
],
'ignore_paths' => [
'nova-api*',
// 'nova-api*',
],
];
20 changes: 11 additions & 9 deletions src/Aspect/CacheAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Cache\CacheManager;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\PackerInterface;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Stringable\Str;

use function Hyperf\Config\config;
use function Hyperf\Tappable\tap;

/**
Expand All @@ -36,8 +36,10 @@ class CacheAspect extends AbstractAspect
'Hyperf\Cache\Driver\*Driver::set',
];

public function __construct(protected SwitchManager $switcherManager)
{
public function __construct(
protected ConfigInterface $config,
protected TelescopeConfig $telescopeConfig
) {
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
Expand All @@ -57,7 +59,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
protected function processGetDriver($proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($driver) use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('redis')) {
if (! $this->telescopeConfig->isEnable('redis')) {
return;
}

Expand All @@ -69,7 +71,7 @@ protected function processGetDriver($proceedingJoinPoint)
protected function processDriverFetch($proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('cache')) {
if (! $this->telescopeConfig->isEnable('cache')) {
return;
}

Expand All @@ -86,7 +88,7 @@ protected function processDriverFetch($proceedingJoinPoint)
protected function processDriverGet($proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('cache')) {
if (! $this->telescopeConfig->isEnable('cache')) {
return;
}

Expand All @@ -102,7 +104,7 @@ protected function processDriverGet($proceedingJoinPoint)
protected function processDriverSet($proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function () use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('cache')) {
if (! $this->telescopeConfig->isEnable('cache')) {
return;
}

Expand All @@ -118,6 +120,6 @@ protected function processDriverSet($proceedingJoinPoint)
protected function getCacheKey(string $key): string
{
$driver = TelescopeContext::getCacheDriver();
return config('cache.' . $driver . '.prefix', '') . $key;
return $this->config->get('cache.' . $driver . '.prefix', '') . $key;
}
}
6 changes: 3 additions & 3 deletions src/Aspect/EventAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Event\EventDispatcher;
Expand All @@ -30,14 +30,14 @@ class EventAspect extends AbstractAspect
EventDispatcher::class . '::dispatch',
];

public function __construct(protected SwitchManager $switcherManager, protected ListenerProviderInterface $listeners)
public function __construct(protected TelescopeConfig $telescopeConfig, protected ListenerProviderInterface $listeners)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('event')) {
if (! $this->telescopeConfig->isEnable('event')) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Aspect/GrpcClientAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand All @@ -24,7 +24,7 @@ class GrpcClientAspect extends AbstractAspect
GrpcClient::class . '::send',
];

public function __construct(protected SwitchManager $switcherManager)
public function __construct(protected TelescopeConfig $telescopeConfig)
{
}

Expand All @@ -38,7 +38,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)

private function processSend(ProceedingJoinPoint $proceedingJoinPoint)
{
if ($this->switcherManager->isEnable('grpc')) {
if ($this->telescopeConfig->isEnable('grpc')) {
$carrier = [];
$carrier['batch-id'] = TelescopeContext::getBatchId();
/** @var Request $request */
Expand Down
6 changes: 3 additions & 3 deletions src/Aspect/HttpClientAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use GuzzleHttp\Client;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand All @@ -29,13 +29,13 @@ class HttpClientAspect extends AbstractAspect
Client::class . '::requestAsync',
];

public function __construct(protected SwitchManager $switcherManager)
public function __construct(protected TelescopeConfig $telescopeConfig)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (! $this->switcherManager->isEnable('guzzle')) {
if (! $this->telescopeConfig->isEnable('guzzle')) {
return $proceedingJoinPoint->process();
}
$startTime = microtime(true);
Expand Down
10 changes: 4 additions & 6 deletions src/Aspect/LogAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\Severity;
use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Stringable\Str;
use Monolog\Logger;
use UnitEnum;

use function Hyperf\Config\config;
use function Hyperf\Tappable\tap;

class LogAspect extends AbstractAspect
Expand All @@ -30,14 +29,14 @@ class LogAspect extends AbstractAspect
Logger::class . '::addRecord',
];

public function __construct(protected SwitchManager $switcherManager)
public function __construct(protected TelescopeConfig $telescopeConfig)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('log')) {
if (! $this->telescopeConfig->isEnable('log')) {
return;
}
$level = $proceedingJoinPoint->arguments['keys']['level'];
Expand All @@ -48,8 +47,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
return;
}
$name = $proceedingJoinPoint->getInstance()->getName();
$ignoreLogs = config('telescope.ignore_logs', []);
if ($ignoreLogs && in_array($name, $ignoreLogs)) {
if ($this->telescopeConfig->isLogIgnored($name)) {
return;
}
Telescope::recordLog(
Expand Down
15 changes: 9 additions & 6 deletions src/Aspect/RedisAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\PackerInterface;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand All @@ -23,7 +24,6 @@
use Throwable;

use function Hyperf\Collection\collect;
use function Hyperf\Config\config;
use function Hyperf\Tappable\tap;

/**
Expand All @@ -36,15 +36,18 @@ class RedisAspect extends AbstractAspect
Redis::class . '::__call',
];

public function __construct(protected SwitchManager $switcherManager, protected ContainerInterface $container)
{
public function __construct(
protected ContainerInterface $container,
protected ConfigInterface $config,
protected TelescopeConfig $telescopeConfig,
) {
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$startTime = microtime(true);
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint, $startTime) {
if (! $this->switcherManager->isEnable('redis')) {
if (! $this->telescopeConfig->isEnable('redis')) {
return;
}

Expand Down Expand Up @@ -80,7 +83,7 @@ private function formatCommand(string $command, array $parameters): string
&& $key == 1
&& $driver = TelescopeContext::getCacheDriver()
) {
$packer = config('cache.' . $driver . '.packer', '');
$packer = $this->config->get('cache.' . $driver . '.packer', '');
$packer = $this->container->get($packer);
if ($packer instanceof PackerInterface) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/Aspect/RequestDispatcherAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand All @@ -27,14 +27,14 @@ class RequestDispatcherAspect extends AbstractAspect
RequestDispatcher::class . '::dispatch',
];

public function __construct(protected SwitchManager $switcherManager)
public function __construct(protected TelescopeConfig $telescopeConfig)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function () use ($proceedingJoinPoint) {
if (! $this->switcherManager->isEnable('request')) {
if (! $this->telescopeConfig->isEnable('request')) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Aspect/RpcAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace FriendsOfHyperf\Telescope\Aspect;

use FriendsOfHyperf\Telescope\SwitchManager;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand All @@ -26,14 +26,14 @@ class RpcAspect extends AbstractAspect
AbstractServiceClient::class . '::__generateRpcPath',
];

public function __construct(protected SwitchManager $switcherManager, protected Context $context)
public function __construct(protected TelescopeConfig $telescopeConfig, protected Context $context)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function () {
if (static::class == self::class && $this->switcherManager->isEnable('rpc') === false) {
if (static::class == self::class && $this->telescopeConfig->isEnable('rpc') === false) {
return;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Command/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

namespace FriendsOfHyperf\Telescope\Command;

use FriendsOfHyperf\Telescope\Telescope;
use Hyperf\Command\Command;
use Hyperf\DbConnection\Db;

use function Hyperf\Config\config;

class ClearCommand extends Command
{
protected ?string $signature = 'telescope:clear';

public function handle()
{
$connection = config('telescope.database.connection');
$connection = Telescope::getConfig()->getDatabaseConnection();
Db::connection($connection)->table('telescope_entries')->delete();
Db::connection($connection)->table('telescope_entries_tags')->delete();
Db::connection($connection)->table('telescope_monitoring')->delete();
Expand Down
5 changes: 2 additions & 3 deletions src/Command/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
namespace FriendsOfHyperf\Telescope\Command;

use Carbon\Carbon;
use FriendsOfHyperf\Telescope\Telescope;
use Hyperf\Command\Command;
use Hyperf\DbConnection\Db;

use function Hyperf\Config\config;

class PruneCommand extends Command
{
protected ?string $signature = 'telescope:prune {--hours=24 : The number of hours to retain Telescope data}';

public function handle()
{
$connection = config('telescope.database.connection');
$connection = Telescope::getConfig()->getDatabaseConnection();
$created_at = Carbon::now()->subHours($this->input->getOption('hours'));
Db::connection($connection)->table('telescope_entries')
->where('created_at', '<', $created_at)
Expand Down
14 changes: 0 additions & 14 deletions src/Controller/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,6 @@ abstract protected function watcher();
*/
protected function status()
{
// if (! config('telescope.enabled', false)) {
// return 'disabled';
// }

// if (cache('telescope:pause-recording', false)) {
// return 'paused';
// }

// $watcher = config('telescope.watchers.'.$this->watcher());

// if (! $watcher || (isset($watcher['enabled']) && ! $watcher['enabled'])) {
// return 'off';
// }

return 'enabled';
}
}
Loading

0 comments on commit bfbfa99

Please sign in to comment.