GithubHelp home page GithubHelp logo

profiler's Introduction

Spiral Xhprof Profiler

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

Requirements

Make sure that your server is configured with following PHP version and extensions:

  • PHP 8.1+
  • Spiral framework 3.0+

Installation

To install the package:

composer require spiral/profiler

After package install you need to add bootloader from the package in your application.

use Spiral\RoadRunnerBridge\Bootloader as RoadRunnerBridge;

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

Define env variables:

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

Usage

There are two ways to use profiler:

  • Profiler as a middleware
  • Profiler as an interceptor

Profiler as an interceptor

Interceptor will be useful if you want to profile some specific part of your application which supports using interceptors.

  • Controllers,
  • GRPC,
  • Queue jobs,
  • TCP
  • Events.
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
    ];
}

Read more about interceptors here.

Profiler as a middleware

To use profiler as a middleware you need to add it to your router.

Global middleware

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,
        ];
    }
    
    // ...
}

Route group middleware

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',
            ],
        ];
    }
    
    // ...
}

Route middleware

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 
    {
        // ...
    }
}

Profiling strategy.

By default, middleware start profiling on every request. ะ You can configure profiling to be enabled only for certain requests.

  1. Set env variable PROFILER_MIDDLEWARE_DEFAULT_ENABLED to false.
PROFILER_MIDDLEWARE_DEFAULT_ENABLED=false
  1. Pass Http header X-Spiral-Profiler-Enable=1 for request you want to profile.

profiler's People

Contributors

alexndr-novikov avatar butschster avatar gam6itko avatar milky-ashes avatar wolfy-j avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

profiler's Issues

FR. Ability to disable profiling

I have been using your profiler package on my local-dev machine and it has been working great. However, when I tried running my project on a stage machine that doesn't have any profiling extension, I encountered the following error:

[SpiralPackages\\Profiler\\Exception\\ProfilerNotFoundException]  as /srv/vendor/spiral-packages/profiler/src/DriverFactory.php:30

I believe it would be useful to have the ability to disable this functionality by setting an environment variable, such as

PROFILE=false

I would like to request that this feature be added to the package. This would greatly improve the usability of the package and make it more accessible to a wider range of users.

Related packages: https://github.com/spiral-packages/profiler

[BUG] segmentation fault when ProfilerInterceptor process calls several times

I use ProfilerInterceptor for collecting profiles.

If I use $core->callAction somewhere in my controller action then segfault occurs.

Controller code example

#[Route(route: '/api/foo', methods: 'GET')]
public function fooAction(LogsInterface $logs, CoreInterface $core): void
{
    $logs->getLogger('segfault')->info('foo start');
    $core->callAction(self::class, 'barAction');
    $logs->getLogger('segfault')->info('foo finish');
}

#[Route(route: '/api/bar', methods: 'GET')]
public function barAction(LogsInterface $logs): void
{
    $logs->getLogger('segfault')->info('bar start');
    $logs->getLogger('segfault')->info('bar finish');
}

rr logs

2023-05-17T21:30:10+0000  DEBUG   server          worker stopped  {"internal_event_name": "EventWorkerWaitExit", "error": "signal: segmentation fault (core dumped)\nprocess_wait: signal: segmentation fault (core dumped)"}

$profiler->start();

In this case ProfilerInterceptor::process calls 2 times.

calls stack example:

  • ProfilerInterceptor::process
    • $profiler->start(); //1st time
    • $core->callAction();
      • $core->callAction(self::class, 'barAction');
      • ProfilerInterceptor::process
      • $profiler->start(); //2nd time
      • $core->callAction();
      • $profiler->end(); //ends 1st profiler start
      • controller action exit
    • $profiler->end(); //segfault occurs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.