Last active
February 20, 2017 15:27
-
-
Save jippi/24f8c92e5e0385b60483cecef87bf90d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
newrelic_add_custom_parameter('APP_VERSION', Environment::getAppVersion()); | |
newrelic_add_custom_parameter('APP_ENV', Environment::get()); | |
if (php_sapi_name() !== 'cli') { | |
newrelic_add_custom_parameter('_get', json_encode($_GET)); | |
newrelic_add_custom_parameter('_post', json_encode($_POST)); | |
newrelic_add_custom_parameter('_files', json_encode($_FILES)); | |
newrelic_add_custom_parameter('_cookies', json_encode($_COOKIE)); | |
$serverVars = [ | |
'GEOIP_COUNTRY_CODE', 'GEOIP_CITY_NAME', 'HTTP_HOST', | |
'HTTP_X_ORIGIN_IP', 'PATH_INFO', 'REQUEST_METHOD', 'HTTP_USER_AGENT', | |
'HTTP_X_HTTPS', 'SERVER_ADDR', 'REDIRECT_STATUS', 'HTTP_ACCEPT', | |
'HTTP_ACCEPT_ENCODING', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_CACHE_CONTROL', | |
'REQUEST_URI', 'REMOTE_ADDR', 'IS_PUBLIC', 'HTTP_X_FORWARDED_FOR', | |
'REQUEST_TIME_FLOAT', 'REQUEST_TIME', | |
'APP_VERSION', 'APP_ENV', 'HTTP_REFERER', 'HTTP_X_FORWARDED_HOST', 'HTTP_X_AUTHORIZATION' | |
]; | |
foreach ($_SERVER as $k => $v) { | |
if (!in_array($k, $serverVars)) { | |
continue; | |
} | |
newrelic_add_custom_parameter('server_' . strtolower($k), $v); | |
} | |
foreach (['location', 'overlay', 'pubuid', 'user'] as $k) { | |
if (!isset($_COOKIE[$k])) { | |
continue; | |
} | |
newrelic_add_custom_parameter('cookie_' . strtolower($k), $_COOKIE[$k]); | |
} | |
} | |
$ignoreExceptions = [ | |
'NotFoundException', | |
'MissingControllerException', | |
'NotImplementedException' | |
]; | |
$BowntyConsoleErrorHandler = null; | |
if (php_sapi_name() === 'cli') { | |
App::uses('BowntyConsoleErrorHandler', 'Console'); | |
$BowntyConsoleErrorHandler = new BowntyConsoleErrorHandler(); | |
Configure::write('KeepObjectAlive.BowntyConsoleErrorHandler', $BowntyConsoleErrorHandler); | |
} | |
// Exception handler | |
Configure::write('Exception.handler', function ($e) use ($ignoreExceptions, $BowntyConsoleErrorHandler) { | |
if (!in_array(get_class($e), $ignoreExceptions)) { | |
newrelic_notice_error(null, $e); | |
} | |
if (php_sapi_name() === 'cli') { | |
return $BowntyConsoleErrorHandler->handleException($e); | |
} | |
CakeLog::write('emergency', $e->getMessage(), ['exception' => $e]); | |
App::uses('BowntyErrorHandler', 'Error'); | |
BowntyErrorHandler::handleException($e); | |
}); | |
// Error handler for non-exception errors | |
$errorHandler = function ($code, $description, $file = null, $line = null, $context = null) use ($BowntyConsoleErrorHandler) { | |
// Whatever stupid APCu extension | |
if (false !== strpos($description, 'apc_store(): GC cache entry')) { | |
return; | |
} | |
// Whatever stupid output buffering logic | |
if (false !== strpos($description, 'ob_flush(): failed to flush buffer. No buffer to flush')) { | |
return; | |
} | |
newrelic_notice_error($code, $description, $file, $line, $context); | |
if (php_sapi_name() === 'cli') { | |
return $BowntyConsoleErrorHandler->handleError($code, $description, $file, $line, $context); | |
} | |
App::uses('BowntyErrorHandler', 'Error'); | |
BowntyErrorHandler::handleError($code, $description, $file, $line, $context); | |
}; | |
Configure::write('Error.handler', $errorHandler); | |
Configure::write('Error.consoleHandler', $errorHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment