Skip to content

Commit 535f662

Browse files
authored
Allow setting up features in an inactive state (#698)
1 parent 86ab3b6 commit 535f662

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/Sentry/Laravel/Features/ConsoleIntegration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public function setup(Cache $cache): void
5656
});
5757
}
5858

59+
public function setupInactive(): void
60+
{
61+
SchedulingEvent::macro('sentryMonitor', function (string $monitorSlug) {
62+
// When there is no Sentry DSN set there is nothing for us to do, but we still want to allow the user to setup the macro
63+
});
64+
}
65+
5966
private function startCheckIn(string $mutex, string $slug, bool $useCache, int $useCacheTtlInMinutes): void
6067
{
6168
$checkIn = $this->createCheckIn($slug, CheckInStatus::inProgress());

src/Sentry/Laravel/Features/Feature.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Sentry\Laravel\Features;
44

55
use Illuminate\Contracts\Container\Container;
6-
use Sentry\Integration\IntegrationInterface;
76
use Sentry\Laravel\BaseServiceProvider;
87
use Sentry\SentrySdk;
8+
use Throwable;
99

1010
/**
1111
* @method void setup() Setup the feature in the environment.
12+
* @method void setupInactive() Setup the feature in the environment in an inactive state (when no DSN was set).
1213
*
1314
* @internal
1415
*/
@@ -56,7 +57,21 @@ public function boot(): void
5657
if (method_exists($this, 'setup') && $this->isApplicable()) {
5758
try {
5859
$this->container->call([$this, 'setup']);
59-
} catch (\Throwable $exception) {
60+
} catch (Throwable $exception) {
61+
// If the feature setup fails, we don't want to prevent the rest of the SDK from working.
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Initializes the feature in an inactive state (when no DSN was set).
68+
*/
69+
public function bootInactive(): void
70+
{
71+
if (method_exists($this, 'setupInactive') && $this->isApplicable()) {
72+
try {
73+
$this->container->call([$this, 'setupInactive']);
74+
} catch (Throwable $exception) {
6075
// If the feature setup fails, we don't want to prevent the rest of the SDK from working.
6176
}
6277
}

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Sentry\Integration as SdkIntegration;
1919
use Sentry\Laravel\Console\PublishCommand;
2020
use Sentry\Laravel\Console\TestCommand;
21+
use Sentry\Laravel\Features\Feature;
2122
use Sentry\Laravel\Http\LaravelRequestFetcher;
2223
use Sentry\Laravel\Http\SetRequestIpMiddleware;
2324
use Sentry\Laravel\Http\SetRequestMiddleware;
@@ -26,6 +27,7 @@
2627
use Sentry\State\Hub;
2728
use Sentry\State\HubInterface;
2829
use Sentry\Tracing\TransactionMetadata;
30+
use Throwable;
2931

3032
class ServiceProvider extends BaseServiceProvider
3133
{
@@ -61,11 +63,11 @@ public function boot(): void
6163
{
6264
$this->app->make(HubInterface::class);
6365

66+
$this->setupFeatures();
67+
6468
if ($this->hasDsnSet()) {
6569
$this->bindEvents();
6670

67-
$this->setupFeatures();
68-
6971
if ($this->app instanceof Lumen) {
7072
$this->app->middleware(SetRequestMiddleware::class);
7173
$this->app->middleware(SetRequestIpMiddleware::class);
@@ -146,10 +148,17 @@ protected function bindEvents(): void
146148
*/
147149
protected function setupFeatures(): void
148150
{
151+
$bootActive = $this->hasDsnSet();
152+
149153
foreach (self::FEATURES as $feature) {
150154
try {
151-
$this->app->make($feature)->boot();
152-
} catch (\Throwable $e) {
155+
/** @var Feature $featureInstance */
156+
$featureInstance = $this->app->make($feature);
157+
158+
$bootActive
159+
? $featureInstance->boot()
160+
: $featureInstance->bootInactive();
161+
} catch (Throwable $e) {
153162
// Ensure that features do not break the whole application
154163
}
155164
}

0 commit comments

Comments
 (0)