Skip to content

Add db.transaction span #594

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

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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