diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3222e9d..068e2e6 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() @@ -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() ; diff --git a/src/DependencyInjection/ElasticApmExtension.php b/src/DependencyInjection/ElasticApmExtension.php index b1cc687..14bf699 100644 --- a/src/DependencyInjection/ElasticApmExtension.php +++ b/src/DependencyInjection/ElasticApmExtension.php @@ -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); + } + } + } } } diff --git a/src/EventListener/ExceptionListener.php b/src/EventListener/ExceptionListener.php index 3aebd58..f0743b7 100644 --- a/src/EventListener/ExceptionListener.php +++ b/src/EventListener/ExceptionListener.php @@ -15,6 +15,10 @@ class ExceptionListener implements LoggerAwareInterface, AgentAwareInterface protected $enabled = false; + protected $exclude = []; + + protected $include = []; + public function __construct($enabled) { $this->enabled = $enabled; @@ -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) { @@ -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; + } } diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index ab7ee24..09c540e 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -17,6 +17,10 @@ class RequestListener implements LoggerAwareInterface, AgentAwareInterface protected $enabled = false; + protected $exclude = []; + + protected $include = []; + public function __construct($enabled) { $this->enabled = $enabled; @@ -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; } @@ -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; + } } diff --git a/src/Support/RequestConverter.php b/src/Support/RequestConverter.php index cc51001..74d8536 100644 --- a/src/Support/RequestConverter.php +++ b/src/Support/RequestConverter.php @@ -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); } }