diff --git a/composer.json b/composer.json index 35302ad..23ae3c3 100644 --- a/composer.json +++ b/composer.json @@ -26,13 +26,14 @@ }, "autoload-dev": { "psr-4": { - "Wizacha\\ElasticApmBundle\\Tests\\": "/Tests/" + "Wizacha\\ElasticApmBundle\\Tests\\": "tests/" } }, "require": { "philkra/elastic-apm-php-agent": "dev-master", - "wizaplace/elastic-apm-wrapper": "^0.2.2", - "symfony/symfony": ">=3.0 <4.3" + "symfony/dependency-injection": "~3.0", + "symfony/symfony": ">=3.0 <4.3", + "wizaplace/elastic-apm-wrapper": "^0.3.0" }, "repositories": [ { diff --git a/config/services.yml b/config/services.yml index 2cfcfe3..cfb542b 100644 --- a/config/services.yml +++ b/config/services.yml @@ -10,17 +10,17 @@ services: _defaults: autowire: true autoconfigure: true - bind: - $apmEnabled: '%elastic_apm.enabled%' - $agent: '@PhilKra\Agent' - $agentService: '@Wizacha\ElasticApm\Service\AgentService' Wizacha\ElasticApmBundle\ElasticApmSubscriber: class: Wizacha\ElasticApmBundle\ElasticApmSubscriber tags: - { name: kernel.event_subscriber } + Wizacha\ElasticApm\Service\AgentService: - class: Wizacha\ElasticApm\Service\AgentService + autowire: true + arguments: + - '%elastic_apm.enabled%' + - '@PhilKra\Agent' PhilKra\Agent: class: PhilKra\Agent diff --git a/src/DependencyInjection/Compiler/ElasticApmEnginePass.php b/src/DependencyInjection/Compiler/ElasticApmEnginePass.php new file mode 100644 index 0000000..94adf1a --- /dev/null +++ b/src/DependencyInjection/Compiler/ElasticApmEnginePass.php @@ -0,0 +1,26 @@ + + * @copyright Copyright (c) Wizacha + * @license Proprietary + */ + +namespace Wizacha\ElasticApmBundle\DependencyInjection\Compiler; + +use PhilKra\Agent; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Wizacha\ElasticApm\Service\AgentService; +use Wizacha\ElasticApmBundle\ElasticApmSubscriber; + +class ElasticApmEnginePass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + if (false === $container->resolveEnvPlaceholders($container->getParameter('elastic_apm.enabled'), true)) { + $container->removeDefinition(AgentService::class); + $container->removeDefinition(ElasticApmSubscriber::class); + $container->removeDefinition(Agent::class); + } + } +} diff --git a/src/ElasticApmAbstractSubscriber.php b/src/ElasticApmAbstractSubscriber.php index a9a3f52..8b1c672 100644 --- a/src/ElasticApmAbstractSubscriber.php +++ b/src/ElasticApmAbstractSubscriber.php @@ -37,20 +37,11 @@ public function __destruct() public function onKernelTerminate() { - if ($this->isDisabled()) { - return $this; - } - if ($this->transaction instanceof Transaction) { $this->agentService->stopTransaction(); $this->transaction = null; } } - public function isDisabled(): bool - { - return false === $this->agentService->getApmEnabled(); - } - abstract public static function getSubscribedEvents(); } diff --git a/src/ElasticApmBundle.php b/src/ElasticApmBundle.php index 8243d09..0f48255 100644 --- a/src/ElasticApmBundle.php +++ b/src/ElasticApmBundle.php @@ -10,8 +10,16 @@ namespace Wizacha\ElasticApmBundle; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Wizacha\ElasticApmBundle\DependencyInjection\Compiler\ElasticApmEnginePass; class ElasticApmBundle extends Bundle { + public function build(ContainerBuilder $container) + { + parent::build($container); + + $container->addCompilerPass(new ElasticApmEnginePass()); + } } diff --git a/src/ElasticApmSubscriber.php b/src/ElasticApmSubscriber.php index 2fe90fc..30acb70 100644 --- a/src/ElasticApmSubscriber.php +++ b/src/ElasticApmSubscriber.php @@ -31,10 +31,6 @@ public static function getSubscribedEvents() */ public function onKernelRequest(GetResponseEvent $kernelEvent) { - if ($this->isDisabled()) { - return $this; - } - if (true === $kernelEvent->isMasterRequest() && null === $this->transaction) { $this->transaction = $this->agentService ->startTransaction( @@ -51,10 +47,6 @@ public function onKernelRequest(GetResponseEvent $kernelEvent) public function onKernelException(GetResponseForExceptionEvent $kernelEvent) { - if ($this->isDisabled()) { - return $this; - } - $this->agentService->error($kernelEvent->getException()); } } diff --git a/tests/ElasticApmEnginePassTest.php b/tests/ElasticApmEnginePassTest.php new file mode 100644 index 0000000..71a827e --- /dev/null +++ b/tests/ElasticApmEnginePassTest.php @@ -0,0 +1,98 @@ + + * @copyright Copyright (c) Wizacha + * @license Proprietary + */ +declare(strict_types=1); + +namespace Wizacha\ElasticApmBundle\Tests; + +use PhilKra\Agent; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Wizacha\ElasticApm\Service\AgentService; +use Wizacha\ElasticApmBundle\DependencyInjection\Compiler\ElasticApmEnginePass; +use Wizacha\ElasticApmBundle\ElasticApmSubscriber; + +class ElasticApmEnginePassTest extends TestCase +{ + /** + * @var ContainerBuilder + */ + protected $container; + + protected function setUp(): void + { + $this->container = new ContainerBuilder(); + $this->container->getCompilerPassConfig()->setOptimizationPasses([]); + $this->container->getCompilerPassConfig()->setRemovingPasses([]); + $this->container->getCompilerPassConfig()->setAfterRemovingPasses([]); + $this->container->addCompilerPass(new ElasticApmEnginePass()); + + $loader = new YamlFileLoader( + $this->container, + new FileLocator(__DIR__ . '/../config') + ); + + $loader->load(__DIR__ . '/../config/services.yml'); + } + + protected function tearDown(): void + { + $this->container = null; + } + + public function testEnabledConfiguration(): void + { + $this->setParameter('elastic_apm.enabled', true); + $this->container->compile(); + + static::assertContains(AgentService::class, $this->container->getServiceIds()); + static::assertContains(ElasticApmSubscriber::class, $this->container->getServiceIds()); + static::assertContains(Agent::class, $this->container->getServiceIds()); + } + + public function testDisabledConfiguration(): void + { + $this->setParameter('elastic_apm.enabled', false); + $this->container->compile(); + + static::assertNotContains(AgentService::class, $this->container->getServiceIds()); + static::assertNotContains(ElasticApmSubscriber::class, $this->container->getServiceIds()); + static::assertNotContains(Agent::class, $this->container->getServiceIds()); + } + + /** + * Shortcut for quickly defining services. The returned Definition object can be further modified if necessary. + */ + final protected function registerService(string $serviceId, string $class): Definition + { + $definition = new Definition($class); + + $this->container->setDefinition($serviceId, $definition); + + return $definition; + } + + /** + * Set a service definition you manually created. + */ + final protected function setDefinition(string $serviceId, Definition $definition): void + { + $this->container->setDefinition($serviceId, $definition); + } + + /** + * Set a parameter. + * + * @param mixed $parameterValue + */ + final protected function setParameter(string $parameterId, $parameterValue): void + { + $this->container->setParameter($parameterId, $parameterValue); + } +} diff --git a/tests/ElasticApmSubscriberTest.php b/tests/ElasticApmSubscriberTest.php index a9a7ce7..147374e 100644 --- a/tests/ElasticApmSubscriberTest.php +++ b/tests/ElasticApmSubscriberTest.php @@ -6,7 +6,7 @@ */ declare(strict_types=1); -namespace Wizacha\ElasticApmBundle\tests; +namespace Wizacha\ElasticApmBundle\Tests; use PHPUnit\Framework\TestCase; use PhilKra\Events\Transaction; @@ -39,7 +39,6 @@ public function setUp(): void ->getMock(); } - public function testEventSubscription(): void { static::assertArrayHasKey(KernelEvents::REQUEST, ElasticApmSubscriber::getSubscribedEvents());