File Coverage

blib/lib/Sentry/SDK.pm
Criterion Covered Total %
statement 78 79 98.7
branch 5 8 62.5
condition 17 19 89.4
subroutine 15 15 100.0
pod 7 7 100.0
total 122 128 95.3


line stmt bran cond sub pod time code
1             use Mojo::Base -base, -signatures;
2 3     3   459  
  3         5  
  3         17  
3             use version 0.77;
4 3     3   561 use Mojo::Util 'dumper';
  3         52  
  3         18  
5 3     3   208 use Sentry::Client;
  3         6  
  3         138  
6 3     3   1128 use Sentry::Hub;
  3         8  
  3         26  
7 3     3   101 use Sentry::Logger 'logger';
  3         8  
  3         9  
8 3     3   68  
  3         3  
  3         1860  
9             our $VERSION = version->declare('v1.2.1');
10              
11             my $hub = Sentry::Hub->get_current_hub();
12 20     20   33  
  20         28  
  20         34  
  20         23  
13 20         47 if (my $cb = $hub->can($method)) {
14             return $cb->($hub, @args);
15 20 50       95 }
16 20         69  
17             die
18             "No hub defined or $method was not found on the hub, please open a bug report.";
19             }
20 0         0  
21             my $hub = Sentry::Hub->get_current_hub();
22             my $client = Sentry::Client->new(_options => $options);
23 17     17   24 $hub->bind_client($client);
  17         23  
  17         19  
24 17         64 }
25 17         76  
26 17         123 $options->{default_integrations} //= [];
27             $options->{dsn} //= $ENV{SENTRY_DSN};
28             $options->{traces_sample_rate} //= $ENV{SENTRY_TRACES_SAMPLE_RATE};
29 17     17 1 854 $options->{release} //= $ENV{SENTRY_RELEASE};
  17         25  
  17         24  
  17         25  
30 17   100     82 $options->{environment} //= $ENV{SENTRY_ENVIRONMENT};
31 17   100     45 $options->{_metadata} //= {};
32 17   66     49 $options->{_metadata}{sdk}
33 17   100     65 = { name => 'sentry.perl', packages => [], version => $VERSION };
34 17   100     64  
35 17   100     56 logger->active_contexts(['.*']) if $options->{debug} // $ENV{SENTRY_DEBUG};
36              
37 17         69 _init_and_bind($options);
38             }
39 17 100 66     98  
40             my $level = ref($capture_context) ? undef : $capture_context;
41 17         52  
42             _call_on_hub(
43             'capture_message',
44 8     8 1 364 $message, $level,
  8         13  
  8         12  
  8         12  
  8         10  
45 8 50       32 {
46             originalException => $message,
47 8 50       41 capture_context => ref($capture_context) ? $capture_context : undef,
48             }
49             );
50             }
51              
52             _call_on_hub('capture_event', $event);
53             }
54              
55             _call_on_hub('capture_exception', $exception, $capture_context);
56             }
57 1     1 1 284  
  1         2  
  1         2  
  1         2  
58 1         4 Sentry::Hub->get_current_hub()->configure_scope($cb);
59             }
60              
61 2     2 1 306 Sentry::Hub->get_current_hub()->add_breadcrumb($crumb);
  2         4  
  2         3  
  2         3  
  2         4  
62 2         4 }
63              
64             return _call_on_hub('start_transaction', $context, $custom_sampling_context);
65 34     34 1 9706 }
  34         44  
  34         45  
  34         42  
66 34         86  
67             1;
68              
69 3     3 1 188  
  3         6  
  3         6  
  3         3  
70 3         22 =encoding utf-8
71              
72             =head1 NAME
73 9     9 1 1778  
  9         17  
  9         11  
  9         17  
  9         11  
74 9         51 Sentry::SDK - sentry.io integration
75              
76             =head1 SYNOPSIS
77              
78             use Sentry::SDK;
79              
80             Sentry::SDK->init({
81             dsn => "https://examplePublicKey@o0.ingest.sentry.io/0",
82              
83             # Adjust this value in production
84             traces_sample_rate => 1.0,
85             });
86              
87             =head1 DESCRIPTION
88              
89             =head1 FUNCTIONS
90              
91             =head2 init
92              
93             Sentry::SDK->init(\%options);
94              
95             Initializes the Sentry SDK in your app. The following options are provided:
96              
97             =head3 dsn
98              
99             The DSN tells the SDK where to send the events. If this value is not provided, the SDK will try to read it from the C<SENTRY_DSN> environment variable. If that variable also does not exist, the SDK will just not send any events.
100              
101             =head3 release
102              
103             Sets the release. Defaults to the C<SENTRY_RELEASE> environment variable.
104              
105             =head3 environment
106              
107             Sets the environment. This string is freeform and not set by default. A release can be associated with more than one environment to separate them in the UI (think staging vs prod or similar).
108              
109             By default the SDK will try to read this value from the C<SENTRY_ENVIRONMENT> environment variable.
110              
111             =head3 traces_sample_rate
112              
113             A number between 0 and 1, controlling the percentage chance a given transaction will be sent to Sentry. (0 represents 0% while 1 represents 100%.) Applies equally to all transactions created in the app. This must be defined to enable tracing.
114              
115             =head3 before_send
116              
117             Sentry::SDK->init({
118             before_send => sub ($event) {
119             $event->tags->{foo} = 'bar';
120              
121             # discard event
122             if (rand() < 0.5) {
123             return undef;
124             }
125              
126             return $event;
127             };
128             });
129              
130             C<beforeSend> is called immediately before the event is sent to the server, so it’s the final place where you can edit its data. It receives the event object as a parameter, so you can use that to modify the event’s data or drop it completely (by returning C<undef>) based on custom logic and the data available on the event.
131              
132             =head3 integrations
133              
134             Sentry::SDK->init({
135             integrations => [My::Integration->new],
136             });
137              
138             Enables your custom integration. Optional.
139              
140             =head3 default_integrations
141              
142             This can be used to disable integrations that are added by default. When set to a falsy value, no default integrations are added.
143              
144             =head3 debug
145              
146             Enables debug printing.
147              
148             =head2 add_breadcrumb
149              
150             Sentry::SDK->add_breadcrumb({
151             category => "auth",
152             message => "Authenticated user " . user->{email},
153             level => Sentry::Severity->Info,
154             });
155              
156             You can manually add breadcrumbs whenever something interesting happens. For example, you might manually record a breadcrumb if the user authenticates or another state change happens.
157              
158             =head2 capture_exception
159              
160             eval {
161             $app->run();
162             };
163             if ($@) {
164             Sentry::SDK->capture_exception($@);
165             }
166              
167             You can pass an error object to capture_exception() to get it captured as event. It's possible to throw strings as errors.
168              
169             =head2 capture_message
170              
171             Sentry::SDK->capture_message("Something went wrong");
172              
173             Another common operation is to capture a bare message. A message is textual information that should be sent to Sentry. Typically messages are not emitted, but they can be useful for some teams.
174              
175             =head2 capture_event
176              
177             Sentry::SDK->capture_event(\%data);
178              
179             Captures a manually created event and sends it to Sentry.
180              
181             =head2 configure_scope
182              
183             Sentry::SDK->configure_scope(sub ($scope) {
184             $scope->set_tag(foo => "bar");
185             $scope->set_user({id => 1, email => "john.doe@example.com"});
186             });
187              
188             When an event is captured and sent to Sentry, event data with extra information will be merged from the current scope. The C<configure_scope> function can be used to reconfigure the current scope. This for instance can be used to add custom tags or to inform sentry about the currently authenticated user. See L<Sentry::Hub::Scope> for further information.
189              
190             =head2 start_transaction
191              
192             my $transaction = Sentry::SDK->start_transaction({
193             name => 'MyScript',
194             op => 'http.server',
195             });
196              
197             Sentry::SDK->configure_scope(sub ($scope) {
198             $scope->set_span($transaction);
199             });
200              
201             # ...
202              
203             $transaction->set_http_status(200);
204             $transaction->finish();
205              
206             Is needed for recording tracing information. Transactions are usually handled by the respective framework integration. See L<Sentry::Tracing::Transaction>.
207              
208             =head1 AUTHOR
209              
210             Philipp Busse E<lt>pmb@heise.deE<gt>
211              
212             =head1 COPYRIGHT
213              
214             Copyright 2021- Philipp Busse
215              
216             =head1 LICENSE
217              
218             This library is free software; you can redistribute it and/or modify
219             it under the same terms as Perl itself.
220              
221             =head1 SEE ALSO
222              
223             =cut