File Coverage

blib/lib/Sentry/SDK.pm
Criterion Covered Total %
statement 79 80 98.7
branch 10 14 71.4
condition 17 19 89.4
subroutine 15 15 100.0
pod 7 7 100.0
total 128 135 94.8


line stmt bran cond sub pod time code
1             package Sentry::SDK;
2 3     3   751 use Mojo::Base -base, -signatures;
  3         7  
  3         26  
3              
4 3     3   1171 use version 0.77;
  3         119  
  3         28  
5 3     3   294 use Mojo::Util 'dumper';
  3         25  
  3         176  
6 3     3   1937 use Sentry::Client;
  3         12  
  3         25  
7 3     3   216 use Sentry::Hub;
  3         7  
  3         15  
8 3     3   114 use Sentry::Logger 'logger';
  3         6  
  3         2656  
9              
10             our $VERSION = version->declare('1.3.9');
11              
12 25     25   45 sub _call_on_hub ($method, @args) {
  25         37  
  25         42  
  25         33  
13 25         89 my $hub = Sentry::Hub->get_current_hub();
14              
15 25 50       126 if (my $cb = $hub->can($method)) {
16 25         76 return $cb->($hub, @args);
17             }
18              
19             die
20 0         0 "No hub defined or $method was not found on the hub, please open a bug report.";
21             }
22              
23 22     22   29 sub _init_and_bind ($options) {
  22         55  
  22         26  
24 22         96 my $hub = Sentry::Hub->get_current_hub();
25             my $client
26 22 100       121 = $options->{dsn} ? Sentry::Client->new(_options => $options) : undef;
27 22         175 $hub->bind_client($client);
28             }
29              
30 22     22 1 1194 sub init ($package, $options = {}) {
  22         36  
  22         35  
  22         28  
31 22   100     164 $options->{default_integrations} //= [];
32 22   66     93 $options->{dsn} //= $ENV{SENTRY_DSN};
33 22   100     61 $options->{traces_sample_rate} //= $ENV{SENTRY_TRACES_SAMPLE_RATE};
34 22   100     107 $options->{release} //= $ENV{SENTRY_RELEASE};
35 22   100     83 $options->{environment} //= $ENV{SENTRY_ENVIRONMENT};
36 22   100     83 $options->{_metadata} //= {};
37             $options->{_metadata}{sdk}
38 22         137 = { name => 'sentry.perl', packages => [], version => $VERSION };
39              
40 22 100 66     113 logger->active_contexts(['.*']) if $options->{debug} // $ENV{SENTRY_DEBUG};
41              
42 22         84 _init_and_bind($options);
43             }
44              
45 10     10 1 913 sub capture_message ($self, $message, $capture_context = undef) {
  10         17  
  10         20  
  10         18  
  10         18  
46 10 50       25 my $level = ref($capture_context) ? undef : $capture_context;
47              
48 10 50       151 _call_on_hub('capture_message', $message, $level,
49             { capture_context => ref($capture_context) ? $capture_context : undef, });
50             }
51              
52 1     1 1 6 sub capture_event ($package, $event, $capture_context = undef) {
  1         2  
  1         1  
  1         2  
  1         1  
53 1 50       7 _call_on_hub('capture_event', $event,
54             { capture_context => ref($capture_context) ? $capture_context : undef, });
55             }
56              
57 3     3 1 10 sub capture_exception ($package, $exception, $capture_context = undef) {
  3         6  
  3         5  
  3         6  
  3         4  
58 3 100       16 _call_on_hub('capture_exception', $exception,
59             { capture_context => ref($capture_context) ? $capture_context : undef, });
60             }
61              
62 37     37 1 15537 sub configure_scope ($package, $cb) {
  37         62  
  37         48  
  37         58  
63 37         130 Sentry::Hub->get_current_hub()->configure_scope($cb);
64             }
65              
66 3     3 1 251 sub add_breadcrumb ($package, $crumb) {
  3         6  
  3         4  
  3         4  
67 3         11 Sentry::Hub->get_current_hub()->add_breadcrumb($crumb);
68             }
69              
70 11     11 1 2787 sub start_transaction ($package, $context, $custom_sampling_context = undef) {
  11         20  
  11         17  
  11         20  
  11         17  
71 11         32 return _call_on_hub('start_transaction', $context, $custom_sampling_context);
72             }
73              
74             1;
75              
76             __END__
77              
78             =encoding utf-8
79              
80             =head1 NAME
81              
82             Sentry::SDK - sentry.io integration
83              
84             =head1 SYNOPSIS
85              
86             use Sentry::SDK;
87              
88             Sentry::SDK->init({
89             dsn => "https://examplePublicKey@o0.ingest.sentry.io/0",
90              
91             # Adjust this value in production
92             traces_sample_rate => 1.0,
93             });
94              
95             =head1 DESCRIPTION
96              
97             =head1 FUNCTIONS
98              
99             =head2 init
100              
101             Sentry::SDK->init(\%options);
102              
103             Initializes the Sentry SDK in your app. The following options are provided:
104              
105             =head3 dsn
106              
107             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.
108              
109             =head3 release
110              
111             Sets the release. Defaults to the C<SENTRY_RELEASE> environment variable.
112              
113             =head3 environment
114              
115             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).
116              
117             By default the SDK will try to read this value from the C<SENTRY_ENVIRONMENT> environment variable.
118              
119             =head3 traces_sample_rate
120              
121             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.
122              
123             =head3 before_send
124              
125             Sentry::SDK->init({
126             before_send => sub ($event, $hint) {
127              
128             # discard event we don't care about
129             if (ref($hint->{original_exception}) eq 'My::Ignorable::Exception') {
130             return undef;
131             }
132              
133             # add a custom tag otherwise
134             $event->tags->{foo} = 'bar';
135              
136             return $event;
137             };
138             });
139              
140             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.
141              
142             =head3 integrations
143              
144             Sentry::SDK->init({
145             integrations => [My::Integration->new],
146             });
147              
148             Enables your custom integration. Optional.
149              
150             =head3 default_integrations
151              
152             This can be used to disable integrations that are added by default. When set to a falsy value, no default integrations are added.
153              
154             =head3 debug
155              
156             Enables debug printing.
157              
158             =head2 add_breadcrumb
159              
160             Sentry::SDK->add_breadcrumb({
161             category => "auth",
162             message => "Authenticated user " . user->{email},
163             level => Sentry::Severity->Info,
164             });
165              
166             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.
167              
168             =head2 capture_exception
169              
170             eval {
171             $app->run();
172             };
173             if ($@) {
174             Sentry::SDK->capture_exception($@);
175             }
176              
177             You can pass an error object to capture_exception() to get it captured as event. It's possible to throw strings as errors.
178              
179             =head2 capture_message
180              
181             Sentry::SDK->capture_message("Something went wrong");
182              
183             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.
184              
185             =head2 capture_event
186              
187             Sentry::SDK->capture_event(\%data);
188              
189             Captures a manually created event and sends it to Sentry.
190              
191             =head2 configure_scope
192              
193             Sentry::SDK->configure_scope(sub ($scope) {
194             $scope->set_tag(foo => "bar");
195             $scope->set_user({id => 1, email => "john.doe@example.com"});
196             });
197              
198             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.
199              
200             =head2 start_transaction
201              
202             my $transaction = Sentry::SDK->start_transaction({
203             name => 'MyScript',
204             op => 'http.server',
205             });
206              
207             Sentry::SDK->configure_scope(sub ($scope) {
208             $scope->set_span($transaction);
209             });
210              
211             # ...
212              
213             $transaction->set_http_status(200);
214             $transaction->finish();
215              
216             Is needed for recording tracing information. Transactions are usually handled by the respective framework integration. See L<Sentry::Tracing::Transaction>.
217              
218             =head1 AUTHOR
219              
220             Philipp Busse E<lt>pmb@heise.deE<gt>
221              
222             =head1 COPYRIGHT
223              
224             Copyright 2021- Philipp Busse
225              
226             =head1 LICENSE
227              
228             This library is free software; you can redistribute it and/or modify
229             it under the same terms as Perl itself.
230              
231             =head1 SEE ALSO
232              
233             =cut