Skip to content
Open
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
6 changes: 6 additions & 0 deletions analysis-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,12 @@ code = "invalid-return-statement"
message = 'Invalid return type for function `Sentry\Tracing\Transaction::getDynamicSamplingContext`: expected `Sentry\Tracing\DynamicSamplingContext`, but found `Sentry\Tracing\DynamicSamplingContext|null`.'
count = 1

[[issues]]
file = "src/Tracing/Transaction.php"
code = "mixed-assignment"
message = "Assigning `mixed` type to a variable may lead to unexpected behavior."
count = 1

[[issues]]
file = "src/Tracing/Transaction.php"
code = "nullable-return-statement"
Expand Down
4 changes: 2 additions & 2 deletions src/Profiling/ProfileChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ final class ProfileChunk
/**
* @var string
*/
private const THREAD_ID = '0';
public const THREAD_ID = '0';

/**
* @var string
*/
private const THREAD_NAME = 'main';
public const THREAD_NAME = 'main';

/**
* @var int we need at least 2 samples otherwise the event is dropped
Expand Down
13 changes: 12 additions & 1 deletion src/Tracing/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Sentry\Tracing;

use Sentry\EventId;
use Sentry\Profiling\ProfileChunk;
use Sentry\Profiling\Profiles;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Unit;
Expand Down Expand Up @@ -115,6 +117,15 @@ class Span
*/
public function __construct(?SpanContext $context = null)
{
$profilerId = Profiles::getInstance()->continuousProfiler()->getProfilerId();
if ($profilerId !== null) {
$this->data = [
'thread.id' => ProfileChunk::THREAD_ID,
'thread.name' => ProfileChunk::THREAD_NAME,
'profiler_id' => $profilerId,
];
}

if ($context === null) {
$this->traceId = TraceId::generate();
$this->spanId = SpanId::generate();
Expand All @@ -132,7 +143,7 @@ public function __construct(?SpanContext $context = null)
$this->status = $context->getStatus();
$this->sampled = $context->getSampled();
$this->tags = $context->getTags();
$this->data = $context->getData();
$this->data = $context->getData() + $this->data;
$this->endTimestamp = $context->getEndTimestamp();
$this->origin = $context->getOrigin();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Tracing/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ public function finish(?float $endTimestamp = null): ?EventId
$event->setSdkMetadata('dynamic_sampling_context', $this->getDynamicSamplingContext());
$event->setSdkMetadata('transaction_metadata', $this->getMetadata());

$profilerId = $this->data['profiler_id'] ?? null;

if (\is_string($profilerId)) {
$event->setContext('profile', ['profiler_id' => $profilerId]);
}

if ($this->profiler !== null) {
$profile = $this->profiler->getProfile();
if ($profile !== null) {
Expand Down
53 changes: 53 additions & 0 deletions tests/Tracing/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
namespace Sentry\Tests\Tracing;

use PHPUnit\Framework\TestCase;
use Sentry\Client;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventId;
use Sentry\EventType;
use Sentry\Options;
use Sentry\Profiling\Profiles;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\Tests\StubTransport;
use Sentry\Tests\TestUtil\ClockMock;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
Expand Down Expand Up @@ -75,6 +79,55 @@ public function testFinish(): void
$this->assertSame($expectedEventId, $eventId);
}

/**
* @requires extension excimer
*/
public function testSpansCreatedWhileProfilingAreLinkedToTheProfile(): void
{
StubTransport::$events = [];

$hub = SentrySdk::init();
$hub->bindClient(new Client(new Options([
'profile_session_sample_rate' => 1.0,
]), StubTransport::getInstance()));

$profiler = Profiles::getInstance()->continuousProfiler();
$profiler->start();

$profilerId = $profiler->getProfilerId();

$transaction = new Transaction(TransactionContext::make()->setSampled(true), $hub);
$transaction->initSpanRecorder();

$profiledSpan = $transaction->startChild(new SpanContext());
$profiledSpan->finish();

$profiler->stop();

$unprofiledSpan = $transaction->startChild(new SpanContext());
$unprofiledSpan->finish();

$transaction->finish();

$transactionEvents = array_values(array_filter(StubTransport::$events, static function (Event $event): bool {
return $event->getType() === EventType::transaction();
}));

$this->assertCount(1, $transactionEvents);

$event = $transactionEvents[0];
$profileData = [
'thread.id' => '0',
'thread.name' => 'main',
'profiler_id' => $profilerId,
];

$this->assertSame(['profiler_id' => $profilerId], $event->getContexts()['profile']);
$this->assertSame($profileData, $event->getContexts()['trace']['data']);
$this->assertSame($profileData, $profiledSpan->getData());
$this->assertSame([], $unprofiledSpan->getData());
}

public function testFinishDoesNothingIfSampledFlagIsNotTrue(): void
{
$hub = $this->createMock(HubInterface::class);
Expand Down
Loading