Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix `Scope::getTransaction()` so that it returns also unsampled transactions (#1334)

## 3.6.1 (2022-06-27)

- Set the `sentry-trace` header when using the tracing middleware (#1331)
Expand Down
8 changes: 2 additions & 6 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,8 @@ public function setSpan(?Span $span): self
*/
public function getTransaction(): ?Transaction
{
$span = $this->span;

if (null !== $span && null !== $span->getSpanRecorder() && !empty($span->getSpanRecorder()->getSpans())) {
// The first span in the recorder is considered to be a Transaction
/** @var Transaction */
return $span->getSpanRecorder()->getSpans()[0];
if (null !== $this->span) {
return $this->span->getTransaction();
}

return null;
Expand Down
28 changes: 21 additions & 7 deletions src/Tracing/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Sentry\EventId;

/**
* This class stores all the information about a Span.
* This class stores all the information about a span.
*/
class Span
{
Expand All @@ -22,22 +22,22 @@ class Span
protected $traceId;

/**
* @var string|null Description of the Span
* @var string|null Description of the span
*/
protected $description;

/**
* @var string|null Operation of the Span
* @var string|null Operation of the span
*/
protected $op;

/**
* @var SpanStatus|null Completion status of the Span
* @var SpanStatus|null Completion status of the span
*/
protected $status;

/**
* @var SpanId|null ID of the parent Span
* @var SpanId|null ID of the parent span
*/
protected $parentSpanId;

Expand All @@ -47,7 +47,7 @@ class Span
protected $sampled;

/**
* @var array<string, string> A List of tags associated to this Span
* @var array<string, string> A List of tags associated to this span
*/
protected $tags = [];

Expand All @@ -67,10 +67,15 @@ class Span
protected $endTimestamp;

/**
* @var SpanRecorder|null Reference instance to the SpanRecorder
* @var SpanRecorder|null Reference instance to the {@see SpanRecorder}
*/
protected $spanRecorder;

/**
* @var Transaction|null The transaction containing this span
*/
protected $transaction;

/**
* Constructor.
*
Expand Down Expand Up @@ -390,6 +395,7 @@ public function startChild(SpanContext $context): self
$context->setTraceId($this->traceId);

$span = new self($context);
$span->transaction = $this->transaction;
$span->spanRecorder = $this->spanRecorder;

if (null != $span->spanRecorder) {
Expand Down Expand Up @@ -417,6 +423,14 @@ public function detachSpanRecorder(): void
$this->spanRecorder = null;
}

/**
* Returns the transaction containing this span.
*/
public function getTransaction(): ?Transaction
{
return $this->transaction;
}

/**
* Returns a string that can be used for the `sentry-trace` header.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Tracing/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct(TransactionContext $context, ?HubInterface $hub = nu

$this->hub = $hub ?? SentrySdk::getCurrentHub();
$this->name = $context->getName();
$this->transaction = $this;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/State/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,51 @@ public function testStartTransactionWithCustomSamplingContext(): void
$hub = new Hub($client);
$hub->startTransaction(new TransactionContext(), $customSamplingContext);
}

public function testGetTransactionReturnsInstanceSetOnTheScopeIfTransactionIsNotSampled(): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(new Options(['traces_sample_rate' => 1]));

$hub = new Hub($client);
$transaction = $hub->startTransaction(new TransactionContext(TransactionContext::DEFAULT_NAME, false));

$hub->configureScope(static function (Scope $scope) use ($transaction): void {
$scope->setSpan($transaction);
});

$this->assertSame($transaction, $hub->getTransaction());
}

public function testGetTransactionReturnsInstanceSetOnTheScopeIfTransactionIsSampled(): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(new Options(['traces_sample_rate' => 1]));

$hub = new Hub($client);
$transaction = $hub->startTransaction(new TransactionContext(TransactionContext::DEFAULT_NAME, true));

$hub->configureScope(static function (Scope $scope) use ($transaction): void {
$scope->setSpan($transaction);
});

$this->assertSame($transaction, $hub->getTransaction());
}

public function testGetTransactionReturnsNullIfNoTransactionIsSetOnTheScope(): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(new Options(['traces_sample_rate' => 1]));

$hub = new Hub($client);
$hub->startTransaction(new TransactionContext(TransactionContext::DEFAULT_NAME, true));

$this->assertNull($hub->getTransaction());
}
}