Agent/Frameworks/Mojolicious.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 122 CC 14 SCAR 51.0
CC
14
Cov
0%
register
210.0
BEGIN
2.0
BEGIN
2.0
CRAP 163.8
1 package PerlGuard::Agent::Frameworks::Mojolicious;
2 #use Moo;
3 use PerlGuard::Agent;
4 use Mojo::Base 'Mojolicious::Plugin';
5 use Mojo::IOLoop;
6  
7  
8 BEGIN {
9 $PerlGuard::Agent::Frameworks::Mojolicious::VERSION = '1.00';
10 }
11  
12 sub register {
13 my ($self, $app, $args) = @_;
14 $args ||= {};
15  
16 my $agent = PerlGuard::Agent->new($args);
17  
18 $app->helper(perlguard_agent => sub {
19 return $agent;
20 });
21  
22  
23 $app->hook(after_build_tx => sub {
24 my $tx = shift;
25
26 unless($tx->{'PerlGuard::Profile'}) {
27 my $profile = $agent->create_new_profile();
28  
29 $tx->{'PerlGuard::Profile'} //= $profile;
30  
31 $profile->start_recording;
32 }
33 else {
34 warn "I think I already have a profile on this TX even though its just been built" if $ENV{'PERLGUARD_AGENT_DEBUG'}
35 }
36 });
37  
38  
39 $app->hook(after_dispatch => sub {
40 my $c = shift;
41  
42 return if ($c->stash->{'mojo.static'});
43  
44 my $profile = $c->tx->{'PerlGuard::Profile'};
45 $profile->finish_recording();
46 $profile->http_code( $c->tx->res->code );
47 $c->tx->{'PerlGuard::Profile'} = undef;
48  
49 #This does not do what I think it does
50 if(Mojo::IOLoop->is_running()) {
51 Mojo::IOLoop->timer(1 => sub {
52 my $loop = shift;
53
54 $profile->save;
55 });
56 }
57 else {
58 $profile->save;
59 }
60 });
61  
62  
63 $app->hook(before_routes => sub {
64 my $c = shift;
65  
66 my $stash = $c->stash;
67 unless ($stash->{'mojo.static'}) {
68  
69 unless($c->tx->{'PerlGuard::Profile'}) {
70  
71 warn "In before_routes we didn't have a profile on the transaction already so we had to make it";
72 my $profile = $agent->create_new_profile();
73 $c->tx->{'PerlGuard::Profile'} //= $profile;
74  
75 $profile->start_recording;
76 }
77 else {
78 $c->tx->{'PerlGuard::Profile'}->http_code( $c->tx->res->code );
79 $c->tx->{'PerlGuard::Profile'}->url( $c->tx->req->url );
80 #$c->stash('PerlGuard::Profile', $c->tx->{'PerlGuard::Profile'});
81 }
82 }
83  
84 });
85  
86 $app->hook(around_dispatch => sub {
87 my ($next, $c) = @_;
88  
89 #$c->stash->{'PerlGuard::Profile'} = $c->tx->{'PerlGuard::Profile'};
90  
91 do {
92 if($c->tx->{'PerlGuard::Profile'}) {
93 local $PerlGuard::Agent::CURRENT_PROFILE_UUID = $c->tx->{'PerlGuard::Profile'}->uuid() unless $c->stash->{'mojo.static'};
94 $next->();
95 }
96 else {
97 warn "Perlguard profile was not defined at this point";
98 $next->();
99 }
100 };
101  
102 });
103  
104 $app->hook(around_action => sub {
105 my ($next, $c, $action, $last) = @_;
106  
107 unless($c->stash->{'mojo.static'}) {
108 my $profile = $c->tx->{'PerlGuard::Profile'};
109  
110 unless($profile) {
111 #warn "PerlGuard profile was not defined when we expected it to be";
112 }
113 else {
114 $profile->controller( ref($c) );
115 $profile->controller_action( $c->stash->{action} );
116 $profile->http_code( $c->tx->res->code );
117  
118 if( $c->req ) {
119  
120 $profile->url( $c->req->url );
121 $profile->http_method( $c->req->method );
122  
123 if( my $cross_application_tracing_id = $c->req->headers->header("X-PerlGuard-Auto-Track") ) {
124 $profile->cross_application_tracing_id($cross_application_tracing_id);
125 }
126 }
127  
128 do {
129 local $PerlGuard::Agent::CURRENT_PROFILE_UUID = $c->tx->{'PerlGuard::Profile'}->uuid() unless $c->stash->{'mojo.static'} ;
130 return $next->();
131 };
132  
133 }
134  
135 }
136  
137 $next->();
138 });
139  
140 $app->helper(perlguard_profile => sub {
141 my $c = shift;
142 return $c->tx->{'PerlGuard::Profile'};
143 });
144  
145 $agent->detect_monitors();
146 $agent->start_monitors();
147  
148 }
149  
150  
151 1;