-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collecting breadcrumbs and stacktraces #54
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,19 @@ | |
|
||
namespace notamedia\sentry; | ||
|
||
use Sentry\Breadcrumb; | ||
use Sentry\ClientBuilder; | ||
use Sentry\Event; | ||
use Sentry\EventHint; | ||
use Sentry\Frame; | ||
use Sentry\Integration\ErrorListenerIntegration; | ||
use Sentry\Integration\ExceptionListenerIntegration; | ||
use Sentry\Integration\FatalErrorListenerIntegration; | ||
use Sentry\Integration\IntegrationInterface; | ||
use Sentry\SentrySdk; | ||
use Sentry\Serializer\RepresentationSerializer; | ||
use Sentry\Severity; | ||
use Sentry\StacktraceBuilder; | ||
use Sentry\State\Scope; | ||
use Throwable; | ||
use Yii; | ||
|
@@ -48,6 +52,13 @@ class SentryTarget extends Target | |
*/ | ||
public $extraCallback; | ||
|
||
/** | ||
* @var StacktraceBuilder Builder fo creation stack trace frames | ||
* | ||
* It available in \Sentry\Client, but it`s private. | ||
*/ | ||
protected $stacktraceBuilder; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
|
@@ -77,6 +88,8 @@ public function __construct($config = []) | |
}); | ||
|
||
SentrySdk::init()->bindClient($builder->getClient()); | ||
|
||
$this->stacktraceBuilder = new StacktraceBuilder($options, new RepresentationSerializer($options)); | ||
} | ||
|
||
/** | ||
|
@@ -87,13 +100,41 @@ protected function getContextMessage() | |
return ''; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function collect($messages, $final) | ||
{ | ||
foreach ($messages as $message) { | ||
list($text, $level, $category, $timestamp, $traces) = $message; | ||
|
||
[$message, $tags, $exception, $type, $extra] = $this->parseText($text); | ||
|
||
$metadata = []; | ||
if(!empty($tags)) | ||
$metadata['tags'] = $tags; | ||
|
||
if(!empty($exception)) | ||
$metadata['exception'] = $exception; | ||
|
||
if(!empty($extra)) | ||
$metadata['extra'] = $extra; | ||
|
||
// $timestamp parameter will be added soon | ||
// @see https://github.com/getsentry/sentry-php/pull/1193 | ||
$bc = new Breadcrumb($this->getLogLevel($level), $type, $category, $message, $metadata, $timestamp); | ||
\Sentry\addBreadcrumb($bc); | ||
} | ||
parent::collect($messages, $final); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function export() | ||
{ | ||
foreach ($this->messages as $message) { | ||
[$text, $level, $category] = $message; | ||
[$text, $level, $category, $timestamp, $traces] = $message; | ||
|
||
$data = [ | ||
'message' => '', | ||
|
@@ -115,31 +156,10 @@ public function export() | |
} | ||
} catch (Throwable $e) {} | ||
|
||
\Sentry\withScope(function (Scope $scope) use ($text, $level, $data) { | ||
if (is_array($text)) { | ||
if (isset($text['msg'])) { | ||
$data['message'] = (string)$text['msg']; | ||
unset($text['msg']); | ||
} | ||
if (isset($text['message'])) { | ||
$data['message'] = (string)$text['message']; | ||
unset($text['message']); | ||
} | ||
|
||
if (isset($text['tags'])) { | ||
$data['tags'] = ArrayHelper::merge($data['tags'], $text['tags']); | ||
unset($text['tags']); | ||
} | ||
|
||
if (isset($text['exception']) && $text['exception'] instanceof Throwable) { | ||
$data['exception'] = $text['exception']; | ||
unset($text['exception']); | ||
} | ||
\Sentry\withScope(function (Scope $scope) use ($text, $level, $timestamp, $traces, $data) { | ||
[$data['message'], $tags, $data['exception'], $breadcrumbType, $data['extra']] = $this->parseText($text); | ||
|
||
$data['extra'] = $text; | ||
} else { | ||
$data['message'] = (string) $text; | ||
} | ||
$data['tags'] = ArrayHelper::merge($data['tags'], $tags); | ||
|
||
if ($this->context) { | ||
$data['extra']['context'] = parent::getContextMessage(); | ||
|
@@ -148,9 +168,7 @@ public function export() | |
$data = $this->runExtraCallback($text, $data); | ||
|
||
$scope->setUser($data['userData']); | ||
foreach ($data['extra'] as $key => $value) { | ||
$scope->setExtra((string) $key, $value); | ||
} | ||
|
||
foreach ($data['tags'] as $key => $value) { | ||
if ($value) { | ||
$scope->setTag($key, $value); | ||
|
@@ -163,15 +181,75 @@ public function export() | |
$event = Event::createEvent(); | ||
$event->setMessage($data['message']); | ||
$event->setLevel($this->getLogLevel($level)); | ||
$event->setTimestamp($timestamp); | ||
|
||
\Sentry\captureEvent($event, EventHint::fromArray(array_filter([ | ||
'exception' => $data['exception'] ?? null, | ||
'extra' => $data['extra'] ?? [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check this line, it seems not the same extra data that we can attach to scope There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All scope There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
'stacktrace' => $this->buildStacktrace($traces), | ||
]))); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Parse the text variable | ||
* @param $text | ||
* @return array | ||
*/ | ||
protected function parseText($text) | ||
{ | ||
$message = ''; | ||
$tags = []; | ||
$exception = null; | ||
$extra = []; | ||
$type = Breadcrumb::TYPE_DEFAULT; | ||
if (is_array($text)) { | ||
if (isset($text['msg'])) { | ||
$message = (string)$text['msg']; | ||
unset($text['msg']); | ||
} | ||
|
||
if (isset($text['message'])) { | ||
$message = (string)$text['message']; | ||
unset($text['message']); | ||
} | ||
|
||
if (isset($text['tags'])) { | ||
$tags = (array)$text['tags']; | ||
unset($text['tags']); | ||
} | ||
|
||
if (isset($text['type'])) { | ||
$type = $text['type']; | ||
unset($text['type']); | ||
} | ||
|
||
if (isset($text['exception']) && $text['exception'] instanceof Throwable) { | ||
$exception = $text['exception']; | ||
unset($text['exception']); | ||
} | ||
$extra = $text; | ||
} else { | ||
$message = (string) $text; | ||
} | ||
return [$message, $tags, $exception, $type, $extra]; | ||
} | ||
|
||
/** | ||
* Builds \Sentry\Stacktrace from traces array | ||
* @param $traces Yii message traces | ||
* @return null|\Sentry\Stacktrace | ||
*/ | ||
protected function buildStacktrace($traces) | ||
{ | ||
if(is_array($traces) && ! empty($traces)) { | ||
return $this->stacktraceBuilder->buildFromBacktrace($traces, Frame::INTERNAL_FRAME_FILENAME , 0); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Calls the extra callback if it exists | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work!
Please format code according to PSR-12 (spaces and braces)