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
13 changes: 12 additions & 1 deletion src/Profiling/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ final class Profiler implements ProfilerInterface
*/
private $profile;

/**
* @var bool Whether the Excimer extension was available when this profiler was created
*/
private $available = false;

/**
* @var LoggerInterface
*/
Expand Down Expand Up @@ -59,12 +64,17 @@ public function stop(): void
$this->profiler->stop();

$this->profile->setExcimerLog($this->profiler->flush());

// Release the Excimer profiler right away: it holds an OS timer (a thread
// on Linux) that is only freed on destruction. The owning transaction is
// part of a reference cycle, so it may otherwise live until a GC run.
$this->profiler = null;
}
}

public function getProfile(): ?Profile
{
if ($this->profiler === null) {
if (!$this->available) {
return null;
}

Expand All @@ -80,6 +90,7 @@ private function initProfiler(): void
}

$this->profiler = new \ExcimerProfiler();
$this->available = true;
$this->profile->setStartTimeStamp(microtime(true));

$this->profiler->setEventType(\EXCIMER_REAL);
Expand Down
84 changes: 84 additions & 0 deletions tests/Profiling/ProfilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Sentry\Tests\Profiling;

use PHPUnit\Framework\TestCase;
use Sentry\Client;
use Sentry\Event;
use Sentry\EventType;
use Sentry\Options;
use Sentry\Profiling\Profile;
use Sentry\Profiling\Profiler;
use Sentry\SentrySdk;
use Sentry\Tests\StubTransport;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;

/**
* @requires extension excimer
*/
final class ProfilerTest extends TestCase
{
public function testStopReleasesTheExcimerProfiler(): void
{
$profiler = new Profiler(new Options());

$this->assertInstanceOf(\ExcimerProfiler::class, $this->getExcimerProfiler($profiler));

$profiler->start();
$profiler->stop();

$this->assertNull($this->getExcimerProfiler($profiler));
$this->assertInstanceOf(Profile::class, $profiler->getProfile());

// Calling start/stop again after the profiler has been released is a no-op
$profiler->start();
$profiler->stop();

$this->assertInstanceOf(Profile::class, $profiler->getProfile());
}

public function testFinishingTransactionReleasesTheExcimerProfilerAndKeepsTheProfile(): void
{
StubTransport::$events = [];

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

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

$profiler = $transaction->initProfiler();
$profiler->start();

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

$this->assertInstanceOf(Profiler::class, $profiler);
$this->assertNull($this->getExcimerProfiler($profiler));

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

$this->assertCount(1, $transactionEvents);
$this->assertSame($profiler->getProfile(), $transactionEvents[0]->getSdkMetadata('profile'));
}

private function getExcimerProfiler(Profiler $profiler): ?\ExcimerProfiler
{
$property = new \ReflectionProperty(Profiler::class, 'profiler');

if (\PHP_VERSION_ID < 80100) {
$property->setAccessible(true);
}

return $property->getValue($profiler);
}
}
Loading