螺旋/分析器

Spiral Xhprof 分析器

安装次数: 73,031

依赖项: 1

建议者: 0

安全: 0

星标: 6

关注者: 4

分支: 4

开放问题: 4

类型:模块

v3.2.0 2023-09-26 05:16 UTC

This package is auto-updated.

Last update: 2024-08-26 07:30:06 UTC


README

PHP Version Require Latest Stable Version phpunit psalm Codecov Total Downloads StyleCI

要求

请确保您的服务器已配置以下PHP版本和扩展

  • PHP 8.1+
  • Spiral 框架 3.0+

安装

要安装此包

composer require spiral/profiler

安装包后,您需要将包中的引导程序添加到您的应用程序中。

use Spiral\RoadRunnerBridge\Bootloader as RoadRunnerBridge;

protected const LOAD = [
    // ...
    Spiral\Profiler\ProfilerBootloader::class,
    // ...
];

定义环境变量

PROFILER_ENABLE=true
PROFILER_ENDPOINT=http://127.0.0.1:8080/api/profiler/store
PROFILER_APP_NAME="My super app"
PROFILER_MIDDLEWARE_DEFAULT_ENABLED=true

使用方法

使用分析器有两种方式

  • 作为中间件使用分析器
  • 作为拦截器使用分析器

作为拦截器使用分析器

如果您想分析应用程序的特定部分,并且支持使用拦截器,拦截器将很有用。

  • 控制器,
  • GRPC,
  • 队列任务,
  • TC,
  • 事件。
namespace App\Bootloader;

use App\Interceptor\CustomInterceptor;
use Spiral\Bootloader\DomainBootloader;
use Spiral\Core\CoreInterface;

class AppBootloader extends DomainBootloader
{
    protected const SINGLETONS = [
        CoreInterface::class => [self::class, 'domainCore']
    ];

    protected const INTERCEPTORS = [
        \Spiral\Profiler\ProfilerInterceptor::class
    ];
}

有关拦截器的更多信息,请参阅此处

作为中间件使用分析器

要将分析器作为中间件使用,您需要将其添加到您的路由器中。

全局中间件

namespace App\Bootloader;

use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader;
use Spiral\Profiler\ProfilerMiddleware;

final class RoutesBootloader extends BaseRoutesBootloader
{
    protected function globalMiddleware(): array
    {
        return [
            ProfilerMiddleware::class,  // <================
            LocaleSelector::class,
            ErrorHandlerMiddleware::class,
            JsonPayloadMiddleware::class,
            HttpCollector::class,
        ];
    }
    
    // ...
}

路由组中间件

namespace App\Bootloader;

use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader;
use Spiral\Profiler\ProfilerMiddleware;

final class RoutesBootloader extends BaseRoutesBootloader
{
    // ...

    protected function middlewareGroups(): array
    {
        return [
            'web' => [
                CookiesMiddleware::class,
                SessionMiddleware::class,
                CsrfMiddleware::class,
            ],
            'profiler' => [                  // <================
                ProfilerMiddleware::class,
                'middleware:web',
            ],
        ];
    }
    
    // ...
}

路由中间件

class HomeController implements SingletonInterface
{
    #[Route(route: '/', name: 'index.page', methods: ['GET'], middleware: 'profiler')]
    public function index(...): void 
    {
        // ...
    }
    
    #[Route(route: '/', name: 'index.page', methods: ['GET'], middleware: \Spiral\Profiler\ProfilerMiddleware::class)]
    public function index(...): void 
    {
        // ...
    }
}

分析策略。

默认情况下,中间件将在每个请求上启动分析。您可以将分析配置为仅针对某些请求启用。

  1. 将环境变量PROFILER_MIDDLEWARE_DEFAULT_ENABLED设置为false。
PROFILER_MIDDLEWARE_DEFAULT_ENABLED=false
  1. 传递Http头X-Spiral-Profiler-Enable=1以分析您想要的请求。