Skip to content

Commit

Permalink
add transaction/exception include/exclude config
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixgao committed Jan 14, 2020
1 parent d6eb269 commit 51a8995
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function getConfigTreeBuilder()
->scalarNode('appVersion')->defaultValue('')->end()
->scalarNode('serverUrl')->defaultValue('http://127.0.0.1:8200')->end()
->scalarNode('secretToken')->defaultNull()->end()
->scalarNode('environment')->defaultValue('development')->end()
->integerNode('timeout')->defaultValue(5)->end()
->arrayNode('env')
->scalarPrototype()->end()
Expand All @@ -44,6 +45,26 @@ public function getConfigTreeBuilder()
->integerNode('backtraceLimit')->defaultValue(0)->end()
->end()
->end()
->arrayNode('transactions')
->children()
->arrayNode('exclude')
->scalarPrototype()->end()
->end()
->arrayNode('include')
->scalarPrototype()->end()
->end()
->end()
->end()
->arrayNode('exceptions')
->children()
->arrayNode('exclude')
->scalarPrototype()->end()
->end()
->arrayNode('include')
->scalarPrototype()->end()
->end()
->end()
->end()
->scalarNode('sharedContextProvider')->defaultNull()->end()
->end()
;
Expand Down
31 changes: 31 additions & 0 deletions src/DependencyInjection/ElasticApmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,36 @@ public function load(array $configs, ContainerBuilder $container)
$agentConfig = $config['agent'];
$elasticApmAgentDefinition = $container->getDefinition('elastic_apm.agent');
$elasticApmAgentDefinition->replaceArgument(0, $agentConfig);

$requestListenerDefinition = $container->getDefinition('elastic_apm.listener.request');
$exceptionListenerDefinition = $container->getDefinition('elastic_apm.listener.exception');

if ($transactionConfig = $config['transactions']) {
if ($transactionConfig['exclude']) {
$requestListenerDefinition->addMethodCall('setExclude', [$transactionConfig['exclude']]);
}

if ($transactionConfig['include']) {
$requestListenerDefinition->addMethodCall('setInclude', [$transactionConfig['include']]);

if ($transactionConfig['exclude']) {
@trigger_error('The "transactions.exclude" option is ignored in when "transactions.include" was set.', E_USER_NOTICE);
}
}
}

if ($exceptionConfig = $config['exceptions']) {
if ($exceptionConfig['exclude']) {
$exceptionListenerDefinition->addMethodCall('setExclude', [$exceptionConfig['exclude']]);
}

if ($exceptionConfig['include']) {
$exceptionListenerDefinition->addMethodCall('setInclude', [$exceptionConfig['include']]);

if ($exceptionConfig['exclude']) {
@trigger_error('The "exceptions.exclude" option is ignored in when "exceptions.include" was set.', E_USER_NOTICE);
}
}
}
}
}
38 changes: 38 additions & 0 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ExceptionListener implements LoggerAwareInterface, AgentAwareInterface

protected $enabled = false;

protected $exclude = [];

protected $include = [];

public function __construct($enabled)
{
$this->enabled = $enabled;
Expand All @@ -28,6 +32,30 @@ public function onKernelException(GetResponseForExceptionEvent $event)

$exception = $event->getException();

$name = get_class($exception);
$match = true;
if ($this->include) {
$match = false;
foreach ($this->include as $pattern) {
$this->logger->info($pattern);
if (fnmatch($pattern, $name, FNM_NOESCAPE)) {
$match = true;
break;
}
}
} else if ($this->exclude) {
foreach ($this->exclude as $pattern) {
if (fnmatch($pattern, $name, FNM_NOESCAPE)) {
$match = false;
break;
}
}
}

if (!$match) {
return;
}

$this->agent->captureThrowable($exception);

if ($this->logger instanceof LoggerInterface) {
Expand All @@ -46,4 +74,14 @@ public function onKernelException(GetResponseForExceptionEvent $event)
);
}
}

public function setExclude(array $exclude)
{
$this->exclude = $exclude;
}

public function setInclude(array $include)
{
$this->include = $include;
}
}
41 changes: 40 additions & 1 deletion src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class RequestListener implements LoggerAwareInterface, AgentAwareInterface

protected $enabled = false;

protected $exclude = [];

protected $include = [];

public function __construct($enabled)
{
$this->enabled = $enabled;
Expand All @@ -29,7 +33,32 @@ public function onKernelRequest(GetResponseEvent $event)
}

try {
$this->agent->startTransaction($name = RequestConverter::getTransactionName($event->getRequest()));
$name = RequestConverter::getTransactionName($event->getRequest());

$match = true;
if ($this->include) {
$match = false;
foreach ($this->include as $pattern) {
$this->logger->info($pattern);
if (fnmatch($pattern, $name, FNM_NOESCAPE)) {
$match = true;
break;
}
}
} else if ($this->exclude) {
foreach ($this->exclude as $pattern) {
if (fnmatch($pattern, $name, FNM_NOESCAPE)) {
$match = false;
break;
}
}
}

if (!$match) {
return;
}

$this->agent->startTransaction($name);
} catch (DuplicateTransactionNameException $e) {
return;
}
Expand All @@ -38,4 +67,14 @@ public function onKernelRequest(GetResponseEvent $event)
$this->logger->info(sprintf('APM transaction registered: "%s"', $name));
}
}

public function setExclude(array $exclude)
{
$this->exclude = $exclude;
}

public function setInclude(array $include)
{
$this->include = $include;
}
}
6 changes: 3 additions & 3 deletions src/Support/RequestConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class RequestConverter
{
public static function getTransactionName(Request $request)
{
$method = $request->getMethod();
$routeName = $request->get('_route');
// $method = $request->getMethod();
// $routeName = $request->get('_route');
$controllerName = $request->get('_controller');

return sprintf('%s (%s)', $controllerName, $routeName);
return sprintf('%s', $controllerName);
}
}

0 comments on commit 51a8995

Please sign in to comment.