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