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