Skip to content

Commit 672506c

Browse files
authored
Merge pull request #5 from websupport-sk/support-breadcrumbs
support for breadcrumbs
2 parents d7b842e + 488993c commit 672506c

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/Client.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CEvent;
88
use CJavaScript;
99
use CMap;
10+
use Sentry\Breadcrumb;
1011
use Sentry\Severity;
1112
use Sentry\State\HubAdapter;
1213
use Sentry\State\HubInterface;
@@ -111,6 +112,19 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
111112
return $this->getSentry()->getClient()->captureMessage($message, $level, $scope);
112113
}
113114

115+
public function addBreadcrumb(
116+
string $level,
117+
string $type,
118+
string $category,
119+
?string $message = null,
120+
array $metadata = [],
121+
?float $timestamp = null
122+
): bool {
123+
return $this->getSentry()->addBreadcrumb(
124+
new Breadcrumb($level, $type, $category, $message, $metadata, $timestamp)
125+
);
126+
}
127+
114128
/**
115129
* Logs an exception.
116130
*
@@ -121,7 +135,9 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
121135
*/
122136
public function captureException(\Throwable $exception, ?Scope $scope = null): ?string
123137
{
124-
$this->rootTransaction->setHttpStatus(500);
138+
if ($this->rootTransaction !== null) {
139+
$this->rootTransaction->setHttpStatus(500);
140+
}
125141
return $this->getSentry()->getClient()->captureException($exception, $scope);
126142
}
127143

@@ -291,6 +307,8 @@ public function handleBeginRequestEvent(\CEvent $event): void
291307
*/
292308
public function handleEndRequestEvent(\CEvent $event): void
293309
{
310+
$this->grabPushLogsFromLoggerToBreadcrumbs();
311+
294312
if ($this->appSpan !== null) {
295313
$this->appSpan->finish();
296314
}
@@ -305,13 +323,17 @@ public function handleEndRequestEvent(\CEvent $event): void
305323

306324
public function handleExceptionEvent(\CEvent $event): void
307325
{
308-
$this->rootTransaction->setHttpStatus(500);
326+
$this->grabPushLogsFromLoggerToBreadcrumbs();
327+
328+
if ($this->rootTransaction !== null) {
329+
$this->rootTransaction->setHttpStatus(500);
330+
}
309331
}
310332

311333
/**
312334
* @param string[]|int[]|bool[] $data
313335
*/
314-
public function startRootTransaction(string $description, array $data = []): Transaction
336+
private function startRootTransaction(string $description, array $data = []): Transaction
315337
{
316338
$context = new TransactionContext();
317339
$context->setOp('yii-app');
@@ -374,4 +396,28 @@ private function operationNameFromBeginRequestEvent(CEvent $event): string
374396
}
375397

376398
//endregion
399+
private function grabPushLogsFromLoggerToBreadcrumbs(): void
400+
{
401+
$logs = Yii::getLogger()->getLogs();
402+
403+
foreach ($logs as $log) {
404+
/**
405+
* @var string $message
406+
* @var string $level
407+
* @var string $category
408+
*/
409+
list($message, $level, $category) = $log;
410+
411+
// remove stack trace from message
412+
if (($pos = strpos($message, 'Stack trace:')) !== false) {
413+
$message = substr($message, 0, $pos);
414+
}
415+
416+
if ($level === 'trace') {
417+
$level = Breadcrumb::LEVEL_DEBUG;
418+
}
419+
420+
$this->addBreadcrumb($level, Breadcrumb::TYPE_DEFAULT, $category, $message);
421+
}
422+
}
377423
}

0 commit comments

Comments
 (0)