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

Profiler frame processor #1574

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Profiling/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ final class Profile
{
use PrefixStripper;

/**
* @var callable(SentryProfileFrame): SentryProfileFrame|null
*/
private static $frameProcessor;

/**
* @var string The version of the profile format
*/
Expand Down Expand Up @@ -214,13 +219,17 @@ public function getFormattedData(Event $event): ?array
}

$frameHashMap[$frameKey] = $frameIndex = \count($frames);
$frames[] = [

/** @var SentryProfileFrame $frame */
$frame = [
'filename' => $file,
'abs_path' => $absolutePath,
'module' => $module,
'function' => $function,
'lineno' => $lineno,
];

$frames[] = self::$frameProcessor !== null ? (self::$frameProcessor)($frame) : $frame;
}

$stackFrames[] = $frameIndex;
Expand Down Expand Up @@ -378,4 +387,16 @@ private function validateEvent(Event $event): bool

return true;
}

/**
* Set a callable to process each frame of the profile.
*
* @param callable(SentryProfileFrame $frame): SentryProfileFrame|null $frameProcessor
*
* @internal this method is used internally by framework SDKs to process each frame of the profile
*/
public static function setFrameProcessor(?callable $frameProcessor): void
{
self::$frameProcessor = $frameProcessor;
}
}
110 changes: 109 additions & 1 deletion tests/Profiling/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ final class ProfileTest extends TestCase
/**
* @dataProvider formattedDataDataProvider
*/
public function testGetFormattedData(Event $event, array $excimerLog, $expectedData, ?Options $options = null): void
public function testGetFormattedData(Event $event, array $excimerLog, $expectedData, ?Options $options = null, ?callable $frameProcessor = null): void
{
Profile::setFrameProcessor($frameProcessor);

$profile = new Profile($options);
// 2022-02-28T09:41:00Z
$profile->setStartTimeStamp(1677573660.0000);
Expand Down Expand Up @@ -316,6 +318,112 @@ public static function formattedDataDataProvider(): \Generator
]),
];

yield 'With frame processor' => [
$event,
$excimerLog,
[
'device' => [
'architecture' => 'aarch64',
],
'event_id' => '815e57b4bb134056ab1840919834689d',
'os' => [
'name' => 'macOS',
'version' => '13.2.1',
'build_number' => '22D68',
],
'platform' => 'php',
'release' => '1.0.0',
'environment' => 'dev',
'runtime' => [
'name' => 'php',
'version' => '8.2.3',
],
'timestamp' => '2023-02-28T08:41:00.000+00:00',
'transaction' => [
'id' => 'fc9442f5aef34234bb22b9a615e30ccd',
'name' => 'GET /',
'trace_id' => '566e3688a61d4bc888951642d6f14a19',
'active_thread_id' => '0',
],
'version' => '1',
'profile' => [
'frames' => [
[
'filename' => '/var/www/html/index.php',
'abs_path' => '/var/www/html/index.php',
'module' => 'foo',
'function' => '/var/www/html/index.php',
'lineno' => 42,
],
[
'filename' => '/var/www/html/function.php',
'abs_path' => '/var/www/html/function.php',
'module' => 'foo',
'function' => 'Function::doStuff',
'lineno' => 84,
],
[
'filename' => '/var/www/html/class.php',
'abs_path' => '/var/www/html/class.php',
'module' => 'foo',
'function' => 'Class\Something::run',
'lineno' => 42,
],
[
'filename' => '/var/www/html/index.php',
'abs_path' => '/var/www/html/index.php',
'module' => 'foo',
'function' => '{closure}',
'lineno' => 126,
],
],
'samples' => [
[
'stack_id' => 0,
'thread_id' => '0',
'elapsed_since_start_ns' => 1000000,
],
[
'stack_id' => 0,
'thread_id' => '0',
'elapsed_since_start_ns' => 2000000,
],
[
'stack_id' => 1,
'thread_id' => '0',
'elapsed_since_start_ns' => 3000000,
],
[
'stack_id' => 2,
'thread_id' => '0',
'elapsed_since_start_ns' => 4000000,
],
],
'stacks' => [
[
0,
],
[
0,
1,
],
[
0,
1,
2,
3,
],
],
],
],
null,
static function (array $data): array {
$data['module'] = 'foo';

return $data;
},
];

yield 'Too little samples' => [
$event,
[
Expand Down