blib/lib/Mojolicious/Plugin/GetSentry.pm

filter: Statementstmt 21.3% 26 / 122 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 13.1% 5 / 38 Podpod 100% 13 / 13 totaltotal 21.2% 44 / 207 CC 19 SCAR 21.5
CC
19
Cov
17%
__ANON__
30.0
register
20.0
__ANON__
20.0
CRAP 8.5
↑ Summary
1 package Mojolicious::Plugin::GetSentry;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '1.1.9';
5  
6 2 ▶use Data::Dump 'dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Devel::StackTrace::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Sentry::Raven;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 has [qw(
12 sentry_dsn timeout
13 )];
14  
15 0 ▶has 'log_levels' => sub { ['error', 'fatal'] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 0 ▶has 'processors' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 0 ▶has 'release' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 my $release = eval { `git log --pretty="%h" -n1 HEAD` };
19 0 chomp($release);
20  
21 0 return $release;
22 2 };
23  
24 0 ▶has 'raven' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
25 0 my $self = shift;
26  
27 0 foreach my $processor (@{ $self->processors }) {
28 0 eval "require $processor; $processor->import;";
29  
30 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
31 }
32  
33 0 return Sentry::Raven->new(
34 sentry_dsn => $self->sentry_dsn,
35 timeout => $self->timeout,
36 processors => $self->processors,
37 release => $self->release,
38 );
39 2 };
40  
41 0 ▶has 'handlers' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 my $self = shift;
43  
44 return {
45 0 ▶ capture_request => sub { $self->capture_request(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 ▶ capture_message => sub { $self->capture_message(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 ▶ stacktrace_context => sub { $self->stacktrace_context(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ exception_context => sub { $self->exception_context(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 ▶ user_context => sub { $self->user_context(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 ▶ request_context => sub { $self->request_context(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 ▶ tags_context => sub { $self->tags_context(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 ▶ ignore => sub { $self->ignore(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 ▶ on_error => sub { $self->on_error(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 };
55 2 };
56  
57 0 ▶has 'custom_handlers' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
58 0 ▶has 'pending' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
59 0 ▶has 'skip' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
60  
61 =head2 register
62  
63 =cut
64  
65 0 ▶sub register {
Subroutine register: not called CC=4 SCAR=30.0
Pod documented
66 0 my ($self, $app, $config) = (@_);
67
68 0 my $handlers = {};
69  
70 0 foreach my $name (keys(%{ $self->handlers })) {
71 0 $handlers->{ $name } = delete($config->{ $name });
72 }
73  
74 # Set custom handlers
75 0 $self->custom_handlers($handlers);
76  
77 0 ▶ $config ||= {};
Condition0%0/2
$config ||= {}
l!l
00
MC/DC0%0/1
$config ||= {}
$config
Truth table0%0/2
A
A= $config
Aresult
11
00
78 0 $self->$_($config->{ $_ }) for keys %$config;
79
80 0 $self->hook_after_dispatch($app);
81 0 $self->hook_on_message($app);
82 }
83  
84 =head2 hook_after_dispatch
85  
86 =cut
87  
88 0 ▶sub hook_after_dispatch {
Subroutine hook_after_dispatch: not called CC=1 SCAR=6.9
Pod documented
89 0 my $self = shift;
90 0 my $app = shift;
91  
92 0 ▶ $app->hook(after_dispatch => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
93 0 my $controller = shift;
94  
95 0 ▶ if (my $exception = $controller->stash('exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exception = $controller->stash("exception")) 0 0 0
96 # Mark this exception as handled. We don't delete it from $pending
97 # because if the same exception is logged several times within a
98 # 2-second period, we want the logger to ignore it.
99 0 ▶ $self->pending->{ $exception } = 0 if defined $self->pending->{ $exception };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pending->{$exception} 0 0 0
100
101 # Check if the exception should be ignored
102 0 ▶ if (!$self->handle('ignore', $exception)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->handle("ignore", $exception)) 0 0 0
103 0 $self->handle('capture_request', $exception, $controller);
104 }
105 }
106 0 });
107 }
108  
109 =head2 hook_on_message
110  
111 =cut
112  
113 0 ▶sub hook_on_message {
Subroutine hook_on_message: not called CC=1 SCAR=6.9
Pod documented
114 0 my $self = shift;
115 0 my $app = shift;
116  
117 0 ▶ $app->log->on(message => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
118 0 my ($log, $level, $exception) = @_;
119  
120 0 ▶ if( grep { $level eq $_ } @{ $self->log_levels } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$level eq $_;} @{$self->log_levels;}) 0 0 0
121 0 ▶ $exception = Mojo::Exception->new($exception) unless ref $exception;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $exception 0 0 0
122  
123 # This exception is already pending
124 0 ▶ return if defined $self->pending->{ $exception };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pending->{$exception} 0 0 0
125
126 0 $self->pending->{ $exception } = 1;
127  
128 # Check if the exception should be ignored
129 0 ▶ if (!$self->handle('ignore', $exception)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->handle("ignore", $exception)) 0 0 0
130 # Wait 2 seconds before we handle it; if the exception happened in
131 # a request we want the after_dispatch-hook to handle it instead.
132 0 ▶ Mojo::IOLoop->timer(2 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 $self->handle('capture_message', $exception);
134 0 });
135 }
136 }
137 0 });
138 }
139  
140 =head2 handle
141  
142 =cut
143  
144 0 ▶sub handle {
Subroutine handle: not called CC=2 SCAR=17.9
Pod documented
145 0 my ($self, $method) = (shift, shift);
146  
147 return $self->custom_handlers->{ $method }->($self, @_)
148 0 ▶ if (defined($self->custom_handlers->{ $method }));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->custom_handlers->{$method} 0 0 0
149
150 0 return $self->handlers->{ $method }->(@_);
151 }
152  
153 =head2 capture_request
154  
155 =cut
156  
157 0 ▶sub capture_request {
Subroutine capture_request: not called CC=2 SCAR=17.9
Pod documented
158 0 my ($self, $exception, $controller) = @_;
159  
160 0 $self->handle('stacktrace_context', $exception);
161 0 $self->handle('exception_context', $exception);
162 0 $self->handle('user_context', $controller);
163 0 $self->handle('tags_context', $controller);
164
165 0 my $request_context = $self->handle('request_context', $controller);
166  
167 0 my $event_id = $self->raven->capture_request($controller->url_for->to_abs, %$request_context, $self->raven->get_context);
168  
169 0 ▶ if (!defined($event_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_id) 0 0 0
170 0 $self->handle('on_error', $exception->message, $self->raven->get_context);
171 }
172  
173 0 return $event_id;
174 }
175  
176 =head2 capture_message
177  
178 =cut
179  
180 0 ▶sub capture_message {
Subroutine capture_message: not called CC=2 SCAR=17.9
Pod documented
181 0 my ($self, $exception) = @_;
182  
183 0 $self->handle('exception_context', $exception);
184  
185 0 my $event_id = $self->raven->capture_message($exception->message, $self->raven->get_context);
186  
187 0 ▶ if (!defined($event_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_id) 0 0 0
188 0 $self->handle('on_error', $exception->message, $self->raven->get_context);
189 }
190  
191 0 return $event_id;
192 }
193  
194 =head2 stacktrace_context
195  
196 $app->sentry->stacktrace_context($exception)
197  
198 Build the stacktrace context from current exception.
199 See also L<Sentry::Raven->stacktrace_context|https://metacpan.org/pod/Sentry::Raven#Sentry::Raven-%3Estacktrace_context(-$frames-)>
200  
201 =cut
202  
203 0 ▶sub stacktrace_context {
Subroutine stacktrace_context: not called CC=1 SCAR=6.9
Pod documented
204 0 my ($self, $exception) = @_;
205  
206 0 my $stacktrace = Devel::StackTrace::Extract::extract_stack_trace($exception);
207  
208 0 $self->raven->add_context(
209 $self->raven->stacktrace_context($self->raven->_get_frames_from_devel_stacktrace($stacktrace))
210 );
211 }
212  
213 =head2 exception_context
214  
215 $app->sentry->exception_context($exception)
216  
217 Build the exception context from current exception.
218 See also L<Sentry::Raven->exception_context|https://metacpan.org/pod/Sentry::Raven#Sentry::Raven-%3Eexception_context(-$value,-%25exception_context-)>
219  
220 =cut
221  
222 0 ▶sub exception_context {
Subroutine exception_context: not called CC=1 SCAR=6.9
Pod documented
223 0 my ($self, $exception) = @_;
224  
225 0 $self->raven->add_context(
226 $self->raven->exception_context($exception->message, type => ref($exception))
227 );
228 }
229  
230 =head2 user_context
231  
232 $app->sentry->user_context($controller)
233  
234 Build the user context from current controller.
235 See also L<Sentry::Raven->user_context|https://metacpan.org/pod/Sentry::Raven#Sentry::Raven-%3Euser_context(-%25user_context-)>
236  
237 =cut
238  
239 0 ▶sub user_context {
Subroutine user_context: not called CC=3 SCAR=24.8
Pod documented
240 0 my ($self, $controller) = @_;
241  
242 0 ▶ if (defined($controller->user)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controller->user) 0 0 0
243 0 ▶ $self->raven->add_context(
Condition0%0/3
$controller->tx && $controller->tx->remote_address
!ll&&!rl&&r
000
MC/DC0%0/2
$controller->tx && $controller->tx->remote_address
$controller->tx $controller->tx->remote_address
Truth table0%0/3
A && B
A= $controller->txB= $controller->tx->remote_address
ABresult
0X0
100
111
244 $self->raven->user_context(
245 id => $controller->user->id,
246 ip_address => $controller->tx && $controller->tx->remote_address,
247 )
248 );
249 }
250 }
251  
252 =head2 request_context
253  
254 $app->sentry->request_context($controller)
255  
256 Build the request context from current controller.
257 See also L<Sentry::Raven->request_context|https://metacpan.org/pod/Sentry::Raven#Sentry::Raven-%3Erequest_context(-$url,-%25request_context-)>
258  
259 =cut
260  
261 0 ▶sub request_context {
Subroutine request_context: not called CC=2 SCAR=17.9
Pod documented
262 0 my ($self, $controller) = @_;
263  
264 0 ▶ if (defined($controller->req)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controller->req) 0 0 0
265 0 my $request_context = {
266 method => $controller->req->method,
267 headers => $controller->req->headers->to_hash,
268 };
269  
270 0 $self->raven->add_context(
271 $self->raven->request_context($controller->url_for->to_abs, %$request_context)
272 );
273  
274 0 return $request_context;
275 }
276  
277 0 return {};
278 }
279  
280 =head2 tags_context
281
282 $app->sentry->tags_context($controller)
283  
284 Add some tags to the context.
285 See also L<Sentry::Raven->3Emerge_tags|https://metacpan.org/pod/Sentry::Raven#$raven-%3Emerge_tags(-%25tags-)>
286  
287 =cut
288  
289 0 ▶sub tags_context {
Subroutine tags_context: not called CC=1 SCAR=6.9
Pod documented
290 0 my ($self, $c) = @_;
291  
292 0 $self->raven->merge_tags(
293 getsentry => $VERSION,
294 );
295 }
296  
297 =head2 ignore
298
299 $app->sentry->ignore($exception)
300  
301 Check if the exception should be ignored.
302  
303 =cut
304  
305 0 ▶sub ignore {
Subroutine ignore: not called CC=1 SCAR=6.9
Pod documented
306 0 my ($self, $exception) = @_;
307  
308 0 return 0;
309 }
310  
311 =head2 on_error
312
313 $app->sentry->on_error($message, %context)
314  
315 Handle reporting to Sentry error.
316  
317 =cut
318  
319 0 ▶sub on_error {
Subroutine on_error: not called CC=1 SCAR=6.9
Pod documented
320 0 my ($self, $message) = (shift, shift);
321  
322 0 die "failed to submit event to sentry service:\n" . dump($self->raven->_construct_message_event($message, @_));
323 }
324  
325 2 1;
326  
327 __END__
↑ Summary