This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraven.module
420 lines (367 loc) · 10.3 KB
/
raven.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* @file
* Allows to track errors to Sentry server.
*/
define('RAVEN_PERMISSION', 'administer raven');
/**
* Implements hook_menu().
*/
function raven_menu() {
$items = array();
$items['admin/config/development/raven'] = array(
'title' => 'Raven-php client settings',
'description' => 'Administer raven-php settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('raven_settings_form'),
'access arguments' => array(RAVEN_PERMISSION),
'file' => 'raven.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function raven_permission() {
return array(
RAVEN_PERMISSION => array(
'title' => t('Administer raven-php'),
'description' => t('Access the raven-php settings page.'),
),
);
}
/**
* Implements hook_libraries_info().
*/
function raven_libraries_info() {
$libraries['raven'] = array(
'name' => 'Raven-php library',
'vendor url' => 'https://github.com/getsentry/raven-php',
'download url' => 'https://github.com/getsentry/raven-php/releases',
'path' => 'lib/Raven',
'version arguments' => array(
'file' => 'lib/Raven/Client.php',
'pattern' => '#const\s+VERSION\s*=\s*\'([0-9a-z._-]+)\';#',
'lines' => 25,
),
'files' => array(
'php' => array(
'Client.php',
'Compat.php',
'Context.php',
'ErrorHandler.php',
'Processor.php',
'SanitizeDataProcessor.php',
'Serializer.php',
'Stacktrace.php',
'Util.php',
),
),
);
return $libraries;
}
/**
* Implements hook_init().
*/
function raven_init() {
if (!variable_get('raven_enabled', FALSE)) {
return;
}
$client = _raven_get_client();
if (!$client) {
return;
}
// Get enabled error handlers.
$exception_handler = variable_get('raven_exception_handler', TRUE);
$fatal_error_handler = variable_get('raven_fatal_error_handler', TRUE);
$error_handler = variable_get('raven_error_handler', TRUE);
// Bind the logged in user.
$user = array();
drupal_alter('raven_user', $user);
$client->user_context($user);
// Tag the request with something interesting.
$tags = array();
drupal_alter('raven_tags', $tags);
$client->tags_context($tags);
// Provide a bit of additional context.
$extra = array();
drupal_alter('raven_extra', $extra);
$client->extra_context($extra);
$raven_error_handler = _raven_get_error_handler();
if ($exception_handler) {
$raven_error_handler->registerExceptionHandler();
}
if ($fatal_error_handler) {
$reserved_memory = variable_get('raven_fatal_error_handler_memory', 2.5 * 1024);
$raven_error_handler->registerShutdownFunction($reserved_memory);
}
if ($error_handler) {
$old_error_handler = set_error_handler('_raven_error_handler');
$GLOBALS['_raven_old_error_handler'] = $old_error_handler;
}
}
/**
* PHP error handler.
*
* @param int $code
* The level of the error raised.
* @param string $message
* The error message.
* @param string $file
* The filename that the error was raised in.
* @param int $line
* The line number the error was raised at.
* @param array $context
* An array of every variable that existed in the scope the error was
* triggered in.
*/
function _raven_error_handler($code, $message, $file = '', $line = 0, array $context = array()) {
$error_levels = _raven_get_enabled_error_levels();
if ($error_levels & $code & error_reporting()) {
$filter = array(
'process' => TRUE,
'code' => $code,
'message' => $message,
'file' => $file,
'line' => $line,
'context' => $context,
);
drupal_alter('raven_error_filter', $filter);
if ($filter['process']) {
$raven_error_handler = _raven_get_error_handler();
$e = new ErrorException($message, 0, $code, $file, $line);
$raven_error_handler->handleException($e, TRUE, $context);
}
}
$old_error_handler = $GLOBALS['_raven_old_error_handler'];
if ($old_error_handler) {
call_user_func($old_error_handler, $code, $message, $file, $line, $context);
}
}
/**
* Returns PHP error levels which should be logged.
*
* @return int
* Combination of the error levels, joined with the binary OR (|) operator.
*/
function _raven_get_enabled_error_levels() {
static $error_levels;
if (!isset($error_levels)) {
$error_levels = 0;
$enabled_error_types = variable_get('raven_error_levels', array());
foreach ($enabled_error_types as $level => $enabled) {
if ($enabled) {
$error_levels |= $level;
}
}
}
return $error_levels;
}
/**
* Implements hook_watchdog().
*/
function raven_watchdog($log_entry) {
if (!variable_get('raven_enabled', FALSE)) {
return;
}
if (!variable_get('raven_watchdog_handler', FALSE)) {
return;
}
// Do not process php errors.
if ($log_entry['type'] === 'php') {
return;
}
$_watchdog_levels = variable_get('raven_error_levels', array());
$watchdog_levels = array();
foreach ($_watchdog_levels as $level) {
$watchdog_levels[] = $level - 1;
}
if (!in_array($log_entry['severity'], $watchdog_levels)) {
return;
}
$filter = array(
'process' => TRUE,
'log_entry' => $log_entry,
);
drupal_alter('raven_watchdog_filter', $filter);
if (!$filter['process']) {
return;
}
$levels_map = array(
WATCHDOG_ALERT => Raven_Client::INFO,
WATCHDOG_CRITICAL => Raven_Client::FATAL,
WATCHDOG_DEBUG => Raven_Client::DEBUG,
WATCHDOG_EMERGENCY => Raven_Client::FATAL,
WATCHDOG_ERROR => Raven_Client::ERROR,
WATCHDOG_INFO => Raven_Client::INFO,
WATCHDOG_NOTICE => Raven_Client::WARNING,
WATCHDOG_WARNING => Raven_Client::WARNING,
);
$variables = $log_entry['variables'];
if (!$variables) {
$variables = array();
}
$data = array(
'level' => $levels_map[$log_entry['severity']],
'message' => strip_tags(format_string($log_entry['message'], $variables)),
'sentry.interfaces.Message' => array(
'message' => $log_entry['message'],
'params' => $log_entry['variables'],
),
'tags' => array(
'type' => $log_entry['type'],
),
'extra' => array(
'link' => $log_entry['link'],
'request_uri' => $log_entry['request_uri'],
'referer' => $log_entry['referer'],
'ip' => $log_entry['ip'],
),
'logger' => 'watchdog',
);
$client = _raven_get_client();
if (!$client) {
return;
}
$client->capture($data, NULL);
}
/**
* Returns an instance of the Raven-php client.
*
* @return Raven_Client
* Raven-php client library instance.
*/
function _raven_get_client() {
static $client;
static $message_sent = FALSE;
if (!isset($client)) {
$library = libraries_load('raven');
if (!$library['loaded']) {
// This function can be called multiple times,
// so prevent multiple messages.
if (!$message_sent) {
drupal_set_message(t('Raven-php library can not be loaded. Check <a href="!url">reports</a> for more details.', array(
'!url' => url('admin/reports/status'),
)), 'error');
$message_sent = TRUE;
}
return NULL;
}
// Prepare config.
$dsn = variable_get('raven_dsn');
$timeout = variable_get('raven_timeout', 2);
$stack = variable_get('raven_stack', TRUE);
$trace = variable_get('raven_trace', FALSE);
// Instantiate a new client with a compatible DSN.
$client = new Raven_Client($dsn, array(
'timeout' => $timeout,
'auto_log_stacks' => $stack,
'trace' => $trace,
'tags' => array(
'php_version' => phpversion(),
),
));
}
return $client;
}
/**
* Returns an instance of the Reven-php error handler.
*
* @return Raven_ErrorHandler
* Raven-php error handler.
*/
function _raven_get_error_handler() {
static $handler;
if (!isset($handler)) {
$client = _raven_get_client();
if ($client) {
$handler = new Raven_ErrorHandler($client);
}
}
return $handler;
}
/**
* Returns supported PHP error levels and their labels.
*
* @return array
* An array contains supported PHP error levels and their labels.
*/
function raven_get_error_levels() {
$levels = array(
E_ERROR => 'E_ERROR',
E_WARNING => 'E_WARNING',
E_PARSE => 'E_PARSE',
// Note: E_NOTICE is not really useful for Drupal.
E_NOTICE => 'E_NOTICE',
E_CORE_ERROR => 'E_CORE_ERROR',
E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE',
// Note: E_STRICT is not really useful for Drupal.
E_STRICT => 'E_STRICT',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
$levels += array(
E_DEPRECATED => 'E_DEPRECATED',
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
);
}
return $levels;
}
/**
* Returns supported watchdog levels and their labels.
*
* @return array
* An array contains supported watchdog levels and their labels.
*/
function raven_get_watchdog_levels() {
$levels = array(
WATCHDOG_DEBUG => 'WATCHDOG_DEBUG',
WATCHDOG_INFO => 'WATCHDOG_INFO',
WATCHDOG_ALERT => 'WATCHDOG_ALERT',
WATCHDOG_NOTICE => 'WATCHDOG_NOTICE',
WATCHDOG_WARNING => 'WATCHDOG_WARNING',
WATCHDOG_ERROR => 'WATCHDOG_ERROR',
WATCHDOG_CRITICAL => 'WATCHDOG_CRITICAL',
WATCHDOG_EMERGENCY => 'WATCHDOG_EMERGENCY',
);
return $levels;
}
/**
* Implements hook_raven_user_alter().
*/
function raven_raven_user_alter(array &$variables) {
global $user;
if (user_is_logged_in()) {
$variables['id'] = $user->uid;
$variables['name'] = $user->name;
$variables['email'] = $user->mail;
$variables['roles'] = implode(', ', $user->roles);
}
}
/**
* Implements hook_raven_error_filter_alter().
*/
function raven_raven_error_filter_alter(array &$error) {
$known_errors = array();
drupal_alter('raven_known_php_errors', $known_errors);
// Filter known errors to prevent spamming the Sentry server.
foreach ($known_errors as $known_error) {
$check = TRUE;
foreach ($known_error as $key => $value) {
if ($error[$key] != $value) {
$check = FALSE;
break;
}
}
if ($check) {
$error['process'] = FALSE;
break;
}
}
}