diff --git a/src/Sentry/Laravel/Profiling/FrameProcessor.php b/src/Sentry/Laravel/Profiling/FrameProcessor.php new file mode 100644 index 00000000..01208d75 --- /dev/null +++ b/src/Sentry/Laravel/Profiling/FrameProcessor.php @@ -0,0 +1,40 @@ +<?php + +namespace Sentry\Laravel\Profiling; + +use Illuminate\Support\Str; +use Sentry\Laravel\Tracing\BacktraceHelper; + +class FrameProcessor +{ + /** + * @param array $frame + * + * @return array + */ + public function __invoke(array $frame): array + { + // Check if we are dealing with a frame for a cached view path + if (Str::startsWith($frame['filename'], '/storage/framework/views/')) { + $originalViewPath = $this->backtraceHelper()->getOriginalViewPathForCompiledViewPath($frame['abs_path']); + + if ($originalViewPath !== null) { + // For views both the filename and function is the view path so we set them both to the original view path + $frame['filename'] = $frame['function'] = $originalViewPath; + } + } + + return $frame; + } + + private function backtraceHelper(): BacktraceHelper + { + static $helper = null; + + if ($helper === null) { + $helper = app(BacktraceHelper::class); + } + + return $helper; + } +} diff --git a/src/Sentry/Laravel/Tracing/BacktraceHelper.php b/src/Sentry/Laravel/Tracing/BacktraceHelper.php index 4d57cab3..bd77d804 100644 --- a/src/Sentry/Laravel/Tracing/BacktraceHelper.php +++ b/src/Sentry/Laravel/Tracing/BacktraceHelper.php @@ -75,12 +75,24 @@ public function getOriginalViewPathForFrameOfCompiledViewPath(Frame $frame): ?st return null; } + return $this->getOriginalViewPathForCompiledViewPath($frame->getAbsoluteFilePath()); + } + + /** + * Takes a path and if it's a compiled view returns the original view path. + * + * @param string $suspectedViewPath + * + * @return string|null + */ + public function getOriginalViewPathForCompiledViewPath(string $suspectedViewPath): ?string + { // If for some reason the file does not exists, skip resolving - if (!file_exists($frame->getAbsoluteFilePath())) { + if (!file_exists($suspectedViewPath)) { return null; } - $viewFileContents = file_get_contents($frame->getAbsoluteFilePath()); + $viewFileContents = file_get_contents($suspectedViewPath); preg_match('/PATH (?<originalPath>.*?) ENDPATH/', $viewFileContents, $matches); diff --git a/src/Sentry/Laravel/Tracing/ServiceProvider.php b/src/Sentry/Laravel/Tracing/ServiceProvider.php index e7ccc5f5..5766d775 100644 --- a/src/Sentry/Laravel/Tracing/ServiceProvider.php +++ b/src/Sentry/Laravel/Tracing/ServiceProvider.php @@ -16,8 +16,10 @@ use InvalidArgumentException; use Laravel\Lumen\Application as Lumen; use Sentry\Laravel\BaseServiceProvider; +use Sentry\Laravel\Profiling\FrameProcessor; use Sentry\Laravel\Tracing\Routing\TracingCallableDispatcherTracing; use Sentry\Laravel\Tracing\Routing\TracingControllerDispatcherTracing; +use Sentry\Profiling\Profile; use Sentry\Serializer\RepresentationSerializer; class ServiceProvider extends BaseServiceProvider @@ -56,6 +58,8 @@ public function boot(): void $httpKernel->prependMiddleware(Middleware::class); } } + + Profile::setFrameProcessor(new FrameProcessor); } public function register(): void