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;
        });

    }
}