Skip to content

Commit 253fb04

Browse files
committed
Add Scope:: setTransactionName()
1 parent 1b9e3b2 commit 253fb04

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/State/Hub.php

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ public function getIntegration(string $className): ?IntegrationInterface
252252
public function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction
253253
{
254254
$transaction = new Transaction($context, $this);
255+
256+
// Sync the transaction name with the scope
257+
$this->getScope()->setTransactionName($transaction->getName());
258+
255259
$client = $this->getClient();
256260
$options = $client !== null ? $client->getOptions() : null;
257261

src/State/Scope.php

+27
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class Scope
7575
*/
7676
private $span;
7777

78+
/**
79+
* @var string|null The transaction name
80+
*/
81+
private $transactionName;
82+
7883
/**
7984
* @var callable[] List of event processors
8085
*
@@ -363,6 +368,10 @@ public function applyToEvent(Event $event, ?EventHint $hint = null, ?Options $op
363368
$event->setExtra(array_merge($this->extra, $event->getExtra()));
364369
}
365370

371+
if ($this->transactionName !== null) {
372+
$event->setTransaction($this->transactionName);
373+
}
374+
366375
if ($this->user !== null) {
367376
$user = $event->getUser();
368377

@@ -460,6 +469,24 @@ public function getTransaction(): ?Transaction
460469
return null;
461470
}
462471

472+
/**
473+
* Returns the transaction attached to the scope (if there is one).
474+
*/
475+
public function setTransactionName(string $name): self
476+
{
477+
$this->transactionName = $name;
478+
479+
// If we have an active transaction, update its name as well
480+
if ($this->span !== null) {
481+
$transaction = $this->span->getTransaction();
482+
if ($transaction !== null) {
483+
$transaction->setName($name);
484+
}
485+
}
486+
487+
return $this;
488+
}
489+
463490
public function getPropagationContext(): PropagationContext
464491
{
465492
return $this->propagationContext;

src/Tracing/Transaction.php

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Sentry\Profiling\Profiler;
1010
use Sentry\SentrySdk;
1111
use Sentry\State\HubInterface;
12+
use Sentry\State\Scope;
1213

1314
/**
1415
* This class stores all the information about a Transaction.
@@ -77,6 +78,11 @@ public function setName(string $name): self
7778
{
7879
$this->name = $name;
7980

81+
// Sync the transaction name with the scope
82+
$this->hub->configureScope(function (Scope $scope) use ($name) {
83+
$scope->setTransactionName($name);
84+
});
85+
8086
return $this;
8187
}
8288

0 commit comments

Comments
 (0)