Skip to content

Commit

Permalink
Option to set a frame processor on the profile
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Oct 24, 2023
1 parent f8b64d3 commit e93e16a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
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

0 comments on commit e93e16a

Please sign in to comment.