File Coverage

blib/lib/Mojolicious/Plugin/SentrySDK.pm
Criterion Covered Total %
statement 43 44 97.7
branch 2 4 50.0
condition 3 4 75.0
subroutine 6 6 100.0
pod 1 1 100.0
total 55 59 93.2


line stmt bran cond sub pod time code
1             use Mojo::Base 'Mojolicious::Plugin', -signatures;
2 1     1   202993  
  1         9  
  1         4  
3             use Sentry::SDK;
4 1     1   1115 use Try::Tiny;
  1         3  
  1         8  
5 1     1   31  
  1         1  
  1         457  
6             $app->hook(
7 3     3 1 38640 before_server_start => sub ($server, $app) {
  3         7  
  3         3  
  3         6  
  3         3  
8 6         7 Sentry::SDK->init($conf);
9 6     6   6 }
  6         6851  
  6         7  
10 6         28 );
11              
12 3         15 $app->hook(
13             around_action => sub ($next, $c, $action, $last) {
14 3         6 return $next->() unless $last;
15 3     3   6  
  3         29731  
  3         4  
  3         4  
  3         4  
16 3 50       21 my $req = $c->req;
17              
18 3         15 Sentry::Hub->get_current_hub()->with_scope(sub ($scope) {
19             my %cookies = map { ($_->name, $_->value) } ($req->cookies // [])->@*;
20 3         12 my $transaction = Sentry::SDK->start_transaction(
21 3   50     14 {
  0         0  
22 3   100     154 name => $c->match->endpoint->pattern->unparsed || '/',
23             op => 'http.server',
24             request => {
25             url => $req->url->to_abs->to_string,
26             cookies => \%cookies,
27             method => $req->method,
28             query_string => $req->url->query->to_hash,
29             headers => $req->headers->to_hash,
30             env => \%ENV,
31             },
32             },
33             );
34              
35             Sentry::SDK->configure_scope(sub ($scope) {
36             $scope->set_span($transaction);
37 3         4 });
38 3         12  
39 3         31 try {
40             $next->();
41             } catch {
42 3         172 Sentry::SDK->capture_exception($_);
43             $c->reply->exception($_)
44 1         481 } finally {
45 1         9 my $status = $c->res->code;
46             $transaction->set_http_status($status) if $status;
47 3         48343 $transaction->finish();
48 3 50       44 };
49 3         24 });
50 3         21 }
51 3         35 );
52             }
53 3         38  
54             1;
55              
56             =encoding utf8
57              
58             =head1 NAME
59              
60             Mojolicious::Plugin::SentrySDK - Sentry plugin for Mojolicious
61              
62             =head1 SYNOPSIS
63              
64             =head1 DESCRIPTION
65              
66             =head1 OPTIONS
67              
68             =head2 register
69              
70             my $config = $plugin->register(Mojolicious->new);
71             my $config = $plugin->register(Mojolicious->new, \%options);
72              
73             Register Sentry in L<Mojolicious> application.
74              
75             =head1 SEE ALSO
76              
77             L<Sentry::SDK>.
78              
79             =cut