Skip to content

Commit

Permalink
Support ignore telescope command (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
guandeng authored and huangdijia committed Jan 31, 2024
1 parent 5c930b6 commit 13f3659
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions publish/telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@
'ignore_paths' => [
// 'nova-api*',
],
'ignore_commands' => [
// 'demo:command',
],
];
11 changes: 9 additions & 2 deletions src/Listener/CommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Command\Event\AfterExecute;
use Hyperf\Event\Contract\ListenerInterface;

Expand Down Expand Up @@ -44,13 +45,19 @@ public function process(object $event): void
}

$command = $event->getCommand();

if (in_array($command->getName(), $this->telescopeConfig->getIgnoreCommands())) {
return;
}

$arguments = (fn () => $this->input->getArguments())->call($command);
$options = (fn () => $this->input->getOptions())->call($command);
$name = $command->getName();
$exitCode = (fn () => $this->exitCode)->call($command);

TelescopeContext::getOrSetBatch((string) TelescopeContext::getBatchId());

Telescope::recordCommand(IncomingEntry::make([
'command' => $name,
'command' => $command->getName(),
'exit_code' => $exitCode,
'arguments' => $arguments,
'options' => $options,
Expand Down
9 changes: 1 addition & 8 deletions src/Listener/RequestHandledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ public function requestReceived(RequestReceived|RpcRequestReceived $event)
$request = $event->request;
$batchId = $request->getHeaderLine('batch-id') ?: $this->getRpcBatchId();

if ($batchId) {
$subBatchId = Str::orderedUuid()->toString();
TelescopeContext::setSubBatchId($subBatchId);
} else {
$batchId = Str::orderedUuid()->toString();
}

TelescopeContext::setBatchId($batchId);
TelescopeContext::getOrSetBatch($batchId);
}

public function requestHandled(RequestTerminated|RpcRequestTerminated $event)
Expand Down
9 changes: 1 addition & 8 deletions src/Middleware/TelescopeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$batchId = $this->getRpcBatchId();
}

if ($batchId) {
$subBatchId = Str::orderedUuid()->toString();
TelescopeContext::setSubBatchId($subBatchId);
} else {
$batchId = Str::orderedUuid()->toString();
}

TelescopeContext::setBatchId($batchId);
TelescopeContext::getOrSetBatch($batchId);

$response = $handler->handle($request);
$response = $response->withHeader('batch-id', $batchId);
Expand Down
5 changes: 5 additions & 0 deletions src/TelescopeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,9 @@ public function isPathIgnored(ServerRequestInterface|string $path): bool

return Str::is($this->getIgnorePaths(), rawurldecode(trim($path, '/')));
}

public function getIgnoreCommands(): array
{
return $this->get('ignore_commands', []);
}
}
13 changes: 13 additions & 0 deletions src/TelescopeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Hyperf\Context\Context;
use Hyperf\Contract\PackerInterface;
use Hyperf\Stringable\Str;

use function Hyperf\Coroutine\defer;

Expand Down Expand Up @@ -133,4 +134,16 @@ public static function getGrpcResponsePayload(): ?array
{
return Context::get(self::GRPC_RESPONSE_PAYLOAD) ?: null;
}

public static function getOrSetBatch(string $batchId): void
{
if ($batchId) {
$subBatchId = Str::orderedUuid()->toString();
self::setSubBatchId($subBatchId);
} else {
$batchId = Str::orderedUuid()->toString();
}

self::setBatchId($batchId);
}
}

0 comments on commit 13f3659

Please sign in to comment.