You are here

public function NewRelicRequestSubscriber::onRequest in New Relic 2.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/NewRelicRequestSubscriber.php \Drupal\new_relic_rpm\EventSubscriber\NewRelicRequestSubscriber::onRequest()
  2. 2.0.x src/EventSubscriber/NewRelicRequestSubscriber.php \Drupal\new_relic_rpm\EventSubscriber\NewRelicRequestSubscriber::onRequest()

Set the desired transaction state and name.

Naming is based on the current path and route.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The current response event for the page.

File

src/EventSubscriber/NewRelicRequestSubscriber.php, line 101

Class

NewRelicRequestSubscriber
Request event listener to set transaction name and flag ignore/background.

Namespace

Drupal\new_relic_rpm\EventSubscriber

Code

public function onRequest(GetResponseEvent $event) {

  // If this is a sub request, only process it if there was no master
  // request yet. In that case, it is probably a page not found or access
  // denied page.
  if ($event
    ->getRequestType() !== HttpKernelInterface::MASTER_REQUEST && $this->processedMasterRequest) {
    return;
  }
  $config = $this->configFactory
    ->get('new_relic_rpm.settings');
  $ignore_roles = $config
    ->get('ignore_roles');
  $ignore_urls = $config
    ->get('ignore_urls');
  $bg_urls = $config
    ->get('bg_urls');
  $exclude_urls = $config
    ->get('exclusive_urls');
  $disable_autorum = $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 the path was not ignored, set the transaction mame.
  if ($name = $event
    ->getRequest()->attributes
    ->get('_transaction_name')) {
    $this->adapter
      ->setTransactionName($name);
  }
  $this->processedMasterRequest = TRUE;
}