diff --git a/src/State/Hub.php b/src/State/Hub.php index 0bd7aa3c8..3840088fe 100644 --- a/src/State/Hub.php +++ b/src/State/Hub.php @@ -252,6 +252,10 @@ public function getIntegration(string $className): ?IntegrationInterface public function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction { $transaction = new Transaction($context, $this); + + // Sync the transaction name with the scope + $this->getScope()->setTransactionName($transaction->getName()); + $client = $this->getClient(); $options = $client !== null ? $client->getOptions() : null; diff --git a/src/State/Scope.php b/src/State/Scope.php index 546072f4e..c7fc4d5ab 100644 --- a/src/State/Scope.php +++ b/src/State/Scope.php @@ -75,6 +75,11 @@ class Scope */ private $span; + /** + * @var string|null The transaction name + */ + private $transactionName; + /** * @var callable[] List of event processors * @@ -363,6 +368,10 @@ public function applyToEvent(Event $event, ?EventHint $hint = null, ?Options $op $event->setExtra(array_merge($this->extra, $event->getExtra())); } + if ($this->transactionName !== null) { + $event->setTransaction($this->transactionName); + } + if ($this->user !== null) { $user = $event->getUser(); @@ -460,6 +469,25 @@ public function getTransaction(): ?Transaction return null; } + /** + * Set the transaction name on the scope as well as on the transaction + * attached to the scope (if there is one). + */ + public function setTransactionName(string $name): self + { + $this->transactionName = $name; + + // If we have an active transaction, update its name as well + if ($this->span !== null) { + $transaction = $this->span->getTransaction(); + if ($transaction !== null) { + $transaction->setName($name); + } + } + + return $this; + } + public function getPropagationContext(): PropagationContext { return $this->propagationContext; diff --git a/src/Tracing/Transaction.php b/src/Tracing/Transaction.php index a0faf4215..3417d3713 100644 --- a/src/Tracing/Transaction.php +++ b/src/Tracing/Transaction.php @@ -9,6 +9,7 @@ use Sentry\Profiling\Profiler; use Sentry\SentrySdk; use Sentry\State\HubInterface; +use Sentry\State\Scope; /** * This class stores all the information about a Transaction. @@ -77,6 +78,11 @@ public function setName(string $name): self { $this->name = $name; + // Sync the transaction name with the scope + $this->hub->configureScope(function (Scope $scope) use ($name) { + $scope->setTransactionName($name); + }); + return $this; }