Skip to content

Telescope

可用監聽器

  • 請求監視器
  • 異常監視器
  • 資料查詢監視器
  • gRPC請求監視器
  • Redis監視器
  • 日誌監視器
  • 命令列監視器
  • 事件監視器
  • HTTP Client 監視器
  • 快取監視器
  • 定時任務監視器

安裝

shell
composer require friendsofhyperf/telescope:~3.1.0

使用 vendor:publish 命令來發布其公共資源

shell
php bin/hyperf.php vendor:publish friendsofhyperf/telescope

執行 migrate 命令執行資料庫變更來建立和儲存 Telescope 需要的資料

shell
php bin/hyperf.php migrate

使用

監聽器和中介軟體,二選一即可

請求監聽器

config/autoload/listeners.php配置檔案新增監聽器

php
<?php

return [
    FriendsOfHyperf\Telescope\Listener\RequestHandledListener::class,
    FriendsOfHyperf\Telescope\Listener\SetRequestLifecycleListener::class,
];

中介軟體

config/autoload/middlewares.php配置檔案加上全域性中介軟體

如需記錄http請求,請使用http中介軟體

php
<?php

return [
    'http' => [
        FriendsOfHyperf\Telescope\Middleware\TelescopeMiddleware::class,
    ],
];

如需記錄gRPC請求,請使用grpc中介軟體

php
<?php

return [
    'grpc' => [
        FriendsOfHyperf\Telescope\Middleware\TelescopeMiddleware::class,
    ],
];

檢視儀表板

http://127.0.0.1:9501/telescope

資料庫配置

config/autoload/telescope.php管理資料庫連線配置,預設使用default連線

php
'connection' => env('TELESCOPE_DB_CONNECTION', 'default'),

標籤

您可能希望將自己的自定義標籤附加到條目。為此,您可以使用 Telescope::tag 方法。

批次過濾

您可能只想記錄某些特殊條件下的條目。為此,您可以使用 Telescope::filter 方法。

例子

php
use FriendsOfHyperf\Telescope\Telescope;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
use FriendsOfHyperf\Telescope\IncomingEntry;

class TelescopeInitListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            BootApplication::class,
        ];
    }

    public function process(object $event): void
    {
        // attach your own custom tags
        Telescope::tag(function (IncomingEntry $entry) {
            if ($entry->type === 'request') {
                return [
                    'status:' . $entry->content['response_status'],
                    'uri:'. $entry->content['uri'],
                ];
            }
        });

        // filter entry
        Telescope::filter(function (IncomingEntry $entry): bool {
            if ($entry->type === 'request'){
                if ($entry->content['uri'] == 'xxxx') {
                    return false;
                }
            }
            return true;
        });

    }
}