You are here

public function RequestSubscriber::onRequest in Raven: Sentry Integration 3.x

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/RequestSubscriber.php \Drupal\raven\EventSubscriber\RequestSubscriber::onRequest()

Starts a transaction if performance tracing is enabled.

@todo In Drupal 9+ the event should actually be RequestEvent.

File

src/EventSubscriber/RequestSubscriber.php, line 82

Class

RequestSubscriber
Initializes Raven logger so Sentry functions can be called.

Namespace

Drupal\raven\EventSubscriber

Code

public function onRequest(KernelEvent $event) {
  $method = method_exists($event, 'isMainRequest') ? 'isMainRequest' : 'isMasterRequest';
  if (!$event
    ->{$method}() || !$this->configFactory) {
    return;
  }
  $config = $this->configFactory
    ->get('raven.settings');
  if (!$config
    ->get('request_tracing') || !$this->logger || !$this->logger
    ->getClient()) {
    return;
  }
  $request = $event
    ->getRequest();
  $sentryTraceHeader = $request->headers
    ->get('sentry-trace');
  $transactionContext = $sentryTraceHeader && method_exists(TransactionContext::class, 'fromSentryTrace') ? TransactionContext::fromSentryTrace($sentryTraceHeader) : new TransactionContext();
  $transactionContext
    ->setName($request
    ->getMethod() . ' ' . $request
    ->getUri());
  $transactionContext
    ->setOp('http.server');
  $transactionContext
    ->setTags([
    'http.method' => $request
      ->getMethod(),
    'http.url' => $request
      ->getUri(),
  ]);
  $transactionContext
    ->setStartTimestamp($this->time
    ->getRequestMicroTime());
  $this->transaction = \Sentry\startTransaction($transactionContext);
  SentrySdk::getCurrentHub()
    ->setSpan($this->transaction);
  if ($config
    ->get('database_tracing')) {
    foreach (Database::getAllConnectionInfo() as $key => $info) {
      Database::startLog('raven', $key);
    }
  }
}