Skip to content

Instantly share code, notes, and snippets.

@dominics
Created April 10, 2017 00:12
Show Gist options
  • Save dominics/61c23f2ded720d039554d889d304afc9 to your computer and use it in GitHub Desktop.
Save dominics/61c23f2ded720d039554d889d304afc9 to your computer and use it in GitHub Desktop.
Report PHP OOM errors in Bugsnag
<?php
namespace Foobar\Bugsnag;
use Bugsnag\Client;
class OomSnag
{
private const REGEX = '/^Allowed memory size of ([0-9]+) bytes exhausted/';
/**
* @var Client
*/
private $bugsnag;
public function __construct(Client $bugsnag)
{
$this->bugsnag = $bugsnag;
}
public function register()
{
// Make sure we can call ini_set for free
ini_set('bugsnag.dummy_call', 0);
register_shutdown_function(function () {
$error = error_get_last();
if ($error && $error['type'] === 1 && preg_match(self::REGEX, $error['message'], $matches)) {
// Allow Bugsnag some extra memory
ini_set('memory_limit', (int)$matches[1] + 10000000);
$this->bugsnag->notifyError('memory_limit', $error['message'], function ($report) use ($error) {
$report->setPHPError($error['type'], $error['message'], $error['file'], $error['line'], true);
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment