Skip to content

Commit

Permalink
Add Scope:: setTransactionName()
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Mar 13, 2024
1 parent 1b9e3b2 commit ea55d1d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 28 additions & 0 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Scope
*/
private $span;

/**
* @var string|null The transaction name
*/
private $transactionName;

/**
* @var callable[] List of event processors
*
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/Tracing/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit ea55d1d

Please sign in to comment.