Skip to content

Commit

Permalink
Strip path prefixes on profile frames (#1568)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Jul 26, 2023
1 parent c965f55 commit 15a028d
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 68 deletions.
23 changes: 5 additions & 18 deletions src/FrameBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sentry;

use Sentry\Serializer\RepresentationSerializerInterface;
use Sentry\Util\PrefixStripper;

/**
* This class builds a {@see Frame} object out of a backtrace's raw frame.
Expand All @@ -22,6 +23,8 @@
*/
final class FrameBuilder
{
use PrefixStripper;

/**
* @var Options The SDK client options
*/
Expand Down Expand Up @@ -66,13 +69,13 @@ public function buildFromBacktraceFrame(string $file, int $line, array $backtrac

$functionName = null;
$rawFunctionName = null;
$strippedFilePath = $this->stripPrefixFromFilePath($file);
$strippedFilePath = $this->stripPrefixFromFilePath($this->options, $file);

if (isset($backtraceFrame['class']) && isset($backtraceFrame['function'])) {
$functionName = $backtraceFrame['class'];

if (str_starts_with($functionName, Frame::ANONYMOUS_CLASS_PREFIX)) {
$functionName = Frame::ANONYMOUS_CLASS_PREFIX . $this->stripPrefixFromFilePath(substr($backtraceFrame['class'], \strlen(Frame::ANONYMOUS_CLASS_PREFIX)));
$functionName = Frame::ANONYMOUS_CLASS_PREFIX . $this->stripPrefixFromFilePath($this->options, substr($backtraceFrame['class'], \strlen(Frame::ANONYMOUS_CLASS_PREFIX)));
}

$rawFunctionName = sprintf('%s::%s', $backtraceFrame['class'], $backtraceFrame['function']);
Expand All @@ -92,22 +95,6 @@ public function buildFromBacktraceFrame(string $file, int $line, array $backtrac
);
}

/**
* Removes from the given file path the specified prefixes.
*
* @param string $filePath The path to the file
*/
private function stripPrefixFromFilePath(string $filePath): string
{
foreach ($this->options->getPrefixes() as $prefix) {
if (str_starts_with($filePath, $prefix)) {
return mb_substr($filePath, mb_strlen($prefix));
}
}

return $filePath;
}

/**
* Checks whether a certain frame should be marked as "in app" or not.
*
Expand Down
17 changes: 15 additions & 2 deletions src/Profiling/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Sentry\Context\RuntimeContext;
use Sentry\Event;
use Sentry\EventId;
use Sentry\Options;
use Sentry\Util\PrefixStripper;
use Sentry\Util\SentryUid;

/**
Expand Down Expand Up @@ -73,6 +75,8 @@
*/
final class Profile
{
use PrefixStripper;

/**
* @var string The version of the profile format
*/
Expand Down Expand Up @@ -108,6 +112,16 @@ final class Profile
*/
private $eventId;

/**
* @var Options|null
*/
private $options;

public function __construct(?Options $options = null)
{
$this->options = $options;
}

public function setStartTimeStamp(float $startTimeStamp): void
{
$this->startTimeStamp = $startTimeStamp;
Expand Down Expand Up @@ -160,8 +174,7 @@ public function getFormattedData(Event $event): ?array
foreach ($loggedStacks as $stackId => $stack) {
foreach ($stack['trace'] as $frame) {
$absolutePath = (string) $frame['file'];
// TODO(michi) Strip the file path based on the `prefixes` option
$file = $absolutePath;
$file = $this->stripPrefixFromFilePath($this->options, $absolutePath);
$module = null;

if (isset($frame['class'], $frame['function'])) {
Expand Down
6 changes: 4 additions & 2 deletions src/Profiling/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Sentry\Profiling;

use Sentry\Options;

/**
* @internal
*/
Expand All @@ -29,9 +31,9 @@ final class Profiler
*/
private const MAX_STACK_DEPTH = 128;

public function __construct()
public function __construct(?Options $options = null)
{
$this->profile = new Profile();
$this->profile = new Profile($options);

$this->initProfiler();
}
Expand Down
5 changes: 4 additions & 1 deletion src/Tracing/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public function detachSpanRecorder(): void
public function initProfiler(): void
{
if (null === $this->profiler) {
$this->profiler = new Profiler();
$client = $this->hub->getClient();
$options = null !== $client ? $client->getOptions() : null;

$this->profiler = new Profiler($options);
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/Util/PrefixStripper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Sentry\Util;

use Sentry\Options;

trait PrefixStripper
{
/**
* Removes from the given file path the specified prefixes in the SDK options.
*/
protected function stripPrefixFromFilePath(?Options $options, string $filePath): string
{
if (null === $options) {
return $filePath;
}

foreach ($options->getPrefixes() as $prefix) {
if (str_starts_with($filePath, $prefix)) {
return mb_substr($filePath, mb_strlen($prefix));
}
}

return $filePath;
}
}
Loading

0 comments on commit 15a028d

Please sign in to comment.