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
2 changes: 1 addition & 1 deletion analysis-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ count = 2
file = "src/Profiling/ContinuousProfiler.php"
code = "invalid-method-access"
message = "Attempting to access a method on a non-object type (`unknown-ref(ExcimerProfiler)`)."
count = 2
count = 3

[[issues]]
file = "src/Profiling/ContinuousProfiler.php"
Expand Down
9 changes: 9 additions & 0 deletions src/Profiling/ContinuousProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ public function stop(): void
$this->activeRootSpans = [];
}

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

$this->profiler->flush();
}
Comment thread
cursor[bot] marked this conversation as resolved.

public function isRunning(): bool
{
return $this->profiler !== null;
Expand Down
8 changes: 8 additions & 0 deletions src/Profiling/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function stop(): void
$this->continuousProfiler->stop();
}

/**
* Sends the samples collected so far to Sentry, the profiler keeps running.
*/
public function flush(): void
{
$this->continuousProfiler->flush();
}

/**
* Get the continuous profiler.
*
Expand Down
2 changes: 2 additions & 0 deletions src/SentrySdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\Profiling\Profiles;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\State\RuntimeContext;
Expand Down Expand Up @@ -187,6 +188,7 @@ public static function flush(): void
{
Logs::getInstance()->flush();
TraceMetrics::getInstance()->flush();
Profiles::getInstance()->flush();

$client = self::getCurrentHub()->getClient();

Expand Down
45 changes: 45 additions & 0 deletions tests/SentrySdkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\Client;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\Options;
use Sentry\Profiling\Profiles;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\Scope;
Expand Down Expand Up @@ -406,6 +408,40 @@ public function testEndContextFlushesClientTransportWithOptionalTimeout(): void
SentrySdk::endContext(12);
}

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

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

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

$profilerId = $profiler->getProfilerId();

self::busyWait(0.05);

SentrySdk::flush();

$this->assertTrue($profiler->isRunning());
$this->assertSame($profilerId, $profiler->getProfilerId());
$this->assertCount(1, StubTransport::$events);

$profileChunk = StubTransport::$events[0]->getProfileChunk();

$this->assertNotNull($profileChunk);
$this->assertSame($profilerId, $profileChunk->getProfilerId());

self::busyWait(0.05);

$profiler->stop();

$this->assertCount(2, StubTransport::$events);
}

public function testFlushFlushesClientTransport(): void
{
/** @var ClientInterface&MockObject $client */
Expand Down Expand Up @@ -565,4 +601,13 @@ private function getCurrentScopeTag(string $key): ?string

return $value;
}

private static function busyWait(float $seconds): void
{
$end = microtime(true) + $seconds;

while (microtime(true) < $end) {
// Burn CPU so that Excimer collects samples
}
}
}
Loading