You are here

function raven_get_client in Raven: Sentry Integration 7.4

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

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

Return value

\Sentry\ClientInterface|null Sentry PHP client 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 287
Allows to track errors to Sentry server.

Code

function raven_get_client() : ?ClientInterface {
  if (!class_exists(SentrySdk::class)) {
    return NULL;
  }
  if ($client = SentrySdk::getCurrentHub()
    ->getClient()) {
    return $client;
  }
  $options = [
    'default_integrations' => FALSE,
    'dsn' => empty($_SERVER['SENTRY_DSN']) ? variable_get('raven_dsn', NULL) : $_SERVER['SENTRY_DSN'],
  ];
  if (variable_get('raven_stack', TRUE)) {
    $options['attach_stacktrace'] = TRUE;
  }
  if (variable_get('raven_fatal_error_handler', TRUE)) {
    $options['integrations'][] = new FatalErrorListenerIntegration();
  }
  $options['integrations'][] = new RequestIntegration();
  $options['integrations'][] = new TransactionIntegration();
  $options['integrations'][] = new FrameContextifierIntegration();
  $options['integrations'][] = new EnvironmentIntegration();
  $options['integrations'][] = new RavenSanitizeIntegration();
  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;
  }
  if (!variable_get('raven_send_request_body', FALSE)) {
    $options['max_request_body_size'] = 'none';
  }

  // Allow other modules to alter $options before passing into Raven client.
  drupal_alter('raven_options', $options);
  try {
    \Sentry\init($options);
  } catch (InvalidArgumentException $e) {

    // Raven is incorrectly configured.
    return NULL;
  }
  \Sentry\configureScope(function (Scope $scope) : void {
    global $user;
    $context['id'] = $user ? $user->uid : 0;
    $context['ip_address'] = ip_address();
    $scope
      ->setUser($context);
  });
  return SentrySdk::getCurrentHub()
    ->getClient();
}