Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow profiler to start before SDK and transaction is initialized #1778

Closed
wants to merge 2 commits into from
Closed
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
97 changes: 84 additions & 13 deletions src/Profiling/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ final class Profiler
/**
* @var \ExcimerProfiler|null
*/
private $profiler;
private static $profiler;

/**
* @var float|null
*/
private static $startedAt;

/**
* @var Profile
Expand Down Expand Up @@ -48,23 +53,39 @@ public function __construct(?Options $options = null)

public function start(): void
{
if ($this->profiler !== null) {
$this->profiler->start();
if (self::$profiler === null) {
return;
}

// Prevent starting the profiler multiple times since that will discard the previous data
// If you need to "restart" profiling, stop first to remove data and start again to start fresh
if (self::$startedAt !== null) {
return;
}

self::startProfiling();
}

public function stop(): void
{
if ($this->profiler !== null) {
$this->profiler->stop();
if (self::$profiler === null) {
return;
}

self::$profiler->stop();

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

$this->profile->setExcimerLog($this->profiler->flush());
if (self::$startedAt !== null) {
$this->profile->setStartTimeStamp(self::$startedAt);

self::$startedAt = null;
}
}

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

Expand All @@ -73,17 +94,67 @@ public function getProfile(): ?Profile

private function initProfiler(): void
{
if (!\extension_loaded('excimer')) {
if (self::$startedAt !== null) {
$this->logger->debug('The profiler was already started, will continue profiling.', ['started_at' => self::$startedAt]);

$this->profile->setStartTimeStamp(self::$startedAt);

return;
}

self::setupProfiler();

if (self::$profiler === null) {
$this->logger->warning('The profiler was started but is not available because the "excimer" extension is not loaded.');

return;
}

$this->profiler = new \ExcimerProfiler();
$this->profile->setStartTimeStamp(microtime(true));
self::startProfiling();
}

private static function setupProfiler(): void
{
// Setup is only needed once
if (self::$profiler !== null) {
return;
}

if (!\extension_loaded('excimer')) {
return;
}

$profiler = new \ExcimerProfiler();

$profiler->setPeriod(self::SAMPLE_RATE);
$profiler->setMaxDepth(self::MAX_STACK_DEPTH);
$profiler->setEventType(\EXCIMER_REAL);

self::$profiler = $profiler;
}

public static function startProfiling(): void
{
self::setupProfiler();

if (self::$profiler === null) {
return;
}

self::$profiler->start();
self::$startedAt = microtime(true);
}

public static function maybeDiscardProfilingData(): void
{
// If we don't have an instance of the profiler there is no data to discard making this a no-op if the profiler was not started
if (self::$profiler === null) {
return;
}

self::$profiler->stop();

$this->profiler->setEventType(\EXCIMER_REAL);
$this->profiler->setPeriod(self::SAMPLE_RATE);
$this->profiler->setMaxDepth(self::MAX_STACK_DEPTH);
self::$profiler = null;
self::$startedAt = null;
}
}
5 changes: 5 additions & 0 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\Profiling\Profiler;
use Sentry\Severity;
use Sentry\Tracing\SamplingContext;
use Sentry\Tracing\Span;
Expand Down Expand Up @@ -320,12 +321,16 @@ public function startTransaction(TransactionContext $context, array $customSampl
$profilesSampleRate = $options->getProfilesSampleRate();
if ($profilesSampleRate === null) {
$logger->info(\sprintf('Transaction [%s] is not profiling because `profiles_sample_rate` option is not set.', (string) $transaction->getTraceId()));

Profiler::maybeDiscardProfilingData();
} elseif ($this->sample($profilesSampleRate)) {
$logger->info(\sprintf('Transaction [%s] started profiling because it was sampled.', (string) $transaction->getTraceId()));

$transaction->initProfiler()->start();
} else {
$logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled.', (string) $transaction->getTraceId()));

Profiler::maybeDiscardProfilingData();
}

return $transaction;
Expand Down
Loading