NewRelicRequestSubscriber.php in New Relic 8
File
src/EventSubscriber/NewRelicRequestSubscriber.php
View source
<?php
namespace Drupal\new_relic_rpm\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NewRelicRequestSubscriber implements EventSubscriberInterface {
protected $adapter;
protected $pathMatcher;
protected $config;
protected $currentPathStack;
protected $processedMasterRequest = FALSE;
protected $currentUser;
public function __construct(NewRelicAdapterInterface $adapter, PathMatcherInterface $path_matcher, ConfigFactoryInterface $config_factory, CurrentPathStack $current_path_stack, AccountInterface $current_user) {
$this->adapter = $adapter;
$this->pathMatcher = $path_matcher;
$this->config = $config_factory
->get('new_relic_rpm.settings');
$this->currentPathStack = $current_path_stack;
$this->currentUser = $current_user;
}
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => [
'onRequest',
30,
],
];
}
public function onRequest(GetResponseEvent $event) {
if ($event
->getRequestType() !== HttpKernelInterface::MASTER_REQUEST && $this->processedMasterRequest) {
return;
}
$ignore_roles = $this->config
->get('ignore_roles');
$ignore_urls = $this->config
->get('ignore_urls');
$bg_urls = $this->config
->get('bg_urls');
$exclude_urls = $this->config
->get('exclusive_urls');
$disable_autorum = $this->config
->get('disable_autorum');
if ($disable_autorum) {
$this->adapter
->disableAutorum();
}
if (!empty($ignore_roles)) {
$user_roles = $this->currentUser
->getRoles();
foreach ($ignore_roles as $ignored_role) {
if (in_array($ignored_role, $user_roles)) {
return $this->adapter
->setTransactionState(NewRelicAdapterInterface::STATE_IGNORE);
}
}
}
$path = ltrim($this->currentPathStack
->getPath(), '/');
if (!empty($exclude_urls)) {
if (!$this->pathMatcher
->matchPath($path, $exclude_urls)) {
return $this->adapter
->setTransactionState(NewRelicAdapterInterface::STATE_IGNORE);
}
}
if (!empty($ignore_urls)) {
if ($this->pathMatcher
->matchPath($path, $ignore_urls)) {
return $this->adapter
->setTransactionState(NewRelicAdapterInterface::STATE_IGNORE);
}
}
if (!empty($bg_urls)) {
if ($this->pathMatcher
->matchPath($path, $bg_urls)) {
$this->adapter
->setTransactionState(NewRelicAdapterInterface::STATE_BACKGROUND);
}
}
if ($name = $event
->getRequest()->attributes
->get('_transaction_name')) {
$this->adapter
->setTransactionName($name);
}
$this->processedMasterRequest = TRUE;
}
}