File Coverage

blib/lib/Mojolicious/Plugin/SentrySDK.pm
Criterion Covered Total %
statement 49 50 98.0
branch 2 4 50.0
condition 4 6 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 63 68 92.6


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