From dcc1ad552e2a6d6e9d042119410d247fedecf48f Mon Sep 17 00:00:00 2001 From: Michael Budde Date: Mon, 3 Apr 2023 09:40:07 +0200 Subject: [PATCH] Disable reporting if DSN is not set --- lib/Sentry/Hub.pm | 6 +++--- lib/Sentry/SDK.pm | 2 +- t/sdk.t | 11 ++++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/Sentry/Hub.pm b/lib/Sentry/Hub.pm index f727901..7a01a90 100644 --- a/lib/Sentry/Hub.pm +++ b/lib/Sentry/Hub.pm @@ -28,7 +28,7 @@ sub reset ($self) { sub bind_client ($self, $client) { $self->client($client); - $client->setup_integrations(); + $client->setup_integrations() if $client; } sub get_current_scope ($package) { @@ -66,7 +66,7 @@ sub get_scope ($self) { } sub _invoke_client ($self, $method, @args) { - my $client = $self->client; + my $client = $self->client or return; my $scope = $self->get_current_scope; if ($client->can($method)) { @@ -124,7 +124,7 @@ sub run ($self, $cb) { } sub sample ($self, $transaction, $sampling_context) { - my $client = $self->client; + my $client = $self->client or return; my $options = ($client && $client->get_options) // {}; # nothing to do if there's no client or if tracing is disabled diff --git a/lib/Sentry/SDK.pm b/lib/Sentry/SDK.pm index f6d0a90..127b702 100644 --- a/lib/Sentry/SDK.pm +++ b/lib/Sentry/SDK.pm @@ -22,7 +22,7 @@ sub _call_on_hub ($method, @args) { sub _init_and_bind ($options) { my $hub = Sentry::Hub->get_current_hub(); - my $client = Sentry::Client->new(_options => $options); + my $client = $options->{dsn} ? Sentry::Client->new(_options => $options) : undef; $hub->bind_client($client); } diff --git a/t/sdk.t b/t/sdk.t index 3def54b..5586023 100644 --- a/t/sdk.t +++ b/t/sdk.t @@ -14,6 +14,7 @@ use Sentry::Hub; use Sentry::Logger 'logger'; use Sentry::SDK; use Sentry::Severity; +use Test::Exception; use Test::Snapshot; use Test::Spec; use UUID::Tiny 'is_UUID_string'; @@ -66,6 +67,14 @@ describe 'Sentry::SDK' => sub { is_deeply_snapshot($hub->client->get_options, 'client options (env)'); }; + + it 'disables SDK if DSN is empty' => sub { + Sentry::SDK->init({ dsn => '' }); + + is($hub->client, undef, 'client is undefined'); + + lives_ok { Sentry::SDK->capture_message('foo') }; + }; }; describe 'message sending' => sub { @@ -139,7 +148,7 @@ describe 'Sentry::SDK' => sub { }; it 'start_transaction()' => sub { - Sentry::SDK->init({ traces_sample_rate => 1, }); + Sentry::SDK->init({ dsn => 'abc', traces_sample_rate => 1, }); my $tx = Sentry::SDK->start_transaction(