Agent/Output/PerlGuardServer.pm

filter: Statementstmt 34.0% 32 / 94 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.0% 8 / 20 Podpod 0.0% 0 / 7 totaltotal 25.0% 40 / 160 CC 19 SCAR 29.4
CC
19
Cov
25%
save
59.0
format_database_transactions
20.0
format_webservice_transactions
20.0
CRAP 18.9
« Agent/Monitors/DBI/Tracer.pm ↑ Summary Agent/Monitors/NetHTTP.pm »
1 package PerlGuard::Agent::Output::PerlGuardServer;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 extends 'PerlGuard::Agent::Output';
4  
5 1 ▶use HTTP::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has api_key => ( is => 'rw', lazy => 1, default => \&_attempt_to_fetch_api_key_from_env_or_die);
13 1 has base_url => ( is => 'rw', lazy => 1, default => \&DEFAULT_BASE_URL );
14  
15 0 ▶has async_http => ( is => 'rw', lazy => 1, default => sub { HTTP::Async->new(timeout => 2, max_request_time=>2, slots=>1000000); });
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 0 ▶has disabled_until => (is => 'rw', lazy => 1, default => sub { [0,0] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
18  
19 0 ▶has headers => (is => 'rw', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 HTTP::Headers->new(
21 'X-API-KEY' => shift->api_key,
22 'content-type' => 'application/json'
23 )
24 1 });
25  
26 0 ▶has json_encoder => ( is => 'rw', lazy => 1, default => sub { JSON->new->utf8->convert_blessed->allow_blessed });
Subroutine __ANON__: not called CC=1 SCAR=1.2
27  
28 0 ▶sub DEFAULT_BASE_URL {
Subroutine DEFAULT_BASE_URL: not called CC=1 SCAR=6.9
Pod undocumented
29 0 return 'https://perlguard.com';
30 }
31  
32 0 ▶sub _attempt_to_fetch_api_key_from_env_or_die {
Subroutine _attempt_to_fetch_api_key_from_env_or_die: not called CC=2 SCAR=17.9
33 0 my $self = shift;
34 0 ▶ return $ENV{PERLGUARD_API_KEY} || die "No api_key specified, can be specified in PerlGuard::Agent->new() or with an ENV var named PERLGUARD_API_KEY";
Condition0%0/2
$ENV{'PERLGUARD_API_KEY'} || die('No api_key specified, can be specified in PerlGuard::Agent->new() or with an ENV var named PERLGUARD_API_KEY')
l!l
00
MC/DC0%0/1
$ENV{'PERLGUARD_API_KEY'} || die('No api_key specified, can be specified in PerlGuard::Agent->new() or with an ENV var named PERLGUARD_API_KEY')
$ENV{'PERLGUARD_API_KEY'}
Truth table0%0/2
A
A= $ENV{'PERLGUARD_API_KEY'}
Aresult
11
00
35 }
36  
37 0 ▶sub save {
Subroutine save: not called CC=8 SCAR=40.8
Pod undocumented
38 0 my $self = shift;
39 0 my $profile = shift;
40  
41 0 ▶ return unless $profile->should_save();
Branch: 1 branch
BranchTrueFalseTotal
unless $profile->should_save 0 0 0
42 0 my $content;
43  
44 0 do {
45 1 ▶ no warnings 'uninitialized'; #Protect our end users from any future errors we might make here
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 0 ▶ my $controller = $profile->controller || $profile->http_code;
Condition0%0/3
$profile->controller || $profile->http_code
l!l&&r!l&&!r
000
MC/DC0%0/2
$profile->controller || $profile->http_code
$profile->controller $profile->http_code
Truth table0%0/3
A || B
A= $profile->controllerB= $profile->http_code
ABresult
1X1
011
000
48 0 ▶ my $action = $profile->controller_action || $profile->url;
Condition0%0/3
$profile->controller_action || $profile->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$profile->controller_action || $profile->url
$profile->controller_action $profile->url
Truth table0%0/3
A || B
A= $profile->controller_actionB= $profile->url
ABresult
1X1
011
000
49  
50 0 $content = {
51 "start_time" => $profile->start_time,
52 "finish_time" => $profile->finish_time,
53 "total_elapsed_time_in_ms" => $profile->total_elapsed_time_in_ms,
54 "cross_application_tracing_id" => $profile->cross_application_tracing_id,
55 # "project_id": 10,
56 "type" => "web",
57 "grouping_name" => $controller . '#' . $action,
58 "database_transactions" => $self->format_database_transactions($profile),
59 "web_transactions" => $self->format_webservice_transactions($profile),
60 "database_elapsed_time_in_ms" => $profile->database_elapsed_time_in_ms,
61 "web_elapsed_time_in_ms" => $profile->webservice_elapsed_time_in_ms,
62 "sum_of_database_transactions" => $profile->database_transaction_count,
63 "sum_of_web_transactions" => $profile->webservice_transaction_count,
64 };
65  
66 0 $content = $self->json_encoder->encode($content);
67  
68 };
69  
70
71  
72 #warn $content;
73  
74 0 $self->check_responses();
75  
76 0 ▶ unless($self->can_run_yet()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_run_yet) 0 0 0
77 0 warn "Skipping due to previous errors\n";
78 0 return;
79 }
80  
81 #without_collectors_do {} - We can't really include sending this report in the request time..
82  
83 0 ▶ if($self->async_http->to_send_count > 250) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->async_http->to_send_count > 250) 0 0 0
84 0 warn "PerlGuard send queue has reached 250, dropping subsequent requests\n";
85 0 return;
86 }
87  
88 0 ▶ if($self->async_http->in_progress_count > 250) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->async_http->in_progress_count > 250) 0 0 0
89 0 warn "PerlGuard in progress count queue has reached 250, dropping subsequent requests\n";
90 0 return;
91 }
92  
93 0 my $request_id = $self->async_http->add( HTTP::Request->new(
94 POST => $self->base_url . "/collector/v1/profile",
95 $self->headers,
96 $content
97 ));
98  
99 0 ▶ while($self->async_http->to_send_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->async_http->to_send_count > 0) 0 0 0
100 0 $self->async_http->poke();
101 }
102  
103 #warn "completed send";
104  
105 # This helped keep things cleaner on local but it quite obviously causes a race condition,
106 #$self->async_http->remove($request_id);
107  
108  
109 }
110  
111 0 ▶sub flush {
Subroutine flush: not called CC=2 SCAR=17.9
Pod undocumented
112 0 my $self = shift;
113  
114 0 ▶ while($self->async_http->not_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->async_http->not_empty) 0 0 0
115 0 $self->async_http->next_response( $self->async_http->max_request_time );
116 }
117 }
118  
119 0 ▶sub check_responses {
Subroutine check_responses: not called CC=3 SCAR=24.8
Pod undocumented
120 0 my $self = shift;
121  
122 0 ▶ while(my $response = $self->async_http->next_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $response = $self->async_http->next_response) 0 0 0
123 0 ▶ if($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
124 #print STDERR "Response is " . $response->as_string ."\n";
125  
126 0 my $next_run_time = [Time::HiRes::gettimeofday];
127 0 $next_run_time->[0]++;
128  
129 0 $self->disabled_until($next_run_time);
130  
131 }
132  
133 }; #Clear queue
134 }
135  
136 0 ▶sub can_run_yet {
Subroutine can_run_yet: not called CC=2 SCAR=17.9
Pod undocumented
137 0 my $self = shift;
138  
139 0 ▶ return Time::HiRes::tv_interval( $self->disabled_until ) >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
Time::HiRes::tv_interval($self->disabled_until) >= 0 ? : 0 0 0
140 }
141  
142 0 ▶sub format_database_transactions {
Subroutine format_database_transactions: not called CC=4 SCAR=30.0
Pod undocumented
143 0 my $self = shift;
144 0 my $profile = shift;
145  
146 0 my @results;
147  
148 0 foreach my $row(@{$profile->database_transactions}) {
149 0 ▶ if($row->{start_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"start_time"}) 0 0 0
150 0 $row->{start_time_offset} = $profile->calculate_time_index_in_ms($row->{start_time});
151 }
152 0 ▶ if($row->{finish_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"finish_time"}) 0 0 0
153 0 $row->{finish_time_offset} = $profile->calculate_time_index_in_ms($row->{finish_time});
154 }
155  
156 0 push @results, $row;
157 }
158  
159 0 return \@results;
160  
161 }
162  
163 0 ▶sub format_webservice_transactions {
Subroutine format_webservice_transactions: not called CC=4 SCAR=30.0
Pod undocumented
164 0 my $self = shift;
165 0 my $profile = shift;
166  
167 0 my @results;
168  
169 0 foreach my $row(@{$profile->webservice_transactions}) {
170 0 ▶ if($row->{start_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"start_time"}) 0 0 0
171 0 $row->{start_time_offset} = $profile->calculate_time_index_in_ms($row->{start_time});
172 }
173 0 ▶ if($row->{finish_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"finish_time"}) 0 0 0
174 0 $row->{finish_time_offset} = $profile->calculate_time_index_in_ms($row->{finish_time});
175 }
176  
177 0 push @results, $row;
178  
179 }
180  
181 0 return \@results;
182  
183 }
184  
185  
186  
187  
188 1 1;
« Agent/Monitors/DBI/Tracer.pm ↑ Summary Agent/Monitors/NetHTTP.pm »