File Coverage

blib/lib/CGI/Application/Plugin/Sentry.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::Sentry;
2 1     1   155305 use Mojo::Base -base, -signatures;
  1         3  
  1         8  
3              
4 1     1   365 use CGI::Application;
  1         3  
  1         31  
5 1     1   4 use HTTP::Status ':constants';
  1         2  
  1         545  
6 1     1   8 use Mojo::Util 'dumper';
  1         2  
  1         61  
7 1     1   625 use Sentry::SDK;
  1         5  
  1         11  
8 1     1   803 use Sys::Hostname 'hostname';
  1         1607  
  1         954  
9              
10             CGI::Application->add_callback(
11             init => sub ($c, @args) {
12             my $options = $c->param('sentry_options');
13             Sentry::SDK->init($options);
14              
15             Sentry::Hub->get_current_hub()->reset();
16              
17             Sentry::SDK->configure_scope(sub ($scope) {
18             $scope->set_tags({ runtime => "Perl $]", server_name => hostname });
19             });
20             }
21             );
22              
23             CGI::Application->add_callback(
24             error => sub ($c, $error) {
25             Sentry::SDK->capture_exception($error);
26             }
27             );
28              
29             CGI::Application->add_callback(
30             prerun => sub ($c, $rm) {
31             my $request_uri = $c->query->self_url;
32              
33             Sentry::SDK->configure_scope(sub ($scope) {
34             $scope->set_tags({ runtime => "Perl $]", runmode => $rm, });
35             });
36              
37             Sentry::Hub->get_current_hub()->push_scope();
38              
39             Sentry::SDK->configure_scope(sub ($scope) {
40             $scope->add_breadcrumb({ message => 'prerun' });
41             });
42              
43             my $method = $c->query->request_method;
44             my $transaction_name = "$method $rm";
45             my $transaction = Sentry::SDK->start_transaction(
46             {
47             name => $transaction_name,
48             op => 'http.server',
49             request => {
50             url => $request_uri,
51             method => $method,
52             query => { $c->query->Vars },
53             headers => { map { $_ => $c->query->http($_) } $c->query->http },
54             env => \%ENV,
55             }
56             },
57             );
58              
59             $c->param('__sentry__transaction', $transaction);
60              
61             Sentry::SDK->configure_scope(sub ($scope) {
62             $scope->set_transaction_name($transaction_name);
63             $scope->set_span($transaction);
64             });
65             }
66             );
67              
68             CGI::Application->add_callback(
69             postrun => sub ($c, $body_ref) {
70             Sentry::SDK->configure_scope(sub ($scope) {
71             $scope->add_breadcrumb({ message => 'postrun' });
72             });
73              
74             my $transaction = $c->param('__sentry__transaction');
75             # Does anyone know how to get the HTTP respnose status code using $c?
76             $transaction->set_http_status(HTTP_OK);
77             $transaction->finish();
78              
79             Sentry::Hub->get_current_hub()->pop_scope();
80              
81             $c->param('__sentry__transaction', undef);
82             }
83             );
84              
85             1;
86              
87             =encoding utf8
88              
89             =head1 NAME
90              
91             CGI::Application::Plugin::Sentry - Sentry plugin for CGI::Application
92              
93             =head1 SYNOPSIS
94              
95             =head1 DESCRIPTION
96              
97             =head1 OPTIONS
98              
99             =head1 METHODS
100              
101             =head1 SEE ALSO
102              
103             L<Sentry::SDK>.
104              
105             =cut