Skip to content

Commit 9254120

Browse files
authored
Add db.transaction span (#594)
1 parent ed9212d commit 9254120

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Add tracing span for Laravel HTTP client requests (#585)
2222
- Simplify Sentry meta tag retrieval, by adding `Sentry\Laravel\Integration::sentryMeta()` (#586)
2323
- Fix tracing with nested queue jobs (mostly when running jobs in the `sync` driver) (#592)
24+
- Add a `db.transaction` span, this span indicates the time that is spent inside a database transaction (#594)
2425

2526
## 2.14.2
2627

src/Sentry/Laravel/Tracing/EventHandler.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class EventHandler
3535
HttpClientEvents\RequestSending::class => 'httpClientRequestSending',
3636
HttpClientEvents\ResponseReceived::class => 'httpClientResponseReceived',
3737
HttpClientEvents\ConnectionFailed::class => 'httpClientConnectionFailed',
38+
DatabaseEvents\TransactionBeginning::class => 'transactionBeginning',
39+
DatabaseEvents\TransactionCommitted::class => 'transactionCommitted',
40+
DatabaseEvents\TransactionRolledBack::class => 'transactionRolledBack',
3841
];
3942

4043
/**
@@ -116,6 +119,9 @@ public function __construct(array $config, BacktraceHelper $backtraceHelper)
116119
*
117120
* @uses self::routeMatchedHandler()
118121
* @uses self::queryExecutedHandler()
122+
* @uses self::transactionBeginningHandler()
123+
* @uses self::transactionCommittedHandler()
124+
* @uses self::transactionRolledBackHandler()
119125
* @uses self::httpClientRequestSendingHandler()
120126
* @uses self::httpClientResponseReceivedHandler()
121127
* @uses self::httpClientConnectionFailedHandler()
@@ -240,6 +246,40 @@ private function resolveQueryOriginFromBacktrace(): ?string
240246
return "{$filePath}:{$firstAppFrame->getLine()}";
241247
}
242248

249+
protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginning $event): void
250+
{
251+
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
252+
253+
if ($parentSpan === null) {
254+
return;
255+
}
256+
257+
$context = new SpanContext;
258+
$context->setOp('db.transaction');
259+
260+
$this->pushSpan($parentSpan->startChild($context));
261+
}
262+
263+
protected function transactionCommittedHandler(DatabaseEvents\TransactionCommitted $event): void
264+
{
265+
$span = $this->popSpan();
266+
267+
if ($span !== null) {
268+
$span->finish();
269+
$span->setStatus(SpanStatus::ok());
270+
}
271+
}
272+
273+
protected function transactionRolledBackHandler(DatabaseEvents\TransactionRolledBack $event): void
274+
{
275+
$span = $this->popSpan();
276+
277+
if ($span !== null) {
278+
$span->finish();
279+
$span->setStatus(SpanStatus::internalError());
280+
}
281+
}
282+
243283
protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSending $event): void
244284
{
245285
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
@@ -252,7 +292,6 @@ protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSendi
252292

253293
$context->setOp('http.client');
254294
$context->setDescription($event->request->method() . ' ' . $event->request->url());
255-
$context->setStartTimestamp(microtime(true));
256295

257296
$this->pushSpan($parentSpan->startChild($context));
258297
}

0 commit comments

Comments
 (0)