Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

feat(blackfire): Add bridge for upscale/swoole-blackfire #221

Merged
merged 4 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@
"phpunit/phpunit": "^9.1.3",
"swoole/ide-helper": "~4.4.7 || ~4.5.0",
"symfony/debug-pack": "^1.0",
"symfony/monolog-bridge": "^4.3.1|^5.0",
"symfony/monolog-bundle": "^3.3",
"symfony/framework-bundle": "^4.3.1|^5.0",
"symfony/messenger": "^4.3.1|^5.0",
"symfony/monolog-bridge": "^4.3.1|^5.0",
"symfony/monolog-bundle": "^3.3",
"symfony/twig-bundle": "^4.3.1|^5.0",
"symfony/var-dumper": "^4.3.1|^5.0",
"symfony/yaml": "^4.3.1|^5.0"
"symfony/yaml": "^4.3.1|^5.0",
"upscale/swoole-blackfire": "^3.0"
},
"suggest": {
luca-nardelli marked this conversation as resolved.
Show resolved Hide resolved
"ext-inotify": "To enable HMR.",
"doctrine/orm": "To use the EntityManager handler.",
"symfony/messenger": "To use Symfony Messenger Swoole Task Transport."
"symfony/messenger": "To use Symfony Messenger Swoole Task Transport.",
"upscale/swoole-blackfire": "To enable Blackfire profiling"
},
"autoload": {
"files": [
Expand Down
234 changes: 233 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docs/configuration-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ swoole:

# see: \K911\Swoole\Bridge\Doctrine\ORM\EntityManagerHandler
entity_manager_handler: true

# see: \K911\Swoole\Bridge\Upscale\Blackfire\WithProfiler
blackfire_profiler: false


# swoole http server settings
# see https://www.swoole.co.uk/docs/modules/swoole-server/configuration
Expand Down
19 changes: 19 additions & 0 deletions docs/swoole-blackfire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Swoole Server Blackfire integration

Blackfire (https://blackfire.io/docs/introduction) is a profiler for PHP applications.

By default, blackfire does not work with Swoole Server, however, thanks to the work of [https://github.com/upscalesoftware/swoole-blackfire](https://github.com/upscalesoftware/swoole-blackfire) the Swoole server can be instrumented to produce data for blackfire.

## How to use?

First of all, setup blackfire following their docs [https://blackfire.io/docs/up-and-running/installation](https://blackfire.io/docs/up-and-running/installation)

Then, install the swoole-blackfire library

```shell script
composer require upscale/swoole-blackfire --dev
```

That's it! The bundle will automatically detect that the library was installed and it will instrument the server.

If, for some reason, you want to explicitly disable the profiler, you can do so from the bundle configuration, see [here](configuration-reference.md)
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('entity_manager_handler')
->defaultNull()
->end()
->booleanNode('blackfire_profiler')
->defaultNull()
->end()
->booleanNode('session_cookie_event_listener')
->defaultFalse()
->treatNullLike(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use K911\Swoole\Bridge\Symfony\HttpKernel\DebugHttpKernelRequestHandler;
use K911\Swoole\Bridge\Symfony\Messenger\SwooleServerTaskTransportFactory;
use K911\Swoole\Bridge\Symfony\Messenger\SwooleServerTaskTransportHandler;
use K911\Swoole\Bridge\Upscale\Blackfire\WithProfiler;
use K911\Swoole\Server\Config\Socket;
use K911\Swoole\Server\Config\Sockets;
use K911\Swoole\Server\Configurator\ConfiguratorInterface;
Expand All @@ -38,6 +39,7 @@
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Upscale\Swoole\Blackfire\Profiler;

final class SwooleExtension extends Extension implements PrependExtensionInterface
{
Expand Down Expand Up @@ -304,6 +306,24 @@ private function registerHttpServerServices(array $config, ContainerBuilder $con
->setPublic(false)
;
}

if ($config['blackfire_profiler'] || (null === $config['blackfire_profiler'] && \class_exists(Profiler::class))) {
$container->register(Profiler::class)
->setClass(Profiler::class)
;

$container->register(WithProfiler::class)
->setClass(WithProfiler::class)
->setAutowired(false)
->setAutoconfigured(false)
->setPublic(false)
->addArgument(new Reference(Profiler::class))
;
$def = $container->getDefinition('swoole_bundle.server.http_server.configurator.for_server_run_command');
$def->addArgument(new Reference(WithProfiler::class));
$def = $container->getDefinition('swoole_bundle.server.http_server.configurator.for_server_start_command');
$def->addArgument(new Reference(WithProfiler::class));
}
}

private function isBundleLoaded(ContainerBuilder $container, string $bundleName): bool
Expand Down
30 changes: 30 additions & 0 deletions src/Bridge/Upscale/Blackfire/WithProfiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Bridge\Upscale\Blackfire;

use K911\Swoole\Server\Configurator\ConfiguratorInterface;
use Swoole\Http\Server;
use Upscale\Swoole\Blackfire\Profiler;

final class WithProfiler implements ConfiguratorInterface
{
/**
* @var Profiler
*/
private $profiler;

public function __construct(Profiler $profiler)
{
$this->profiler = $profiler;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$this->profiler->instrument($server);
}
}
Loading