You are here

function raven_get_client in Raven: Sentry Integration 7.3

Same name and namespace in other branches
  1. 7.4 raven.module \raven_get_client()
  2. 7.2 raven.module \raven_get_client()

Returns the Sentry PHP client instance, or NULL if it could not be created.

Return value

Raven_Client|null Raven PHP client library instance.

8 calls to raven_get_client()
drush_raven_capture_message in ./raven.drush.inc
Sends a test message to Sentry.
raven_extra_context in ./raven.module
Appends additional context.
raven_flush in ./raven.module
Sends all unsent events.
raven_init in ./raven.module
Implements hook_init().
raven_requirements in ./raven.install
Implements hook_requirements().

... See full list

File

./raven.module, line 290
Allows to track errors to Sentry server.

Code

function raven_get_client() {
  global $user;
  static $client;
  if (!isset($client)) {
    if (!raven_libraries_load()) {
      return NULL;
    }

    // Prepare config.
    $dsn = empty($_SERVER['SENTRY_DSN']) ? variable_get('raven_dsn', NULL) : $_SERVER['SENTRY_DSN'];
    $timeout = variable_get('raven_timeout', 2);
    $message_limit = variable_get('raven_message_limit', 2048);
    $stack = variable_get('raven_stack', TRUE);
    $trace = variable_get('raven_trace', FALSE);

    // Build the field sanitization regular expression.
    $fields = array(
      'SESS',
      'key',
      'token',
      'pass',
      'authorization',
      'password',
      'passwd',
      'secret',
      'password_confirmation',
      'card_number',
      'auth_pw',
    );
    drupal_alter('raven_sanitize_fields', $fields);
    $fields_re = '/(' . implode('|', $fields) . ')/i';
    $options = array(
      'timeout' => $timeout,
      'message_limit' => $message_limit,
      'auto_log_stacks' => $stack,
      'trace' => $trace,
      'processorOptions' => array(
        'RavenSanitizeCookieProcessor' => array(
          'fields_re' => $fields_re,
        ),
        'Raven_Processor_SanitizeDataProcessor' => array(
          'fields_re' => $fields_re,
        ),
      ),
      'curl_method' => 'async',
      'verify_ssl' => TRUE,
    );

    // Ease the upgrade path by checking if class exists.
    if (class_exists('Raven_Processor_SanitizeDataProcessor') && class_exists('RavenSanitizeCookieProcessor')) {
      $options['processors'] = array(
        'RavenSanitizeCookieProcessor',
        'Raven_Processor_SanitizeDataProcessor',
      );
    }
    if ($environment = empty($_SERVER['SENTRY_ENVIRONMENT']) ? variable_get('raven_environment') : $_SERVER['SENTRY_ENVIRONMENT']) {
      $options['environment'] = $environment;
    }
    if ($release = empty($_SERVER['SENTRY_RELEASE']) ? variable_get('raven_release') : $_SERVER['SENTRY_RELEASE']) {
      $options['release'] = $release;
    }
    $raven_ssl = variable_get('raven_ssl', 'verify_ssl');

    // Verify against a CA certificate.
    if ($raven_ssl == 'ca_cert') {
      $options['ca_cert'] = drupal_realpath(variable_get('raven_ca_cert', ''));
    }
    elseif ($raven_ssl == 'no_verify_ssl') {
      $options['verify_ssl'] = FALSE;
    }

    // Breadcrumbs confuse Drupal as to which line of code is throwing an error.
    $options['install_default_breadcrumb_handlers'] = FALSE;

    // Allow other modules to alter $options before passing into Raven client.
    drupal_alter('raven_options', $options);
    try {

      // Instantiate a new client with a compatible DSN.
      $client = new Raven_Client($dsn, $options);
    } catch (InvalidArgumentException $e) {

      // Raven is incorrectly configured.
      return NULL;
    }

    // Bind user context to prevent session ID from being sent as user ID.
    $client
      ->user_context(array(
      'id' => $user ? $user->uid : 0,
      'ip_address' => ip_address(),
    ));

    // Register fatal error handler.
    if (variable_get('raven_fatal_error_handler', TRUE)) {
      $handler = new Raven_ErrorHandler($client);
      $reserved_memory = variable_get('raven_fatal_error_handler_memory', 2.5 * 1024);
      $handler
        ->registerShutdownFunction($reserved_memory);

      // Register shutdown function again for fatal error via async.
      register_shutdown_function(array(
        $client,
        'onShutdown',
      ));
    }
  }
  return $client;
}