You are here

public function Raven::getClient in Raven: Sentry Integration 3.x

Returns existing or new Sentry client, or NULL if it could not be created.

4 calls to Raven::getClient()
Raven::flush in src/Logger/Raven.php
Sends all unsent events.
Raven::log in src/Logger/Raven.php
Raven::onConsoleError in src/Logger/Raven.php
Captures console error events.
Raven::__construct in src/Logger/Raven.php
Constructs a Raven log object.

File

src/Logger/Raven.php, line 141

Class

Raven
Logs events to Sentry.

Namespace

Drupal\raven\Logger

Code

public function getClient($force_new = FALSE) : ?ClientInterface {

  // Gracefully handle missing or wrong version of Sentry SDK.
  if (!class_exists(SentrySdk::class) || !method_exists(Event::class, 'createEvent')) {
    return NULL;
  }

  // Is the client already initialized?
  if (!$force_new && ($client = SentrySdk::getCurrentHub()
    ->getClient())) {
    return $client;
  }
  $config = $this->configFactory
    ->get('raven.settings');
  $options = [
    'default_integrations' => FALSE,
    'dsn' => empty($_SERVER['SENTRY_DSN']) ? $config
      ->get('client_key') : $_SERVER['SENTRY_DSN'],
    'environment' => empty($_SERVER['SENTRY_ENVIRONMENT']) ? $this->environment : $_SERVER['SENTRY_ENVIRONMENT'],
  ];
  if ($config
    ->get('stack')) {
    $options['attach_stacktrace'] = TRUE;
  }
  if ($config
    ->get('fatal_error_handler')) {
    $options['integrations'][] = new FatalErrorListenerIntegration();
  }
  $options['integrations'][] = new RequestIntegration();
  $options['integrations'][] = new TransactionIntegration();
  $options['integrations'][] = new FrameContextifierIntegration();
  $options['integrations'][] = new EnvironmentIntegration();
  $options['integrations'][] = new SanitizeIntegration();
  if (!$config
    ->get('trace')) {
    $options['integrations'][] = new RemoveExceptionFrameVarsIntegration();
  }
  if (!empty($_SERVER['SENTRY_RELEASE'])) {
    $options['release'] = $_SERVER['SENTRY_RELEASE'];
  }
  elseif (!empty($config
    ->get('release'))) {
    $options['release'] = $config
      ->get('release');
  }
  if (!$config
    ->get('send_request_body')) {
    $options['max_request_body_size'] = 'none';
  }
  if ($config
    ->get('traces_sample_rate')) {
    $options['traces_sample_rate'] = $config
      ->get('traces_sample_rate');
  }

  // Proxy configuration (DSN is null before install).
  $parsed_dsn = parse_url($options['dsn'] ?? '');
  if (!empty($parsed_dsn['host']) && !empty($parsed_dsn['scheme'])) {
    $http_client_config = $this->settings
      ->get('http_client_config', []);
    if (!empty($http_client_config['proxy'][$parsed_dsn['scheme']])) {
      $no_proxy = isset($http_client_config['proxy']['no']) ? $http_client_config['proxy']['no'] : [];

      // No need to configure proxy if Sentry host is on proxy bypass list.
      if (!in_array($parsed_dsn['host'], $no_proxy, TRUE)) {
        $options['http_proxy'] = $http_client_config['proxy'][$parsed_dsn['scheme']];
      }
    }
  }
  $this->moduleHandler
    ->alter('raven_options', $options);
  try {
    \Sentry\init($options);
  } catch (\InvalidArgumentException $e) {
    return NULL;
  }

  // Set default user context.
  \Sentry\configureScope(function (Scope $scope) use ($config) : void {
    $user = [
      'id' => $this->currentUser ? $this->currentUser
        ->id() : 0,
    ];
    if ($this->requestStack && ($request = $this->requestStack
      ->getCurrentRequest())) {
      $user['ip_address'] = $request
        ->getClientIp();
    }
    if ($this->currentUser && $config
      ->get('send_user_data')) {
      $user['email'] = $this->currentUser
        ->getEmail();
      $user['username'] = $this->currentUser
        ->getAccountName();
    }
    $scope
      ->setUser($user);
  });
  return SentrySdk::getCurrentHub()
    ->getClient();
}