From 760c832121079479c1c344ec4c756393fd6de44e Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 1 Oct 2020 18:03:08 +0200 Subject: [PATCH] Strip away the controllers base namespace --- config/sentry.php | 3 +++ src/Sentry/Laravel/Integration.php | 17 +++++++++++++-- src/Sentry/Laravel/ServiceProvider.php | 29 +++++++++++++++++++------- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/config/sentry.php b/config/sentry.php index 7fec6c73..05f7951b 100644 --- a/config/sentry.php +++ b/config/sentry.php @@ -31,4 +31,7 @@ 'send_default_pii' => false, 'traces_sample_rate' => (float)(env('SENTRY_TRACES_SAMPLE_RATE', 0.0)), + + 'controllers_base_namespace' => env('SENTRY_CONTROLLERS_BASE_NAMESPACE', 'App\\Http\\Controllers'), + ]; diff --git a/src/Sentry/Laravel/Integration.php b/src/Sentry/Laravel/Integration.php index 01d215c9..ae767e0b 100644 --- a/src/Sentry/Laravel/Integration.php +++ b/src/Sentry/Laravel/Integration.php @@ -21,6 +21,11 @@ class Integration implements IntegrationInterface */ private static $transaction; + /** + * @var null|string + */ + private static $baseControllerNamespace; + /** * {@inheritdoc} */ @@ -84,11 +89,19 @@ public static function getTransaction(): ?string /** * @param null|string $transaction */ - public static function setTransaction($transaction): void + public static function setTransaction(?string $transaction): void { self::$transaction = $transaction; } + /** + * @param null|string $namespace + */ + public static function setControllersBaseNamespace(?string $namespace): void + { + self::$baseControllerNamespace = $namespace !== null ? trim($namespace, '\\') : null; + } + /** * Block until all async events are processed for the HTTP transport. * @@ -136,7 +149,7 @@ public static function extractNameForRoute(Route $route): ?string if (empty($routeName) && $route->getActionName()) { // SomeController@someAction (controller action) - $routeName = ltrim($route->getActionName(), '\\'); + $routeName = ltrim($route->getActionName(), (self::$baseControllerNamespace ?? '') . '\\'); } if (empty($routeName) || $routeName === 'Closure') { diff --git a/src/Sentry/Laravel/ServiceProvider.php b/src/Sentry/Laravel/ServiceProvider.php index c6e144a8..67874d23 100644 --- a/src/Sentry/Laravel/ServiceProvider.php +++ b/src/Sentry/Laravel/ServiceProvider.php @@ -14,6 +14,20 @@ class ServiceProvider extends BaseServiceProvider { + /** + * List of configuration options that are Laravel specific and should not be sent to the base PHP SDK. + */ + private const LARAVEL_SPECIFIC_OPTIONS = [ + // We do not want this setting to hit our main client because it's Laravel specific + 'breadcrumbs', + // We resolve the integrations through the container later, so we initially do not pass it to the SDK yet + 'integrations', + // This is kept for backwards compatibility and can be dropped in a future breaking release + 'breadcrumbs.sql_bindings', + // The base namespace for controllers to strip of the beginning of controller class names + 'controllers_base_namespace', + ]; + /** * Boot the service provider. */ @@ -92,18 +106,17 @@ protected function registerArtisanCommands(): void */ protected function configureAndRegisterClient(): void { + $userConfig = $this->getUserConfig(); + + Integration::setControllersBaseNamespace($userConfig['controllers_base_namespace']); + $this->app->bind(ClientBuilderInterface::class, function () { $basePath = base_path(); $userConfig = $this->getUserConfig(); - unset( - // We do not want this setting to hit our main client because it's Laravel specific - $userConfig['breadcrumbs'], - // We resolve the integrations through the container later, so we initially do not pass it to the SDK yet - $userConfig['integrations'], - // This is kept for backwards compatibility and can be dropped in a future breaking release - $userConfig['breadcrumbs.sql_bindings'] - ); + foreach (self::LARAVEL_SPECIFIC_OPTIONS as $laravelSpecificOptionName) { + unset($userConfig[$laravelSpecificOptionName]); + } $options = \array_merge( [