Skip to content

Commit

Permalink
Elastic APM Bundle symfo (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-mercier authored and ecourtial committed Jan 22, 2020
1 parent 3b8e36b commit 5458916
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PHILKRA_APP_NAME=Wizaplace
PHILKRA_APP_VERSION=1.0
PHILKRA_ENVIRONMENT=Dev
PHILKRA_SERVER_URL=http=//172.17.0.1:8200
PHILKRA_SERVER_URL=http://172.17.0.1:8200
PHILKRA_SECRET_TOKEN=blabla

ELASTIC_APM_ENABLED=1
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/vendor
.idea/*
.env
composer.lock
composer.lock
.php_cs.cache
.phpunit.result.cache
19 changes: 19 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
->exclude('tests')
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'increment_style' => ['style' => 'post'],
'declare_strict_types' => true,
'phpdoc_summary' => false
])
->setFinder($finder)
;
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
phpcs:
vendor/bin/phpcs

phpstan:
vendor/bin/phpstan analyse

phpcsfixer:
vendor/bin/php-cs-fixer fix --dry-run --allow-risky=yes

test:
vendor/bin/phpunit
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# elastic-apm-symfony
# Elastic APM Symfony Bundle
Uses [wizaplace/wizaplace/elastic-apm-wrapper](https://github.com/wizaplace/elastic-apm-wrapper).


## Installation

1) Add the repositories to your _composer.json_ file:

{
"type": "git",
"url": "https://github.com/wizaplace/elastic-apm-symfony"
},
{
"type": "git",
"url": "https://github.com/wizaplace/elastic-apm-wrapper"
}
2) Add the requirements in your _require_ section of the _composer.json_ file:
` "philkra/elastic-apm-php-agent": "dev-master"`

3) Require the bundle _composer require wizaplace/elastic-apm-symfony_
19 changes: 18 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"autoload": {
"psr-4": {
"Wizacha\\ElasticApmBundle\\": ""
"Wizacha\\ElasticApmBundle\\": "src/"
},
"exclude-from-classmap": [
"/Tests/"
Expand All @@ -28,5 +28,22 @@
"psr-4": {
"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"
},
"repositories": [
{
"type": "git",
"url": "[email protected]:wizaplace/elastic-apm-wrapper.git"
}
],
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"phpstan/phpstan": "^0.12.5",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
}
}
20 changes: 15 additions & 5 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ services:
_defaults:
autowire: true
autoconfigure: true
bind:
$apmEnabled: '%elastic_apm.enabled%'
$agent: '@PhilKra\Agent'
$agentService: '@Wizacha\ElasticApm\Service\AgentService'

elastic_apm.service.agent_service:
Wizacha\ElasticApmBundle\ElasticApmSubscriber:
class: Wizacha\ElasticApmBundle\ElasticApmSubscriber
tags:
- { name: kernel.event_subscriber }
Wizacha\ElasticApm\Service\AgentService:
class: Wizacha\ElasticApm\Service\AgentService
arguments:
$apmEnabled: '%elastic_apm.enabled%'
$agent: '@philkra.agent'

philkra.agent:
PhilKra\Agent:
class: PhilKra\Agent
arguments:
- { appName: '%philkra.app_name%', appVersion: '%philkra.app_version%', environment: '%philkra.environment%', serverUrl: '%philkra.server_url%', secretToken: '%philkra.secret_token%' }

Wizacha\ElasticApmBundle\DependencyInjection\ElasticApmExtension:
class: Wizacha\ElasticApmBundle\DependencyInjection\ElasticApmExtension
tags:
- { name: apm_bundle.extension }
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<arg name="basepath" value="."/>
<arg name="colors"/>
<arg name="report" value="diff"/>
<rule ref="PSR12"/>
<file>src</file>
</ruleset>
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 5
paths:
- src
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
colors="true"
bootstrap="./vendor/autoload.php"
verbose="true"
>
<testsuite name="Elastic Apm Bundle Unit Tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
<php>
<ini name="date.timezone" value="Europe/Paris"/>
<ini name="xdebug.max_nesting_level" value="200"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/ApmBundle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Wizacha\ApmBundle;
namespace Wizacha\ElasticApmBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand Down
29 changes: 29 additions & 0 deletions src/DependencyInjection/ElasticApmExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @author Wizacha DevTeam <[email protected]>
* @copyright Copyright (c) Wizacha
* @license Proprietary
*/

declare(strict_types=1);

namespace Wizacha\ElasticApmBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class ElasticApmExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../../config')
);

$loader->load(__DIR__ . '/../../config/services.yml');
}
}
47 changes: 47 additions & 0 deletions src/ElasticApmAbstractSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @author Wizacha DevTeam <[email protected]>
* @copyright Copyright (c) Wizacha
* @license Proprietary
*/

declare(strict_types=1);

namespace Wizacha\ElasticApmBundle;

use PhilKra\Events\Transaction;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wizacha\ElasticApm\Service\AgentService;

abstract class ElasticApmAbstractSubscriber implements EventSubscriberInterface
{
/** @var AgentService */
protected $agentService;

/** @var Transaction|null */
protected $transaction;

public function __construct(AgentService $agentService)
{
$this->agentService = $agentService;
}

public function __destruct()
{
if ($this->transaction instanceof Transaction) {
$this->agentService->stopTransaction();
$this->transaction = null;
}
}

public function onKernelTerminate()
{
if ($this->transaction instanceof Transaction) {
$this->agentService->stopTransaction();
$this->transaction = null;
}
}

abstract public static function getSubscribedEvents();
}
17 changes: 17 additions & 0 deletions src/ElasticApmBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @author Wizacha DevTeam <[email protected]>
* @copyright Copyright (c) Wizacha
* @license Proprietary
*/

declare(strict_types=1);

namespace Wizacha\ElasticApmBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ElasticApmBundle extends Bundle
{
}
52 changes: 52 additions & 0 deletions src/ElasticApmSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @author Wizacha DevTeam <[email protected]>
* @copyright Copyright (c) Wizacha
* @license Proprietary
*/

declare(strict_types=1);

namespace Wizacha\ElasticApmBundle;

use PhilKra\Exception\Transaction\DuplicateTransactionNameException;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ElasticApmSubscriber extends ElasticApmAbstractSubscriber
{
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::EXCEPTION => ['onKernelException', 100],
KernelEvents::TERMINATE => 'onKernelTerminate',
];
}

/**
* @throws DuplicateTransactionNameException
*/
public function onKernelRequest(GetResponseEvent $kernelEvent)
{
if (true === $kernelEvent->isMasterRequest() && null === $this->transaction) {
$this->transaction = $this->agentService
->startTransaction(
sprintf(
'%s %s (%s)',
$kernelEvent->getRequest()->getMethod(),
$kernelEvent->getRequest()->get('_controller'),
$kernelEvent->getRequest()->get('_route')
)
)
->getTransaction();
}
}

public function onKernelException(GetResponseForExceptionEvent $kernelEvent)
{
$this->agentService->error($kernelEvent->getException());
}
}
Loading

0 comments on commit 5458916

Please sign in to comment.