Skip to content

Commit c16fffd

Browse files
authored
Strip away the controllers base namespace (#393)
1 parent be93e34 commit c16fffd

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

config/sentry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@
3131
'send_default_pii' => false,
3232

3333
'traces_sample_rate' => (float)(env('SENTRY_TRACES_SAMPLE_RATE', 0.0)),
34+
35+
'controllers_base_namespace' => env('SENTRY_CONTROLLERS_BASE_NAMESPACE', 'App\\Http\\Controllers'),
36+
3437
];

src/Sentry/Laravel/Integration.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Integration implements IntegrationInterface
2121
*/
2222
private static $transaction;
2323

24+
/**
25+
* @var null|string
26+
*/
27+
private static $baseControllerNamespace;
28+
2429
/**
2530
* {@inheritdoc}
2631
*/
@@ -84,11 +89,19 @@ public static function getTransaction(): ?string
8489
/**
8590
* @param null|string $transaction
8691
*/
87-
public static function setTransaction($transaction): void
92+
public static function setTransaction(?string $transaction): void
8893
{
8994
self::$transaction = $transaction;
9095
}
9196

97+
/**
98+
* @param null|string $namespace
99+
*/
100+
public static function setControllersBaseNamespace(?string $namespace): void
101+
{
102+
self::$baseControllerNamespace = $namespace !== null ? trim($namespace, '\\') : null;
103+
}
104+
92105
/**
93106
* Block until all async events are processed for the HTTP transport.
94107
*
@@ -136,7 +149,7 @@ public static function extractNameForRoute(Route $route): ?string
136149

137150
if (empty($routeName) && $route->getActionName()) {
138151
// SomeController@someAction (controller action)
139-
$routeName = ltrim($route->getActionName(), '\\');
152+
$routeName = ltrim($route->getActionName(), (self::$baseControllerNamespace ?? '') . '\\');
140153
}
141154

142155
if (empty($routeName) || $routeName === 'Closure') {

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414

1515
class ServiceProvider extends BaseServiceProvider
1616
{
17+
/**
18+
* List of configuration options that are Laravel specific and should not be sent to the base PHP SDK.
19+
*/
20+
private const LARAVEL_SPECIFIC_OPTIONS = [
21+
// We do not want this setting to hit our main client because it's Laravel specific
22+
'breadcrumbs',
23+
// We resolve the integrations through the container later, so we initially do not pass it to the SDK yet
24+
'integrations',
25+
// This is kept for backwards compatibility and can be dropped in a future breaking release
26+
'breadcrumbs.sql_bindings',
27+
// The base namespace for controllers to strip of the beginning of controller class names
28+
'controllers_base_namespace',
29+
];
30+
1731
/**
1832
* Boot the service provider.
1933
*/
@@ -92,18 +106,17 @@ protected function registerArtisanCommands(): void
92106
*/
93107
protected function configureAndRegisterClient(): void
94108
{
109+
$userConfig = $this->getUserConfig();
110+
111+
Integration::setControllersBaseNamespace($userConfig['controllers_base_namespace']);
112+
95113
$this->app->bind(ClientBuilderInterface::class, function () {
96114
$basePath = base_path();
97115
$userConfig = $this->getUserConfig();
98116

99-
unset(
100-
// We do not want this setting to hit our main client because it's Laravel specific
101-
$userConfig['breadcrumbs'],
102-
// We resolve the integrations through the container later, so we initially do not pass it to the SDK yet
103-
$userConfig['integrations'],
104-
// This is kept for backwards compatibility and can be dropped in a future breaking release
105-
$userConfig['breadcrumbs.sql_bindings']
106-
);
117+
foreach (self::LARAVEL_SPECIFIC_OPTIONS as $laravelSpecificOptionName) {
118+
unset($userConfig[$laravelSpecificOptionName]);
119+
}
107120

108121
$options = \array_merge(
109122
[

0 commit comments

Comments
 (0)