You are here

public function Raven::setClient in Raven: Sentry Integration 8.2

Creates Sentry client based on config and any alter hooks.

2 calls to Raven::setClient()
Raven::__construct in src/Logger/Raven.php
Constructs a Raven log object.
Raven::__wakeup in src/Logger/Raven.php

File

src/Logger/Raven.php, line 142

Class

Raven
Logs events to Sentry.

Namespace

Drupal\raven\Logger

Code

public function setClient() {
  if (!class_exists('Raven_Client')) {

    // Sad raven.
    return;
  }
  $options = [
    'auto_log_stacks' => $this->config
      ->get('stack'),
    'curl_method' => 'async',
    'dsn' => empty($_SERVER['SENTRY_DSN']) ? $this->config
      ->get('client_key') : $_SERVER['SENTRY_DSN'],
    'environment' => empty($_SERVER['SENTRY_ENVIRONMENT']) ? $this->environment : $_SERVER['SENTRY_ENVIRONMENT'],
    'processors' => [
      'Drupal\\raven\\Processor\\SanitizeDataProcessor',
    ],
    'timeout' => $this->config
      ->get('timeout'),
    'message_limit' => $this->config
      ->get('message_limit'),
    'trace' => $this->config
      ->get('trace'),
    'verify_ssl' => TRUE,
  ];
  $ssl = $this->config
    ->get('ssl');

  // Verify against a CA certificate.
  if ($ssl == 'ca_cert') {
    $options['ca_cert'] = realpath($this->config
      ->get('ca_cert'));
  }
  elseif ($ssl == 'no_verify_ssl') {
    $options['verify_ssl'] = FALSE;
  }
  if (!empty($_SERVER['SENTRY_RELEASE'])) {
    $options['release'] = $_SERVER['SENTRY_RELEASE'];
  }
  elseif (!empty($this->config
    ->get('release'))) {
    $options['release'] = $this->config
      ->get('release');
  }

  // Proxy configuration.
  $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']];
      }
    }
  }

  // Disable the default breadcrumb handler because Drupal error handler
  // mistakes it for the calling code when errors are thrown.
  $options['install_default_breadcrumb_handlers'] = FALSE;
  $this->moduleHandler
    ->alter('raven_options', $options);
  try {
    $this->client = new \Raven_Client($options);
  } catch (\InvalidArgumentException $e) {

    // Raven is incorrectly configured.
    return;
  }

  // Set default user context to avoid sending session ID to Sentry.
  $this->client
    ->user_context([
    'id' => $this->currentUser ? $this->currentUser
      ->id() : 0,
  ]);
  if ($this->requestStack && ($request = $this->requestStack
    ->getCurrentRequest())) {
    $this->client
      ->user_context([
      'ip_address' => $request
        ->getClientIp(),
    ]);
  }
}