Skip to content

Commit

Permalink
Added Telescope recording and pause functions (#678)
Browse files Browse the repository at this point in the history
* Support telescope ability

* Support recording

* Optimizing the code

* feat: Refactor telescope recording and caching logic

* chore: Update Psr\Container\ContainerInterface::get() override in .phpstorm.meta.php

* feat: Update EntriesController to use ApplicationInterface for clearing Telescope entries

* feat: Update Psr\Container\ContainerInterface::get() override in .phpstorm.meta.php

* chore: Update Psr\Container\ContainerInterface::get() override in .phpstorm.meta.php

* feat: Update EntriesController to use ApplicationInterface for clearing Telescope entries

* Update Telescope::getCache() to return PsrCacheInterface instead of CacheInterface

* Added `Telescope::isRecording()` function

* Moved `Telescope::isRecording()` condition to `TelescopeConfig isEnable()`

* Optimized

* Optimized

* Update tests

* Remove unused codes

* Optimize Telescope::isRecording() condition

* Optimize Telescope::isRecording() condition

* Optimize Telescope::isRecording() condition

* Optimize Telescope::isRecording() condition and update caching logic

* Optimize Telescope::isRecording() condition and caching logic

* Optimize Telescope::isRecording() condition and caching logic

* Optimize Telescope::isRecording() condition and update caching logic

* chore: Optimize Telescope::isRecording() condition and caching logic

* Optimize Telescope::isRecording() condition and caching logic

* chore: Optimize Telescope::isRecording() condition and caching logic

* Refactor parse recording

* Optimize Telescope::isRecording() condition and caching logic

* chore: Disable Telescope filter for cache entries with 'telescope:' prefix

* Optimized

* chore: Optimize caching logic in Telescope::getCache() method

* Optimize caching logic in CacheAspect.php

* Optimize caching logic in TelescopeConfig.php

* chore: Remove SetupTelescopeFilterListener and optimize caching logic in TelescopeConfig.php and CacheAspect.php

* Optimize caching logic in CacheAspect.php

* Optimize caching logic in CacheAspect.php

* Optimize caching logic in CacheAspect.php

* Optimize caching logic in CacheAspect.php

* chore: Optimize caching logic in TelescopeConfig.php and CacheAspect.php

* chore: Optimize caching logic in TelescopeConfig.php and CacheAspect.php

* optimize code

* optimize code

* optimize code

---------

Co-authored-by: 10951 <[email protected]>
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
3 people authored Jun 23, 2024
1 parent 9d9640c commit 0043cfd
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 47 deletions.
3 changes: 1 addition & 2 deletions src/Aspect/CacheAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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;
Expand All @@ -30,7 +29,7 @@
class CacheAspect extends AbstractAspect
{
public array $classes = [
CacheManager::class . '::getDriver',
'Hyperf\Cache\CacheManager::getDriver',
'Hyperf\Cache\Driver\*Driver::fetch',
'Hyperf\Cache\Driver\*Driver::get',
'Hyperf\Cache\Driver\*Driver::set',
Expand Down
36 changes: 19 additions & 17 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ public function __invoke(): array
defined('BASE_PATH') or define('BASE_PATH', '');

return [
'commands' => [
Command\ClearCommand::class,
Command\InstallCommand::class,
Command\PruneCommand::class,
],
'listeners' => [
Listener\SetRequestLifecycleListener::class,
Listener\CommandListener::class,
Listener\DbQueryListener::class,
Listener\ExceptionHandlerListener::class,
Listener\SetupTelescopeServerListener::class,
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
],
],
'aspects' => [
Aspect\CoroutineAspect::class,
Expand All @@ -42,12 +37,19 @@ public function __invoke(): array
Aspect\RequestDispatcherAspect::class,
Aspect\GrpcCoreMiddlewareAspect::class,
],
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
],
'commands' => [
Command\ClearCommand::class,
Command\InstallCommand::class,
Command\PruneCommand::class,
],
'dependencies' => [
],
'listeners' => [
Listener\SetRequestLifecycleListener::class,
Listener\CommandListener::class,
Listener\DbQueryListener::class,
Listener\ExceptionHandlerListener::class,
Listener\SetupTelescopeServerListener::class,
],
'publish' => [
[
Expand Down
18 changes: 18 additions & 0 deletions src/Contract/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Telescope\Contract;

use Psr\SimpleCache\CacheInterface as PsrCacheInterface;

interface CacheInterface extends PsrCacheInterface
{
}
35 changes: 35 additions & 0 deletions src/Controller/EntriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Telescope\Controller;

use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ApplicationInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Symfony\Component\Console\Input\ArrayInput;

#[Controller(server: 'telescope')]
class EntriesController
{
/**
* Delete all of the entries from storage.
*/
#[DeleteMapping(path: '/telescope/telescope-api/entries')]
public function destroy(): void
{
$application = ApplicationContext::getContainer()->get(ApplicationInterface::class);
$application->setAutoExit(false);
$application->run(
new ArrayInput(['command' => 'telescope:clear'])
);
}
}
10 changes: 6 additions & 4 deletions src/Controller/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FriendsOfHyperf\Telescope\EntryType;
use FriendsOfHyperf\Telescope\Model\TelescopeEntryModel;
use FriendsOfHyperf\Telescope\Model\TelescopeEntryTagModel;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
Expand All @@ -31,6 +32,9 @@ abstract class EntryController
#[Inject]
protected ResponseInterface $response;

#[Inject]
protected TelescopeConfig $telescopeConfig;

public function index()
{
$before = $this->request->input('before');
Expand Down Expand Up @@ -114,11 +118,9 @@ abstract protected function watcher();

/**
* Determine the watcher recording status.
*
* @return string
*/
protected function status()
protected function status(): string
{
return 'enabled';
return $this->telescopeConfig->isRecording() ? 'enabled' : 'paused';
}
}
33 changes: 33 additions & 0 deletions src/Controller/RecordingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Telescope\Controller;

use FriendsOfHyperf\Telescope\TelescopeConfig;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(server: 'telescope')]
class RecordingController
{
#[Inject()]
protected TelescopeConfig $telescopeConfig;

/**
* Toggle recording.
*/
#[PostMapping(path: '/telescope/telescope-api/toggle-recording')]
public function toggle(): void
{
$this->telescopeConfig->isRecording() ? $this->telescopeConfig->pauseRecording() : $this->telescopeConfig->continueRecording();
}
}
10 changes: 9 additions & 1 deletion src/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Telescope\Controller;

use FriendsOfHyperf\Telescope\Telescope;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
Expand Down Expand Up @@ -40,8 +41,15 @@ public function index()
if (! isset($this->caches[$blade])) {
$this->caches[$blade] = file_get_contents($blade);
}
$templateContent = $this->caches[$blade];
$params = [
'$telescopeScriptVariables' => json_encode(Telescope::scriptVariables()),
];
foreach ($params as $key => $value) {
$templateContent = str_replace($key, $value, $templateContent);
}

return $this->response->html($this->caches[$blade]);
return $this->response->html($templateContent);
}

#[GetMapping(path: '/telescope/{view}/{id}')]
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/CommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->telescopeConfig->isEnable('command') === false) {
if (! $this->telescopeConfig->isEnable('command')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Listener/DbQueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->telescopeConfig->isEnable('db') === false) {
if (! $this->telescopeConfig->isEnable('db')) {
return;
}
if ($event instanceof QueryExecuted) {
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/ExceptionHandlerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->telescopeConfig->isEnable('exception') === false) {
if (! $this->telescopeConfig->isEnable('exception')) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/Listener/RequestHandledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function process(object $event): void
if (! $this->telescopeConfig->isEnable('request')) {
return;
}

match ($event::class) {
RequestReceived::class, RpcRequestReceived::class => $this->requestReceived($event),
RequestTerminated::class, RpcRequestTerminated::class => $this->requestHandled($event),
Expand Down
29 changes: 15 additions & 14 deletions src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Hyperf\Collection\Arr;
use Hyperf\Context\ApplicationContext;

use function Hyperf\Config\config;

class Telescope
{
public const SYNC = 0;
Expand Down Expand Up @@ -54,11 +52,6 @@ class Telescope
*/
public static bool $useDarkTheme = false;

/**
* Indicates if Telescope should record entries.
*/
public static bool $shouldRecord = false;

/**
* Indicates if Telescope migrations will be run.
*/
Expand Down Expand Up @@ -145,10 +138,8 @@ public static function getPath(): string

/**
* Add a callback that adds tags to the record.
*
* @return static
*/
public static function tag(Closure $callback)
public static function tag(Closure $callback): static
{
static::$tagUsing[] = $callback;

Expand All @@ -157,10 +148,8 @@ public static function tag(Closure $callback)

/**
* Set the callback that filters the entries that should be recorded.
*
* @return static
*/
public static function filter(Closure $callback)
public static function filter(Closure $callback): static
{
static::$filterUsing[] = $callback;

Expand All @@ -172,6 +161,18 @@ public static function getConfig(): TelescopeConfig
return ApplicationContext::getContainer()->get(TelescopeConfig::class);
}

/**
* Get the default JavaScript variables for Telescope.
*/
public static function scriptVariables(): array
{
return [
'path' => static::getConfig()->getPath(),
'timezone' => static::getConfig()->getTimezone(),
'recording' => static::getConfig()->isRecording(),
];
}

/**
* Determine if the given entry should be recorded.
*/
Expand Down Expand Up @@ -199,7 +200,7 @@ protected static function record(string $type, IncomingEntry $entry): void
return $tagCallback($entry);
}, static::$tagUsing)));

match (config('telescope.save_mode', 0)) {
match (static::getConfig()->getSaveMode()) {
self::ASYNC => TelescopeContext::addEntry($entry),
self::SYNC => $entry->create(),
default => $entry->create(),
Expand Down
46 changes: 45 additions & 1 deletion src/TelescopeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace FriendsOfHyperf\Telescope;

use FriendsOfHyperf\Telescope\Contract\CacheInterface;
use FriendsOfHyperf\Telescope\Server\Server;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Server\Event;
use Hyperf\Server\ServerInterface;
use Hyperf\Stringable\Str;
use Psr\Http\Message\ServerRequestInterface;
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;

class TelescopeConfig
{
Expand Down Expand Up @@ -91,7 +95,7 @@ public function getDatabaseQuerySlow(): int

public function isEnable(string $key): bool
{
return (bool) $this->get('enable.' . $key, false);
return ((bool) $this->get('enable.' . $key, false)) && $this->isRecording();
}

public function getAppName(): string
Expand Down Expand Up @@ -186,4 +190,44 @@ public function getIgnoreCommands(): array
{
return $this->get('ignore_commands', []);
}

public function pauseRecording(): void
{
$this->getCache()?->set($this->getPauseRecordingCacheKey(), 1);
}

public function continueRecording(): void
{
$this->getCache()?->delete($this->getPauseRecordingCacheKey());
}

public function isRecording(): bool
{
if (Context::has($key = $this->getPauseRecordingCacheKey())) {
return false;
}

try {
Context::set($key, true);
return ((bool) $this->getCache()?->get($key)) === false;
} finally {
Context::destroy($key);
}
}

private function getPauseRecordingCacheKey(): string
{
return sprintf('telescope:%s:recording:pause', $this->getAppName());
}

private function getCache(): ?PsrCacheInterface
{
$container = ApplicationContext::getContainer();

return match (true) {
$container->has(CacheInterface::class) => $container->get(CacheInterface::class),
$container->has(PsrCacheInterface::class) => $container->get(PsrCacheInterface::class),
default => null,
};
}
}
Loading

0 comments on commit 0043cfd

Please sign in to comment.