| 1 | package PerlGuard::Agent; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use PerlGuard::Agent::Profile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Data::UUID; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our @ISA = qw(); | |||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.15'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | has output_method => ( is => 'rw', lazy => 1, default => sub { 'PerlGuard::Agent::Output::PerlGuardServer' } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 12 | 1 | has output => (is => 'lazy' ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | has profiles => ( is => 'rw', default => sub { {} }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | has monitors => ( is => 'rw', default => sub { [] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | has async_mode => (is => 'rw', default => sub { 0 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | has api_key => (is => 'rw'); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | has data_uuid => (is => 'ro', default => sub { Data::UUID->new }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | has warnings => (is => 'rw', default => sub { 0 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | our $CURRENT_PROFILE_UUID = undef; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Current profile only makes sense in a sync app which can only have one request running at a time | ||||||||||||||||||||||||||||
| 27 | # Alternatively it could be used with a local statment elsewhere in an async app to make use of lexical scoping | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub current_profile { | ||||||||||||||||||||||||||
|
Subroutine
current_profile: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | warn "current_profile is meaningless when running in async mode" if $self->async_mode(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | #Check if $CURRENT_PROFILE has a value in is | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if(defined $CURRENT_PROFILE_UUID) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if($self->profiles->{$CURRENT_PROFILE_UUID}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if($self->warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | warn "Profile identified has finished, this should not happen" if $self->profiles->{$CURRENT_PROFILE_UUID}->has_finished(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | return $self->profiles->{$CURRENT_PROFILE_UUID}; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if($self->warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | warn "the package variable CURRENT_PROFILE_UUID is not defined, this is potentially a race condition bug"; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if($self->warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | warn "Using fallback mechanism to identify profile"; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # This is not safe, as we could get monitors reporting on the wrong profile | ||||||||||||||||||||||||||||
| 53 | 0 | my @uuids = keys %{ $self->profiles }; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if(scalar(@uuids) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | return $self->profiles->{$uuids[0]}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if($self->warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | warn "Could not identify the most recent profile, we had " . scalar(@uuids) . " profiles currently active with keys @uuids and the current profile var thinks its " . $CURRENT_PROFILE_UUID ; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub _build_output { | ||||||||||||||||||||||||||
|
Subroutine
_build_output: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $output_method = $self->output_method(); | |||||||||||||||||||||||||||
| 71 | 0 | eval "require $output_method"; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | die "Cannot require module $output_method, perhaps you specified an invalid module name in output_method" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my @params; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | push(@params, api_key => $self->api_key) if($self->api_key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | return $output_method->new( @params ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # This supports a transaction being added for a specific profile, which is a future feature we will need to support async apps | ||||||||||||||||||||||||||||
| 82 | # For now though when this is called there should only ever be one profile in process (sync app) | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub add_database_transaction { | ||||||||||||||||||||||||||
|
Subroutine
add_database_transaction: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 0 | my $database_transaction = shift; | |||||||||||||||||||||||||||
| 86 | 0 | my $intended_profile_uuid = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if($intended_profile_uuid and (my $profile = $self->profiles->{$intended_profile_uuid})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$intended_profile_uuid and my $profile = $self->profiles->{$intended_profile_uuid}
MC/DC0%0/2
$intended_profile_uuid and my $profile = $self->profiles->{$intended_profile_uuid}
$intended_profile_uuid
my $profile = $self->profiles->{$intended_profile_uuid}
Truth table0%0/3
A and B
A= $intended_profile_uuidB= my $profile = $self->profiles->{$intended_profile_uuid}
| |||||||||||||||||||||||||||||
| 89 | 0 | $profile->add_database_transaction($database_transaction); | |||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | # Profile not specified! Time to guess | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my $current_profile = $self->current_profile; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | if($current_profile && Scalar::Util::blessed($current_profile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$current_profile and builtin::blessed($current_profile)
MC/DC0%0/2
$current_profile and builtin::blessed($current_profile)
$current_profile
builtin::blessed($current_profile)
Truth table0%0/3
A and B
A= $current_profileB= builtin::blessed($current_profile)
| |||||||||||||||||||||||||||||
| 95 | 0 | $current_profile->add_database_transaction($database_transaction); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if($self->warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | warn "Caught a database transaction occuring outside of a profile"; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub add_webservice_transaction { | ||||||||||||||||||||||||||
|
Subroutine
add_webservice_transaction: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 0 | my $web_transaction = shift; | |||||||||||||||||||||||||||
| 110 | 0 | my $intended_profile_uuid = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if($intended_profile_uuid and (my $profile = $self->profiles->{$intended_profile_uuid})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$intended_profile_uuid and my $profile = $self->profiles->{$intended_profile_uuid}
MC/DC0%0/2
$intended_profile_uuid and my $profile = $self->profiles->{$intended_profile_uuid}
$intended_profile_uuid
my $profile = $self->profiles->{$intended_profile_uuid}
Truth table0%0/3
A and B
A= $intended_profile_uuidB= my $profile = $self->profiles->{$intended_profile_uuid}
| |||||||||||||||||||||||||||||
| 113 | 0 | $profile->add_webservice_transaction($web_transaction); | |||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||
| 115 | # Profile not specified | ||||||||||||||||||||||||||||
| 116 | 0 | my $current_profile = $self->current_profile; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | if($current_profile && Scalar::Util::blessed($current_profile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$current_profile and builtin::blessed($current_profile)
MC/DC0%0/2
$current_profile and builtin::blessed($current_profile)
$current_profile
builtin::blessed($current_profile)
Truth table0%0/3
A and B
A= $current_profileB= builtin::blessed($current_profile)
| |||||||||||||||||||||||||||||
| 118 | 0 | $current_profile->add_webservice_transaction($web_transaction);; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if($self->warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | warn "Caught a web transaction occuring outside of a profile" | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub create_new_profile { | ||||||||||||||||||||||||||
|
Subroutine
create_new_profile: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $profile = PerlGuard::Agent::Profile->new({ | |||||||||||||||||||||||||||
| 133 | # Set some things | ||||||||||||||||||||||||||||
| 134 | uuid => $self->data_uuid->create_str(), | ||||||||||||||||||||||||||||
| 135 | agent => $self | ||||||||||||||||||||||||||||
| 136 | }); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | $self->profiles->{$profile->uuid} = $profile; | |||||||||||||||||||||||||||
| 139 | 0 | Scalar::Util::weaken($self->profiles->{$profile->uuid}); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | return $profile; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub remove_profile { | ||||||||||||||||||||||||||
|
Subroutine
remove_profile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | 0 | my $profile_id = shift; | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | $profile_id = $profile_id->uuid() if Scalar::Util::blessed($profile_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | delete $self->profiles->{$profile_id}; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub detect_monitors { | ||||||||||||||||||||||||||
|
Subroutine
detect_monitors: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | foreach my $monitor(qw( PerlGuard::Agent::Monitors::DBI PerlGuard::Agent::Monitors::NetHTTP )) { | |||||||||||||||||||||||||||
| 156 | eval { | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | eval "require $monitor; 1" or die "skipping loading monitor $monitor"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | my $monitor = $monitor->new(agent => $self); | |||||||||||||||||||||||||||
| 159 | 0 | $monitor->die_unless_suitable(); | |||||||||||||||||||||||||||
| 160 | 0 | push(@{$self->monitors}, $monitor); | |||||||||||||||||||||||||||
| 161 | 0 | 1; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | warn "Error when loading monitor $monitor: " . $@; | |||||||||||||||||||||||||||
| 164 | 0 | next; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub start_monitors { | ||||||||||||||||||||||||||
|
Subroutine
start_monitors: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | foreach my $monitor(@{$self->monitors}) { $monitor->start_monitoring() } | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub stop_monitors { | ||||||||||||||||||||||||||
|
Subroutine
stop_monitors: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | foreach my $monitor(@{$self->monitors}) { $monitor->stop_monitoring() } | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | 1; | |||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||