You are here

RavenConfigForm.php in Raven: Sentry Integration 3.x

Same filename and directory in other branches
  1. 8.2 src/Form/RavenConfigForm.php
  2. 8 src/Form/RavenConfigForm.php

Namespace

Drupal\raven\Form

File

src/Form/RavenConfigForm.php
View source
<?php

namespace Drupal\raven\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;

/**
 * Implements a Raven Config form.
 */
class RavenConfigForm {

  /**
   * Builds Raven config form.
   */
  public static function buildForm(array &$form) {
    $config = \Drupal::config('raven.settings');
    $form['raven'] = [
      '#type' => 'details',
      '#title' => t('Sentry'),
      '#tree' => TRUE,
      '#open' => TRUE,
    ];
    $form['raven']['js'] = [
      '#type' => 'details',
      '#title' => t('JavaScript'),
      '#open' => TRUE,
    ];
    $form['raven']['js']['javascript_error_handler'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable JavaScript error handler'),
      '#description' => t('Check to capture JavaScript errors (if user has the <a target="_blank" href=":url">send JavaScript errors to Sentry</a> permission).', [
        ':url' => Url::fromRoute('user.admin_permissions', [], [
          'fragment' => 'module-raven',
        ])
          ->toString(),
      ]),
      '#default_value' => $config
        ->get('javascript_error_handler'),
    ];
    $form['raven']['js']['public_dsn'] = [
      '#type' => 'textfield',
      '#title' => t('Sentry DSN'),
      '#default_value' => $config
        ->get('public_dsn'),
      '#description' => t('Sentry client key for current site. This setting can be overridden with the SENTRY_DSN environment variable.'),
    ];
    $form['raven']['js']['browser_traces_sample_rate'] = [
      '#type' => 'number',
      '#title' => t('Browser performance tracing sample rate'),
      '#default_value' => $config
        ->get('browser_traces_sample_rate'),
      '#description' => t('Set the browser performance tracing sample rate, or set to 0 to disable performance tracing.'),
      '#min' => 0,
      '#max' => 1,
      '#step' => 1.0E-6,
    ];
    $form['raven']['js']['auto_session_tracking'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable auto session tracking'),
      '#description' => t('Check to monitor release health by sending a session event to Sentry for each page load; only active if a release is specified below or via the SENTRY_RELEASE environment variable.'),
      '#default_value' => $config
        ->get('auto_session_tracking'),
    ];
    $form['raven']['js']['send_client_reports'] = [
      '#type' => 'checkbox',
      '#title' => t('Send client reports'),
      '#description' => t('Send client report (e.g. number of discarded events), if any, when tab is hidden or closed.'),
      '#default_value' => $config
        ->get('send_client_reports'),
    ];
    $form['raven']['php'] = [
      '#type' => 'details',
      '#title' => t('PHP'),
      '#open' => TRUE,
    ];
    $form['raven']['php']['client_key'] = [
      '#type' => 'textfield',
      '#title' => t('Sentry DSN'),
      '#default_value' => $config
        ->get('client_key'),
      '#description' => t('Sentry client key for current site. This setting can be overridden with the SENTRY_DSN environment variable.'),
    ];

    // "0" is not a valid checkbox option.
    $log_levels = [];
    foreach (RfcLogLevel::getLevels() as $key => $value) {
      $log_levels[$key + 1] = $value;
    }
    $form['raven']['php']['log_levels'] = [
      '#type' => 'checkboxes',
      '#title' => t('Log levels'),
      '#default_value' => $config
        ->get('log_levels'),
      '#description' => t('Check the log levels that should be captured by Sentry.'),
      '#options' => $log_levels,
    ];
    $form['raven']['php']['ignored_channels'] = [
      '#type' => 'textarea',
      '#title' => t('Ignored channels'),
      '#description' => t('A list of log channels for which messages will not be sent to Sentry (one channel per line). Commonly-configured log channels include <em>access denied</em> for 403 errors and <em>page not found</em> for 404 errors.'),
      '#default_value' => implode("\n", $config
        ->get('ignored_channels') ?: []),
    ];
    $form['raven']['php']['fatal_error_handler'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable fatal error handler'),
      '#description' => t('Check to capture fatal PHP errors.'),
      '#default_value' => $config
        ->get('fatal_error_handler'),
    ];
    $form['raven']['php']['drush_error_handler'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Drush error handler'),
      '#description' => t('Check to capture errors thrown by Drush commands.'),
      '#default_value' => $config
        ->get('drush_error_handler'),
    ];
    $form['raven']['php']['stack'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable stacktraces'),
      '#default_value' => $config
        ->get('stack'),
      '#description' => t('Check to add stacktraces to reports.'),
    ];
    $form['raven']['php']['trace'] = [
      '#type' => 'checkbox',
      '#title' => t('Reflection tracing in stacktraces'),
      '#default_value' => $config
        ->get('trace'),
      '#description' => t('Check to enable reflection tracing (function calling arguments) in stacktraces. Warning: This setting allows sensitive data to be logged by Sentry! To enable for exception stacktraces, PHP configuration flag zend.exception_ignore_args must be disabled.'),
    ];
    $form['raven']['php']['send_user_data'] = [
      '#type' => 'checkbox',
      '#title' => t('Send user data to Sentry'),
      '#default_value' => $config
        ->get('send_user_data'),
      '#description' => t('Check to send user email and username to Sentry with each event. Warning: User data can still be sent to Sentry even when this setting is disabled, for example as part of a log message or request body. Custom code is required to scrub personally-identifying information from events before they are sent.'),
    ];
    $form['raven']['php']['send_request_body'] = [
      '#type' => 'checkbox',
      '#title' => t('Send request body to Sentry'),
      '#default_value' => $config
        ->get('send_request_body'),
      '#description' => t('Check to send the request body (POST data) to Sentry. Warning: This setting allows sensitive data to be logged by Sentry!'),
    ];
    $form['raven']['php']['rate_limit'] = [
      '#type' => 'number',
      '#title' => t('Rate limit'),
      '#default_value' => $config
        ->get('rate_limit'),
      '#description' => t('Maximum log events sent to Sentry per-request or per-execution. To disable the limit, set to zero. You may need to set a limit if you have buggy code which generates a large number of log messages.'),
      '#min' => 0,
      '#step' => 1,
    ];
    $form['raven']['php']['performance'] = [
      '#type' => 'details',
      '#title' => t('Performance tracing'),
      '#open' => TRUE,
    ];
    $form['raven']['php']['performance']['request_tracing'] = [
      '#type' => 'checkbox',
      '#title' => t('Request/response performance tracing'),
      '#default_value' => $config
        ->get('request_tracing'),
      '#description' => t('Check to enable performance tracing on the server side for each request/response, excluding pages served from the page cache.'),
    ];
    $form['raven']['php']['performance']['database_tracing'] = [
      '#type' => 'checkbox',
      '#title' => t('Database performance tracing'),
      '#default_value' => $config
        ->get('database_tracing'),
      '#description' => t('Check to add database queries to request/response performance tracing.'),
    ];
    $form['raven']['php']['performance']['twig_tracing'] = [
      '#type' => 'checkbox',
      '#title' => t('Twig performance tracing'),
      '#default_value' => $config
        ->get('twig_tracing'),
      '#description' => t('Check to add Twig templates to request/response performance tracing.'),
    ];
    $form['raven']['php']['performance']['traces_sample_rate'] = [
      '#type' => 'number',
      '#title' => t('Performance tracing sample rate'),
      '#default_value' => $config
        ->get('traces_sample_rate'),
      '#description' => t('Set the performance tracing sample rate, or set to 0 to disable performance tracing.'),
      '#min' => 0,
      '#max' => 1,
      '#step' => 1.0E-6,
    ];
    $form['raven']['environment'] = [
      '#type' => 'textfield',
      '#title' => t('Environment'),
      '#default_value' => $config
        ->get('environment'),
      '#description' => t('The environment in which this site is running (leave blank to use kernel.environment parameter). This setting can be overridden with the SENTRY_ENVIRONMENT environment variable.'),
    ];
    $form['raven']['release'] = [
      '#type' => 'textfield',
      '#title' => t('Release'),
      '#default_value' => $config
        ->get('release'),
      '#description' => t('The release this site is running (could be a version or commit hash). This setting can be overridden with the SENTRY_RELEASE environment variable.'),
    ];
    $form['#submit'][] = 'Drupal\\raven\\Form\\RavenConfigForm::submitForm';
  }

  /**
   * Submits Raven config form.
   */
  public static function submitForm(array &$form, FormStateInterface $form_state) {
    \Drupal::configFactory()
      ->getEditable('raven.settings')
      ->set('client_key', $form_state
      ->getValue([
      'raven',
      'php',
      'client_key',
    ]))
      ->set('fatal_error_handler', $form_state
      ->getValue([
      'raven',
      'php',
      'fatal_error_handler',
    ]))
      ->set('drush_error_handler', $form_state
      ->getValue([
      'raven',
      'php',
      'drush_error_handler',
    ]))
      ->set('log_levels', $form_state
      ->getValue([
      'raven',
      'php',
      'log_levels',
    ]))
      ->set('stack', $form_state
      ->getValue([
      'raven',
      'php',
      'stack',
    ]))
      ->set('trace', $form_state
      ->getValue([
      'raven',
      'php',
      'trace',
    ]))
      ->set('send_user_data', $form_state
      ->getValue([
      'raven',
      'php',
      'send_user_data',
    ]))
      ->set('send_request_body', $form_state
      ->getValue([
      'raven',
      'php',
      'send_request_body',
    ]))
      ->set('rate_limit', $form_state
      ->getValue([
      'raven',
      'php',
      'rate_limit',
    ]))
      ->set('request_tracing', $form_state
      ->getValue([
      'raven',
      'php',
      'performance',
      'request_tracing',
    ]))
      ->set('database_tracing', $form_state
      ->getValue([
      'raven',
      'php',
      'performance',
      'database_tracing',
    ]))
      ->set('twig_tracing', $form_state
      ->getValue([
      'raven',
      'php',
      'performance',
      'twig_tracing',
    ]))
      ->set('traces_sample_rate', $form_state
      ->getValue([
      'raven',
      'php',
      'performance',
      'traces_sample_rate',
    ]))
      ->set('ignored_channels', array_map('trim', preg_split('/\\R/', $form_state
      ->getValue([
      'raven',
      'php',
      'ignored_channels',
    ]), -1, PREG_SPLIT_NO_EMPTY)))
      ->set('javascript_error_handler', $form_state
      ->getValue([
      'raven',
      'js',
      'javascript_error_handler',
    ]))
      ->set('public_dsn', $form_state
      ->getValue([
      'raven',
      'js',
      'public_dsn',
    ]))
      ->set('browser_traces_sample_rate', $form_state
      ->getValue([
      'raven',
      'js',
      'browser_traces_sample_rate',
    ]))
      ->set('auto_session_tracking', $form_state
      ->getValue([
      'raven',
      'js',
      'auto_session_tracking',
    ]))
      ->set('send_client_reports', $form_state
      ->getValue([
      'raven',
      'js',
      'send_client_reports',
    ]))
      ->set('environment', $form_state
      ->getValue([
      'raven',
      'environment',
    ]))
      ->set('release', $form_state
      ->getValue([
      'raven',
      'release',
    ]))
      ->save();
  }

}

Classes

Namesort descending Description
RavenConfigForm Implements a Raven Config form.