You are here

raven.module in Raven: Sentry Integration 3.x

Raven Drupal module.

File

raven.module
View source
<?php

/**
 * @file
 * Raven Drupal module.
 */
use Drupal\Component\Utility\Crypt;
use Drupal\raven\Form\RavenConfigForm;
use Sentry\SentrySdk;

/**
 * Implements hook_page_attachments().
 */
function raven_page_attachments(array &$page) {
  $config = \Drupal::config('raven.settings');
  if (!$config
    ->get('javascript_error_handler') || !\Drupal::currentUser()
    ->hasPermission('send javascript errors to sentry')) {
    return;
  }

  // Other modules can attach Sentry browser client options to the page.
  if (!isset($page['#attached']['drupalSettings']['raven']['options'])) {
    $page['#attached']['drupalSettings']['raven']['options'] = new \stdClass();
  }
  $page['#attached']['drupalSettings']['raven']['options']->dsn = empty($_SERVER['SENTRY_DSN']) ? $config
    ->get('public_dsn') : $_SERVER['SENTRY_DSN'];
  $page['#attached']['drupalSettings']['raven']['options']->environment = empty($_SERVER['SENTRY_ENVIRONMENT']) ? $config
    ->get('environment') ?: \Drupal::getContainer()
    ->getParameter('kernel.environment') : $_SERVER['SENTRY_ENVIRONMENT'];
  if (!empty($_SERVER['SENTRY_RELEASE'])) {
    $page['#attached']['drupalSettings']['raven']['options']->release = $_SERVER['SENTRY_RELEASE'];
  }
  elseif ($release = $config
    ->get('release')) {
    $page['#attached']['drupalSettings']['raven']['options']->release = $release;
  }
  $page['#attached']['drupalSettings']['raven']['options']->integrations = [];
  if ($traces = $config
    ->get('browser_traces_sample_rate')) {
    $page['#attached']['drupalSettings']['raven']['options']->tracesSampleRate = $traces;
  }
  $page['#attached']['drupalSettings']['raven']['options']->autoSessionTracking = $config
    ->get('auto_session_tracking');
  $page['#attached']['drupalSettings']['raven']['options']->sendClientReports = $config
    ->get('send_client_reports');
  $page['#attached']['library'][] = 'raven/raven';

  // Add performance tracing meta tag for attaching traces to errors.
  if (!class_exists(SentrySdk::class) || !SentrySdk::getCurrentHub()
    ->getSpan()) {
    return;
  }
  $placeholder = Crypt::randomBytesBase64();
  $page['#attached']['html_head'][] = [
    [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'sentry-trace',
        'content' => $placeholder,
      ],
      '#attached' => [
        'placeholders' => [
          $placeholder => [
            '#create_placeholder' => TRUE,
            '#lazy_builder' => [
              'raven.request_subscriber:getTraceparent',
              [],
            ],
          ],
        ],
      ],
    ],
    'raven_sentry_trace',
  ];
}

/**
 * Implements hook_form_system_logging_settings_alter().
 */
function raven_form_system_logging_settings_alter(array &$form) {
  RavenConfigForm::buildForm($form);
}

Functions

Namesort descending Description
raven_form_system_logging_settings_alter Implements hook_form_system_logging_settings_alter().
raven_page_attachments Implements hook_page_attachments().