Add missing raven client

This commit is contained in:
nikolark committed 2016-08-28 22:20:13 +02:00
1 parent f24699de7c
commit bf22ab1994
15 files changed
+5582 -21

No files matched your search

@@ -1,14 +0,0 @@
{
"name": "ryanbagwell/wordpress-sentry",
"description": "A plugin that sends error messages to the Sentry error logging system.",
"license": "GPLv2 or later",
"authors": [
{
"name": "Ryan Bagwell",
"email": "ryan@ryanbagwell.com"
}
],
"require": {
"raven/raven": "0.10.0"
}
}
@@ -0,0 +1,6 @@
*.lock
package.xml
/vendor
.idea
.php_cs.cache
docs/_build
@@ -0,0 +1,3 @@
[submodule "docs/_sentryext"]
path = docs/_sentryext
url = https://github.com/getsentry/sentry-doc-support
@@ -0,0 +1,12 @@
<?php
$finder = Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__)
;
return Symfony\CS\Config\Config::create()
->setUsingCache(true)
->setUsingLinter(true)
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->finder($finder)
;
@@ -0,0 +1,30 @@
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
matrix:
allow_failures:
- php: hhvm
- php: 7.0
fast_finish: true
sudo: false
cache:
directories:
- $HOME/.composer/cache
before_install:
- composer self-update
install: travis_retry composer install --no-interaction --prefer-source
script:
- vendor/bin/php-cs-fixer fix --config-file=.php_cs --verbose --diff --dry-run
- vendor/bin/phpunit
@@ -1,4 +1,4 @@
#!/usr/bin/php
#!/usr/bin/env php
<?php
// Maximize error reporting
@@ -35,10 +35,12 @@ function cmd_test($dsn)
$client = new Raven_Client($dsn, array(
'trace' => true,
'curl_method' => 'sync',
'app_path' => realpath(__DIR__ . '/..'),
'base_path' => realpath(__DIR__ . '/..'),
));
$config = get_object_vars($client);
$required_keys = array('servers', 'project', 'public_key', 'secret_key');
$required_keys = array('server', 'project', 'public_key', 'secret_key');
echo "Client configuration:\n";
foreach ($required_keys as $key) {
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Raven Breadcrumbs
*
* @package raven
*/
class Raven_Breadcrumbs
{
public function __construct($size = 100)
{
$this->count = 0;
$this->pos = 0;
$this->size = $size;
$this->buffer = array();
}
public function record($crumb)
{
if (empty($crumb['timestamp'])) {
$crumb['timestamp'] = microtime(true);
}
$this->buffer[$this->pos] = $crumb;
$this->pos = ($this->pos + 1) % $this->size;
$this->count++;
}
public function fetch()
{
$results = array();
for ($i = 0; $i <= ($this->size - 1); $i++) {
$node = @$this->buffer[($this->pos + $i) % $this->size];
if (isset($node)) {
$results[] = $node;
}
}
return $results;
}
public function is_empty()
{
return $this->count === 0;
}
public function to_json()
{
return array(
'values' => $this->fetch(),
);
}
}
@@ -0,0 +1,94 @@
<?php
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
class Raven_Breadcrumbs_MonologHandler extends AbstractProcessingHandler
{
/**
* Translates Monolog log levels to Raven log levels.
*/
private $logLevels = array(
Logger::DEBUG => Raven_Client::DEBUG,
Logger::INFO => Raven_Client::INFO,
Logger::NOTICE => Raven_Client::INFO,
Logger::WARNING => Raven_Client::WARNING,
Logger::ERROR => Raven_Client::ERROR,
Logger::CRITICAL => Raven_Client::FATAL,
Logger::ALERT => Raven_Client::FATAL,
Logger::EMERGENCY => Raven_Client::FATAL,
);
private $excMatch = '/^exception \'([^\']+)\' with message \'(.+)\' in .+$/s';
/**
* @var Raven_Client the client object that sends the message to the server
*/
protected $ravenClient;
/**
* @param Raven_Client $ravenClient
* @param int $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true)
{
parent::__construct($level, $bubble);
$this->ravenClient = $ravenClient;
}
protected function parseException($message)
{
if (!preg_match($this->excMatch, $message, $matches)) {
return;
}
return array($matches[1], $matches[2]);
}
/**
* {@inheritdoc}
*/
protected function write(array $record)
{
// sentry uses the 'nobreadcrumb' attribute to skip reporting
if (!empty($record['context']['nobreadcrumb'])) {
return;
}
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
$exc = $record['context']['exception'];
$crumb = array(
'type' => 'error',
'level' => $this->logLevels[$record['level']],
'category' => $record['channel'],
'data' => array(
'type' => get_class($exc),
'value' => $exc->getMessage(),
),
);
} else {
// TODO(dcramer): parse exceptions out of messages and format as above
if ($error = $this->parseException($record['message'])) {
$crumb = array(
'type' => 'error',
'level' => $this->logLevels[$record['level']],
'category' => $record['channel'],
'data' => array(
'type' => $error[0],
'value' => $error[1],
),
);
} else {
$crumb = array(
'level' => $this->logLevels[$record['level']],
'category' => $record['channel'],
'message' => $record['message'],
);
}
}
$this->ravenClient->breadcrumbs->record($crumb);
}
}
@@ -0,0 +1,47 @@
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Serializes a value into a representation that should reasonable suggest
* both the type and value, and be serializable into JSON.
* @package raven
*/
class Raven_ReprSerializer extends Raven_Serializer
{
protected function serializeValue($value)
{
if ($value === null) {
return 'null';
} elseif ($value === false) {
return 'false';
} elseif ($value === true) {
return 'true';
} elseif (is_float($value) && (int) $value == $value) {
return $value.'.0';
} elseif (is_integer($value) || is_float($value)) {
return (string) $value;
} elseif (is_object($value) || gettype($value) == 'object') {
return 'Object '.get_class($value);
} elseif (is_resource($value)) {
return 'Resource '.get_resource_type($value);
} elseif (is_array($value)) {
return 'Array of length ' . count($value);
} else {
$value = (string) $value;
if (function_exists('mb_convert_encoding')) {
$value = mb_convert_encoding($value, 'UTF-8', 'auto');
}
return $value;
}
}
}
File diff suppressed because it is too large. Load diff
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
@@ -22,4 +21,4 @@
<directory suffix=".php">./lib/Raven/</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
@@ -0,0 +1,68 @@
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Raven_Tests_MonologBreadcrumbHandlerTest extends PHPUnit_Framework_TestCase
{
protected function getSampleErrorMessage()
{
return <<<EOF
exception 'Exception' with message 'An unhandled exception' in /sentry-laravel/examples/laravel-4.2/app/routes.php:17
Stack trace:
#0 [internal function]: {closure}()
#1 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(5398): call_user_func_array(Object(Closure), Array)
#2 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(5065): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#3 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(5053): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#4 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(715): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#5 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(696): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#6 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(7825): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#7 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(8432): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#8 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(8379): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#9 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(11123): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#10 /sentry-laravel/examples/laravel-4.2/bootstrap/compiled.php(657): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#11 /sentry-laravel/examples/laravel-4.2/public/index.php(49): Illuminate\Foundation\Application->run()
#12 /sentry-laravel/examples/laravel-4.2/server.php(19): require_once('/Users/dcramer/...')
#13 {main}
EOF;
}
public function testSimple()
{
$client = new \Raven_Client();
$handler = new \Raven_Breadcrumbs_MonologHandler($client);
$logger = new Monolog\Logger('sentry');
$logger->pushHandler($handler);
$logger->addWarning('Foo');
$crumbs = $client->breadcrumbs->fetch();
$this->assertEquals(count($crumbs), 1);
$this->assertEquals($crumbs[0]['message'], 'Foo');
$this->assertEquals($crumbs[0]['category'], 'sentry');
$this->assertEquals($crumbs[0]['level'], 'warning');
}
public function testErrorInMessage()
{
$client = new \Raven_Client();
$handler = new \Raven_Breadcrumbs_MonologHandler($client);
$logger = new Monolog\Logger('sentry');
$logger->pushHandler($handler);
$logger->addError($this->getSampleErrorMessage());
$crumbs = $client->breadcrumbs->fetch();
$this->assertEquals(count($crumbs), 1);
$this->assertEquals($crumbs[0]['data']['type'], 'Exception');
$this->assertEquals($crumbs[0]['data']['value'], 'An unhandled exception');
$this->assertEquals($crumbs[0]['category'], 'sentry');
$this->assertEquals($crumbs[0]['level'], 'error');
}
}
@@ -0,0 +1,38 @@
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Raven_Tests_BreadcrumbsTest extends PHPUnit_Framework_TestCase
{
public function testBuffer()
{
$breadcrumbs = new Raven_Breadcrumbs(10);
for ($i = 0; $i <= 10; $i++) {
$breadcrumbs->record(array('message' => $i));
}
$results = $breadcrumbs->fetch();
$this->assertEquals(count($results), 10);
for ($i = 1; $i <= 10; $i++) {
$this->assertEquals($results[$i - 1]['message'], $i);
}
}
public function testJson()
{
$breadcrumbs = new Raven_Breadcrumbs(1);
$breadcrumbs->record(array('message' => 'test'));
$json = $breadcrumbs->to_json();
$this->assertEquals(count($json['values']), 1);
$this->assertEquals($json['values'][0]['message'], 'test');
}
}
@@ -0,0 +1,79 @@
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Raven_Tests_ReprSerializerTest extends PHPUnit_Framework_TestCase
{
public function testArraysAreArrays()
{
$serializer = new Raven_ReprSerializer();
$input = array(1, 2, 3);
$result = $serializer->serialize($input);
$this->assertEquals(array('1', '2', '3'), $result);
}
public function testObjectsAreStrings()
{
$serializer = new Raven_ReprSerializer();
$input = new Raven_StacktraceTestObject();
$result = $serializer->serialize($input);
$this->assertEquals('Object Raven_StacktraceTestObject', $result);
}
public function testIntsAreInts()
{
$serializer = new Raven_ReprSerializer();
$input = 1;
$result = $serializer->serialize($input);
$this->assertTrue(is_string($result));
$this->assertEquals(1, $result);
}
public function testFloats()
{
$serializer = new Raven_ReprSerializer();
$input = 1.5;
$result = $serializer->serialize($input);
$this->assertTrue(is_string($result));
$this->assertEquals('1.5', $result);
}
public function testBooleans()
{
$serializer = new Raven_ReprSerializer();
$input = true;
$result = $serializer->serialize($input);
$this->assertTrue(is_string($result));
$this->assertEquals('true', $result);
$input = false;
$result = $serializer->serialize($input);
$this->assertTrue(is_string($result));
$this->assertEquals('false', $result);
}
public function testNull()
{
$serializer = new Raven_ReprSerializer();
$input = null;
$result = $serializer->serialize($input);
$this->assertTrue(is_string($result));
$this->assertEquals('null', $result);
}
public function testRecursionMaxDepth()
{
$serializer = new Raven_ReprSerializer();
$input = array();
$input[] = &$input;
$result = $serializer->serialize($input, 3);
$this->assertEquals(array(array(array('Array of length 1'))), $result);
}
}
@@ -30,11 +30,11 @@ To report bugs and get help, see the [official github repository](https://github
= 1.2 =
Adding the ability to access the Raven client directly to send custom error messages to sentry. Merge PR from @wk8.
* Adding the ability to access the Raven client directly to send custom error messages to sentry. Merge PR from @wk8.
= 1.1 =
Upgrade raven php client to v0.15.0
* Upgrade raven php client to v0.15.0
= 1.0 =
@@ -63,6 +63,8 @@ Upgrade raven php client to v0.15.0
== Contributors ==
vbartusevicius (https://github.com/vbartusevicius)
wk8 (https://github.com/wk8)