Skip to content

Commit

Permalink
Add db.transaction span (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Oct 17, 2022
1 parent ed9212d commit 9254120
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@
- Add tracing span for Laravel HTTP client requests (#585)
- Simplify Sentry meta tag retrieval, by adding `Sentry\Laravel\Integration::sentryMeta()` (#586)
- Fix tracing with nested queue jobs (mostly when running jobs in the `sync` driver) (#592)
- Add a `db.transaction` span, this span indicates the time that is spent inside a database transaction (#594)

## 2.14.2

Expand Down
41 changes: 40 additions & 1 deletion src/Sentry/Laravel/Tracing/EventHandler.php
Expand Up @@ -35,6 +35,9 @@ class EventHandler
HttpClientEvents\RequestSending::class => 'httpClientRequestSending',
HttpClientEvents\ResponseReceived::class => 'httpClientResponseReceived',
HttpClientEvents\ConnectionFailed::class => 'httpClientConnectionFailed',
DatabaseEvents\TransactionBeginning::class => 'transactionBeginning',
DatabaseEvents\TransactionCommitted::class => 'transactionCommitted',
DatabaseEvents\TransactionRolledBack::class => 'transactionRolledBack',
];

/**
Expand Down Expand Up @@ -116,6 +119,9 @@ public function __construct(array $config, BacktraceHelper $backtraceHelper)
*
* @uses self::routeMatchedHandler()
* @uses self::queryExecutedHandler()
* @uses self::transactionBeginningHandler()
* @uses self::transactionCommittedHandler()
* @uses self::transactionRolledBackHandler()
* @uses self::httpClientRequestSendingHandler()
* @uses self::httpClientResponseReceivedHandler()
* @uses self::httpClientConnectionFailedHandler()
Expand Down Expand Up @@ -240,6 +246,40 @@ private function resolveQueryOriginFromBacktrace(): ?string
return "{$filePath}:{$firstAppFrame->getLine()}";
}

protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginning $event): void
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

if ($parentSpan === null) {
return;
}

$context = new SpanContext;
$context->setOp('db.transaction');

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

protected function transactionCommittedHandler(DatabaseEvents\TransactionCommitted $event): void
{
$span = $this->popSpan();

if ($span !== null) {
$span->finish();
$span->setStatus(SpanStatus::ok());
}
}

protected function transactionRolledBackHandler(DatabaseEvents\TransactionRolledBack $event): void
{
$span = $this->popSpan();

if ($span !== null) {
$span->finish();
$span->setStatus(SpanStatus::internalError());
}
}

protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSending $event): void
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
Expand All @@ -252,7 +292,6 @@ protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSendi

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

$this->pushSpan($parentSpan->startChild($context));
}
Expand Down

0 comments on commit 9254120

Please sign in to comment.