Skip to content

Commit

Permalink
Compiler pass to set philkra to null (#4)
Browse files Browse the repository at this point in the history
* compiler init

* use dependency version compatible with sf 3.4

* set argument philkra agent

* get proper apm enabled parameter

* syntax

* comment purpose of engine pass

* prevent bundles instantiation if apm disabled

* class ids syntax

* remove references to enabled parameter
  • Loading branch information
alex-mercier authored Jan 30, 2020
1 parent 3f14891 commit 799256f
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 27 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
10 changes: 5 additions & 5 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/DependencyInjection/Compiler/ElasticApmEnginePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @author Wizacha DevTeam <[email protected]>
* @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);
}
}
}
9 changes: 0 additions & 9 deletions src/ElasticApmAbstractSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 8 additions & 0 deletions src/ElasticApmBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
8 changes: 0 additions & 8 deletions src/ElasticApmSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -51,10 +47,6 @@ public function onKernelRequest(GetResponseEvent $kernelEvent)

public function onKernelException(GetResponseForExceptionEvent $kernelEvent)
{
if ($this->isDisabled()) {
return $this;
}

$this->agentService->error($kernelEvent->getException());
}
}
98 changes: 98 additions & 0 deletions tests/ElasticApmEnginePassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* @author Wizacha DevTeam <[email protected]>
* @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);
}
}
3 changes: 1 addition & 2 deletions tests/ElasticApmSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
declare(strict_types=1);

namespace Wizacha\ElasticApmBundle\tests;
namespace Wizacha\ElasticApmBundle\Tests;

use PHPUnit\Framework\TestCase;
use PhilKra\Events\Transaction;
Expand Down Expand Up @@ -39,7 +39,6 @@ public function setUp(): void
->getMock();
}


public function testEventSubscription(): void
{
static::assertArrayHasKey(KernelEvents::REQUEST, ElasticApmSubscriber::getSubscribedEvents());
Expand Down

0 comments on commit 799256f

Please sign in to comment.