Skip to content

Commit

Permalink
Update Client to use sentry-sdk-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
o0h committed Nov 4, 2019
1 parent e7eff1c commit ec746d6
Showing 1 changed file with 37 additions and 80 deletions.
117 changes: 37 additions & 80 deletions src/Http/Client.php
@@ -1,18 +1,19 @@
<?php

namespace Connehito\CakeSentry\Http;

use Sentry\Breadcrumb;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use function Sentry\init;
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Error\PHP7ErrorException;
use Cake\Event\Event;
use Cake\Event\EventDispatcherTrait;
use Cake\Http\ServerRequestFactory;
use Cake\Routing\Router;
use Cake\Utility\Hash;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Raven_Client;
use ReflectionMethod;

class Client
{
Expand All @@ -22,8 +23,8 @@ class Client
/* @var array default instance config */
protected $_defaultConfig = [];

/* @var \Raven_Client */
protected $raven;
/* @var Hub */
protected $hub;

/* @var ServerRequestInterface */
protected $request;
Expand All @@ -32,41 +33,20 @@ class Client
* Client constructor.
*
* @param array $config config for uses Sentry
* @param ServerRequestInterface $request request context message
*/
public function __construct(array $config, ServerRequestInterface $request = null)
public function __construct(array $config)
{
$this->setConfig($config);
if (self::isHttpRequest()) {
$this->setRequest($request);
}
$this->setupClient();
}

/**
* Set context RequestMessage.
*
* @param null|ServerRequestInterface $request if null, factory from global
* @return void
* Accessor for current hub
* @return Hub
*/
protected function setRequest($request)
public function getHub(): Hub
{
if ($request && !($request instanceof ServerRequestInterface)) {
throw new InvalidArgumentException('Request must be ServerRequestInterface');
} elseif (!$request) {
$request = Router::getRequest(true) ?: ServerRequestFactory::fromGlobals();
}
$this->request = $request;
}

/**
* Set context RequestMessage.
*
* @return null|ServerRequestInterface contextual request
*/
public function getRequest()
{
return $this->request;
return $this->hub;
}

/**
Expand All @@ -78,48 +58,37 @@ public function getRequest()
*
* @return void
*/
public function capture($level, string $message, array $context)
public function capture($level, string $message, array $context): void
{
$event = new Event('CakeSentry.Client.beforeCapture', $this, $context);
$data = (array)$this->getEventManager()->dispatch($event)->getResult();
$this->getEventManager()->dispatch($event);

$exception = Hash::get($context, 'exception');
if ($exception) {
if ($exception instanceof PHP7ErrorException) {
$exception = $exception->getError();
}
$this->raven->captureException($exception, $data);
$lastEventId = $this->hub->captureException($exception);
} else {
$stack = array_slice(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT), 3);
$data += $context;
if (!isset($data['level'])) {
$data['level'] = $level;
}
foreach (['file', 'line'] as $stackField) {
if (!isset($data[$stackField])) {
$data[$stackField] = $stack[0][$stackField];
}
$stacks = array_slice(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT), 3);
foreach ($stacks as $stack) {
$method = isset($stack['class']) ? "{$stack['class']}::{$stack['function']}" : $stack['function'];
unset($stack['class']);
unset($stack['function']);
$this->hub->addBreadcrumb(new Breadcrumb(
$level,
Breadcrumb::TYPE_ERROR,
'method',
$method,
$stack
));
}
$data = array_filter($data, function ($val) {
return !is_null($val);
});
$this->raven->captureMessage($message, [], $data, $stack);
$lastEventId = $this->hub->captureMessage($message);
}

if ($this->raven->getLastError() || $this->raven->getLastSentryError()) {
$event = new Event('CakeSentry.Client.captureError', $this, $data);
$this->getEventManager()->dispatch($event);
}
}

/**
* Accessor for Raven_Client instance.
*
* @return Raven_Client
*/
public function getRaven()
{
return $this->raven;
$context['lastEventId'] = $lastEventId;
$event = new Event('CakeSentry.Client.afterCapture', $this, $context);
$this->getEventManager()->dispatch($event);
}

/**
Expand All @@ -130,33 +99,21 @@ public function getRaven()
protected function setupClient()
{
$config = (array)Configure::read('Sentry');
$dsn = Hash::get($config, 'dsn');
if (!$dsn) {
if (!Hash::check($config, 'dsn')) {
throw new InvalidArgumentException('Sentry DSN not provided.');
}
$options = (array)Hash::get($config, 'options');
if (!Hash::get($options, 'send_callback')) {
$options['send_callback'] = function () {
$event = new Event('CakeSentry.Client.afterCapture', $this, func_get_args());
$this->getEventManager()->dispatch($event)->getResult();
$this->getEventManager()->dispatch($event);
};
}
$raven = new Raven_Client($dsn, $options);

$this->raven = $raven;
}

/**
* Detect Http request or not.
* (Delegate Raven_Client private logic.)
*
* @return bool
*/
protected static function isHttpRequest()
{
$isHttpRequest = new ReflectionMethod(Raven_Client::class, 'is_http_request');
$isHttpRequest->setAccessible(true);
init($config);
$this->hub = SentrySdk::getCurrentHub();

return $isHttpRequest->invoke(null);
$event = new Event('CakeSentry.Client.afterSetup', $this);
$this->getEventManager()->dispatch($event);
}
}

0 comments on commit ec746d6

Please sign in to comment.