Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip away the controllers base namespace #393

Merged
merged 1 commit into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/sentry.php
Expand Up @@ -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'),

];
17 changes: 15 additions & 2 deletions src/Sentry/Laravel/Integration.php
Expand Up @@ -21,6 +21,11 @@ class Integration implements IntegrationInterface
*/
private static $transaction;

/**
* @var null|string
*/
private static $baseControllerNamespace;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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') {
Expand Down
29 changes: 21 additions & 8 deletions src/Sentry/Laravel/ServiceProvider.php
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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(
[
Expand Down