You are here

function raven_test_page_attachments in Raven: Sentry Integration 8.2

Same name and namespace in other branches
  1. 3.x tests/modules/raven_test.module \raven_test_page_attachments()

Implements hook_page_attachments().

File

tests/modules/raven_test.module, line 13
Raven test module.

Code

function raven_test_page_attachments(array &$attachments) {
  $client = \Drupal::service('logger.raven')->client;
  $client
    ->setSendCallback(function ($data) use (&$attachments) {
    if (!empty($data['logger']) && !empty($data['sentry.interfaces.Message']['formatted'])) {
      $attachments['#attached']['http_header'][] = [
        $data['logger'],
        $data['sentry.interfaces.Message']['formatted'],
        FALSE,
      ];
      $last_frame = end($data['stacktrace']['frames']);
      $attachments['#attached']['http_header'][] = [
        'X-Stacktrace-File',
        $last_frame['filename'],
        FALSE,
      ];
      if (isset($data['request']['cookies'])) {
        $attachments['#attached']['http_header'][] = [
          $data['logger'] . '-Cookies',
          Json::encode($data['request']['cookies']),
          FALSE,
        ];
      }
    }
    if (!empty($data['exception']['values'][0]['value'])) {
      if (preg_match('/Allowed memory size of ([0-9]+) bytes exhausted/', $data['exception']['values'][0]['value'], $matches)) {
        echo $matches[1];
      }
    }
  });
  \Drupal::logger('X-Logged')
    ->error('Logged');
  \Drupal::logger('X-Not-Logged')
    ->error('Not logged');
  \Drupal::logger('X-Logged')
    ->error('Logged');
  if ($memory_limit = \Drupal::request()->query
    ->get('memory_limit')) {
    $client = \Drupal::service('logger.raven')->client;

    // Output number of pending requests at end of request.
    drupal_register_shutdown_function(function () use (&$client) {
      $reflectedPendingEvents = (new \ReflectionObject($client))
        ->getProperty('_pending_events');
      $reflectedPendingEvents
        ->setAccessible(TRUE);
      $pendingEvents = $reflectedPendingEvents
        ->getValue($client);
      echo count($pendingEvents);
      $reflectedCurlHandler = (new \ReflectionObject($client))
        ->getProperty('_curl_handler');
      $reflectedCurlHandler
        ->setAccessible(TRUE);
      $curlHandler = $reflectedCurlHandler
        ->getValue($client);
      $reflectedPendingRequests = (new \ReflectionObject($curlHandler))
        ->getProperty('requests');
      $reflectedPendingRequests
        ->setAccessible(TRUE);
      $pendingRequests = $reflectedPendingRequests
        ->getValue($curlHandler);
      echo count($pendingRequests);
    });
    ini_set('memory_limit', (int) $memory_limit);
    while (TRUE) {
      $attachments[] = 1;
    }
  }
  $attachments['#cache']['contexts'][] = 'url.query_args:memory_limit';
}