Skip to content

Commit 0555966

Browse files
committed
linked errors with transactions
1 parent b814075 commit 0555966

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

src/Agent.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,15 @@ public function getTransaction(string $name)
194194
*/
195195
public function captureThrowable(\Throwable $thrown, array $context = [], ?Transaction $transaction = null)
196196
{
197+
$err = $this->eventFactory->createError($thrown, array_replace_recursive($this->sharedContext, $context), $transaction);
198+
199+
if ( ! empty($transaction) ) {
200+
$transaction->addError($err);
201+
return;
202+
}
203+
197204
$this->errorsStore->register(
198-
$this->eventFactory->createError($thrown, array_replace_recursive($this->sharedContext, $context), $transaction)
205+
$err
199206
);
200207
}
201208

src/Events/Error.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(\Throwable $throwable, array $contexts, ?Transaction
3737
*/
3838
public function jsonSerialize() : array
3939
{
40-
return [
40+
$result = [
4141
'id' => $this->getId(),
4242
'timestamp' => $this->getTimestamp(),
4343
'context' => $this->getContext(),
@@ -53,6 +53,14 @@ public function jsonSerialize() : array
5353
'name' => 'error',
5454
]
5555
];
56+
57+
if ( ! empty($this->transaction) ) {
58+
$result['transaction_id'] = $this->transaction->getId();
59+
$result['parent_id'] = $this->transaction->getId();
60+
$result['trace_id'] = $this->transaction->getId();
61+
}
62+
63+
return $result;
5664
}
5765

5866
/**
@@ -83,11 +91,6 @@ private function mapStacktrace() : array
8391
if (isset($trace['class']) === true) {
8492
$item['module'] = $trace['class'];
8593
}
86-
<<<<<<< HEAD
87-
88-
=======
89-
90-
>>>>>>> 8fce808847143baef99f86062797fc11da580da0
9194
if (isset($trace['type']) === true) {
9295
$item['type'] = $trace['type'];
9396
}

src/Events/EventBean.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class EventBean
2525
*/
2626
private $timestamp;
2727

28+
/**
29+
* The parent transaction
30+
*
31+
* @var Transaction
32+
*/
33+
protected $transaction;
34+
2835
/**
2936
* Event Metadata
3037
*
@@ -72,6 +79,7 @@ public function __construct(array $contexts, ?Transaction $transaction = null)
7279
$timestamp = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
7380
$timestamp->setTimeZone(new \DateTimeZone('UTC'));
7481
$this->timestamp = $timestamp->format('Y-m-d\TH:i:s.u\Z');
82+
$this->transaction = $transaction;
7583
}
7684

7785
/**

src/Events/Transaction.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class Transaction extends EventBean implements \JsonSerializable
4545
*/
4646
private $spans = [];
4747

48+
/**
49+
* The errors for the transaction
50+
*
51+
* @var array
52+
*/
53+
private $errors = [];
54+
4855
/**
4956
* Backtrace Depth
5057
*
@@ -137,6 +144,21 @@ public function setSpans(array $spans)
137144
$this->spans = $spans;
138145
}
139146

147+
public function addError(Error $error)
148+
{
149+
$this->errors[] = $error;
150+
}
151+
152+
public function setErrors(array $errors)
153+
{
154+
$this->errors = $errors;
155+
}
156+
157+
public function getErrors()
158+
{
159+
return $this->errors ?? [];
160+
}
161+
140162
/**
141163
* Set the Max Depth/Limit of the debug_backtrace method
142164
*
@@ -181,6 +203,7 @@ public function jsonSerialize() : array
181203
'result' => $this->getMetaResult(),
182204
'context' => $this->getContext(),
183205
'spans' => $this->getSpans(),
206+
'errors' => $this->getErrors(),
184207
'processor' => [
185208
'event' => 'transaction',
186209
'name' => 'transaction',

src/Middleware/Connector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function sendTransactions(TransactionsStore $store) : bool
8181
*/
8282
public function sendErrors(ErrorsStore $store) : bool
8383
{
84+
// dd($this->buildEventPayload(new Errors($this->config, $store), 'errors', 'error'));
8485
$response = $this->client->post($this->getEndpoint(), [
8586
'headers' => $this->getRequestHeaders(),
8687
'body' => $this->buildEventPayload(new Errors($this->config, $store), 'errors', 'error')
@@ -104,12 +105,19 @@ private function buildEventPayload(Entity $entity, string $extractEvent, string
104105

105106
foreach ( $data[$extractEvent]->list() as $item ) {
106107
$obj = $item->jsonSerialize();
108+
$errors = $obj['errors'] ?? [];
107109
$spans = $obj['spans'] ?? [];
108110

109-
unset($obj['spans']);
111+
unset($obj['spans'], $obj['errors']);
110112

111113
$body .= "\n" . json_encode([$as => $item]);
112114

115+
if ( !empty($errors) ) {
116+
foreach ( $errors as $i => $error ) {
117+
$body .= "\n" . json_encode(['error' => $error]);
118+
}
119+
}
120+
113121
if ( !empty($spans) ) {
114122
foreach ( $spans as $i => $span ) {
115123
$span = array_merge($span, [

0 commit comments

Comments
 (0)