-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathairbrake.module
212 lines (186 loc) · 7.21 KB
/
airbrake.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
<?php
/*
* Implements hook_boot to run at drupal bootstrap and catch
* all exceptions.
*/
function airbrake_init() {
// Catch all exceptions
// if (($library = libraries_load('airbrake')) && !empty($library['loaded'])) {
// Airbrake\EventHandler::start(variable_get('airbrake_key', ''));
// }
}
function airbrake_watchdog($log) {
$notify_on = variable_get('airbrake_conditions', array(WATCHDOG_EMERGENCY, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR));
if (in_array($log['severity'], $notify_on)) {
$airbrake['component'] = $log['type'];
$airbrake['environmentName'] = variable_get('airbrake_environment', 'unset');
// just in case there's a field named "password" anywhere, don't send it.
$session = $_SESSION;
unset($session['password']);
$airbrake['sessionData'] = $session;
$get = $_GET;
unset($get['password']);
$airbrake['getData'] = $get;
$post = $_POST;
unset($post['password']);
$airbrake['postData'] = $post;
if (($library = libraries_load('airbrake')) && !empty($library['loaded'])) {
$config = new Airbrake\Configuration(variable_get('airbrake_key', ''), $airbrake);
$client = new Airbrake\Client($config);
$response = $client->notifyOnError($log['message']);
// Build the http request instead of using one of the built-in methods of the library
// so we can use drupal_http_request to make the request.
$backtrace = debug_backtrace();
if (count($backtrace) > 1) {
array_shift($backtrace);
}
$notice = new Airbrake\Notice();
$notice->load(array(
'errorClass' => 'Drupal Error',
'backtrace' => $backtrace,
'errorMessage' => strip_tags(format_string($log['message'], $log['variables'])),
));
$headers = array(
'Accept' => 'text/xml, application/xml',
'Content-Type' => 'text/xml'
);
$opts = array(
'headers' => $headers,
'method' => 'POST',
'timeout' => $config->timeout,
'data' => $notice->toXml($config)
);
$host = variable_get('airbreak_host', 'airbrakeapp.com');
$url = "http://{$host}/notifier_api/v2/notices";
$response = drupal_http_request($url, $opts);
}
}
}
function airbrake_libraries_info() {
$library['airbrake'] = array(
'name' => 'PHP Airbrake',
'vendor url' => 'https://github.com/nodrew/php-airbrake',
'download url' => 'https://github.com/nodrew/php-airbrake/downloads',
'path' => 'src/Airbrake',
'version arguments' => array(
'file' => 'src/Airbrake/Version.php',
'pattern' => "/NUMBER\s+=\s+'(\d+.\d+)'/",
),
);
$library['airbrake']['files']['php'] = array('EventHandler.php', 'Client.php', 'Configuration.php');
return $library;
}
function airbrake_admin_settings() {
$form['airbrake_key'] = array(
'#type' => 'textfield',
'#title' => t('Airbrake API Key'),
'#default_value' => variable_get('airbrake_key', ''),
);
$form['airbrake_environment'] = array(
'#type' => 'textfield',
'#title' => t('Environment'),
'#description' => t('i.e. Production, Development, or Staging. Airbrake will group alerts from each environment together.'),
'#default_value' => variable_get('airbrake_environment', ''),
);
$form['airbreak_host'] = array(
'#type' => 'textfield',
'#title' => t('Host'),
'#description' => t('The host that should receive the API call, for example <em>airbrakeapp.com</em>.'),
'#default_value' => variable_get('airbreak_host', 'airbrakeapp.com'),
);
$opts = array(
WATCHDOG_EMERGENCY => 'Emergency: system is unusable',
WATCHDOG_ALERT => 'Alert: action must be taken immediately',
WATCHDOG_CRITICAL => 'Critical: critical conditions',
WATCHDOG_ERROR => 'Error: error conditions',
WATCHDOG_WARNING => 'Warning: warning conditions',
WATCHDOG_NOTICE => 'Notice: normal but significant condition',
WATCHDOG_INFO => 'Informational: informational messages',
WATCHDOG_DEBUG => 'Debug: debug-level messages'
);
$form['airbrake_conditions'] = array(
'#type' => 'checkboxes',
'#title' => t('Severity'),
'#description' => t('Which watchdog severities should be sent to Airbrake?'),
'#options' => $opts,
'#default_value' => variable_get('airbrake_conditions', array(
WATCHDOG_EMERGENCY,
WATCHDOG_ALERT,
WATCHDOG_CRITICAL,
WATCHDOG_ERROR)),
);
return system_settings_form($form);
}
/**
* Implements hook_requirements(). Checks to see if the required library
* is installed.
*/
function airbrake_requirements($phase) {
$requirement = array();
if ($phase == 'runtime') {
if ((!$library = libraries_detect('airbrake')) || empty($library['installed'])) {
$requirement[] = array(
'title' => 'Airbrake library',
'description' => t('The Airbrake library is not installed. Please see the readme file that came with this module for instructions.'),
'value' => t('Missing!'),
'severity' => REQUIREMENT_ERROR
);
}
$apikey = variable_get('airbrake_key', '');
if (empty($apikey)) {
$requirement[] = array(
'title' => 'Airbrake API Key',
'description' => t('@configure the Aibrake module to set an API key.', array('@configure' => l('Configure', 'admin/config/development/airbrake'))),
'severity' => REQUIREMENT_ERROR
);
}
$env = variable_get('airbrake_environment', '');
if (empty($env)) {
$requirement[] = array(
'title' => 'Airbrake Environment name',
'description' => t('@configure an environment name in Aibrake for best results.', array('@configure' => l('Configure', 'admin/config/development/airbrake'))),
'severity' => REQUIREMENT_WARNING
);
}
}
return $requirement;
}
// Implements hook_menu
function airbrake_menu() {
$items['admin/config/development/airbrake'] = array(
'title' => 'Airbrake',
'description' => 'Configure Airbrake log reporting',
'page callback' => 'drupal_get_form',
'page arguments' => array('airbrake_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/development/airbrake/settings'] = array(
'title' => 'Settings',
'access arguments' => array('administer site configuration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/development/airbrake/test'] = array(
'title' => 'Test',
'description' => 'Test your Airbrake integration.',
'page callback' => 'airbrake_test_integration',
'page arguments' => array(),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Make a couple of calls to watchdog to see if the intregration works.
*/
function airbrake_test_integration() {
$conditions = variable_get('airbrake_conditions', array(WATCHDOG_EMERGENCY,
WATCHDOG_ALERT,
WATCHDOG_CRITICAL,
WATCHDOG_ERROR));
foreach ($conditions as $condition) {
watchdog('airbrake', 'This is a test from the Airbrake module.', NULL, $condition);
}
return t('You should now see a couple of messages from your site on your Airbrake account.');
}
?>