Netdata/Collector/HTTP.pm

filter: Statementstmt 86.0% 68 / 79 Branchbran 75.0% 27 / 36 Conditioncond 66.6% 26 / 39 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 70.9% 134 / 189 CC 37 SCAR 26.1
CC
37
Cov
79%
register
19.3
_make_job
17.8
__ANON__
9.1
CRAP 13.7
↑ Summary Netdata/Util.pm »
1 package Mojo::Netdata::Collector::HTTP;
2 1 ▶use Mojo::Base 'Mojo::Netdata::Collector', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Netdata::Util qw(logf safe_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Mojo::Netdata;
9 1 our $VERSION = '0.04';
10  
11 1 has concurrency => 4;
12 0 ▶has jobs => sub ($self) { +[] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 1 has type => 'HTTP';
14 3 ▶has ua => sub { Mojo::UserAgent->new(insecure => 0, connect_timeout => 5, request_timeout => 5) };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 has update_every => 30;
16  
17 2 ▶sub register ($self, $config, $netdata) {
Subroutine register: called CC=15 SCAR=29.6
Pod documented
18 $config->{update_every} ? $self->update_every($config->{update_every})
19 2 ▶ : $netdata->update_every >= 10 ? $self->update_every($netdata->update_every)
Branch: 2 branches
BranchTrueFalseTotal
$config->{'update_every'} ? : 0 2 2
$netdata->update_every >= 10 ? : 2 0 2
20 : $self->update_every(30);
21  
22 2 ▶ $self->ua->insecure($config->{insecure}) if defined $config->{insecure};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"insecure"} 0 2 2
23 2 ▶ $self->ua->connect_timeout($config->{connect_timeout}) if defined $config->{connect_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"connect_timeout"} 0 2 2
24 2 ▶ $self->ua->request_timeout($config->{request_timeout}) if defined $config->{request_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"request_timeout"} 0 2 2
25 2 ▶ $self->ua->proxy->detect if $config->{proxy} // 1;
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"proxy"} // 1 2 0 2
Condition50%1/2
$config->{"proxy"} // 1
l!l
02
MC/DC0%0/1
$config->{"proxy"} // 1
$config->{"proxy"}
Truth table50%1/2
A
A= $config->{"proxy"}
Aresult
11
00
26 2 ▶ $self->ua->transactor->name($config->{user_agent} || "Mojo-Netdata/$VERSION (Perl)");
Condition50%1/2
$config->{'user_agent'} || "Mojo-Netdata/$Mojo::Netdata::Collector::HTTP::VERSION (Perl)"
l!l
02
MC/DC0%0/1
$config->{'user_agent'} || "Mojo-Netdata/$Mojo::Netdata::Collector::HTTP::VERSION (Perl)"
$config->{'user_agent'}
Truth table50%1/2
A
A= $config->{'user_agent'}
Aresult
11
00
27 2 ▶ $self->concurrency($config->{concurrency} || 4);
Condition50%1/2
$config->{'concurrency'} || 4
l!l
02
MC/DC0%0/1
$config->{'concurrency'} || 4
$config->{'concurrency'}
Truth table50%1/2
A
A= $config->{'concurrency'}
Aresult
11
00
28 2 $self->jobs([]);
29  
30 0 ▶ my @jobs = ref $config->{jobs} eq 'HASH' ? %{$config->{jobs}} : @{$config->{jobs}};
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{'jobs'} eq 'HASH' ? : 0 2 2
31 2 ▶ while (my $url = shift @jobs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = shift @jobs) 10 2 12
32 10 ▶ my $job = $self->_make_job($url => ref $jobs[0] eq 'HASH' ? shift @jobs : {}, $config);
Branch: 1 branch
BranchTrueFalseTotal
ref $jobs[0] eq 'HASH' ? : 8 2 10
33 10 ▶ push @{$self->jobs}, $job if $job;
Branch: 1 branch
BranchTrueFalseTotal
if $job 8 2 10
34 }
35  
36 2 ▶ return @{$self->jobs} ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->jobs;} ? : 2 0 2
37 }
38  
39 0 ▶sub update_p ($self) {
Subroutine update_p: not called CC=1 SCAR=6.9
Pod documented
40 0 my ($ua, @p) = ($self->ua);
41  
42 return Mojo::Promise->map(
43 {concurrency => $self->concurrency},
44 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 my ($job, $t0) = ($_, time);
46 0 my $tx = $ua->build_tx(@{$job->[0]});
47 0 ▶ return $ua->start_p($tx)->then(sub ($tx) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 $job->[1]->($tx, $t0);
49 0 ▶ })->catch(sub ($err) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 $job->[1]->($tx, $t0, {message => $err});
51 0 });
52 },
53 0 @{$self->jobs}
54 );
55 }
56  
57 10 ▶sub _make_job ($self, $url, $params, $defaults) {
Subroutine _make_job: called CC=17 SCAR=28.8
58 10 $url = Mojo::URL->new($url);
59 10 ▶ return undef unless my $host = $url->host;
Branch: 1 branch
BranchTrueFalseTotal
unless my $host = $url->host 1 9 10
60  
61 9 ▶ my $headers = Mojo::Headers->new->from_hash($defaults->{headers} || {});
Condition100%2/2
$defaults->{'headers'} || {}
l!l
27
MC/DC100%1/1
$defaults->{'headers'} || {}
$defaults->{'headers'}
Truth table100%2/2
A
A= $defaults->{'headers'}
Aresult
11
00
62 9 ▶ $headers->header($_ => $params->{headers}{$_}) for keys %{$params->{headers} || {}};
Condition100%2/2
$params->{'headers'} || {}
l!l
18
MC/DC100%1/1
$params->{'headers'} || {}
$params->{'headers'}
Truth table100%2/2
A
A= $params->{'headers'}
Aresult
11
00
63 9 ▶ ($headers->header(Host => $url->host), $url->host($params->{via})) if $params->{via};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"via"} 1 8 9
64  
65 9 ▶ my $dimension = $params->{dimension} || $headers->host || $url->host;
Condition83%5/6
$params->{'dimension'} || $headers->host || $url->host
l!l&&r!l&&!r
360
$params->{"dimension"} or $headers->host
l!l&&r!l&&!r
216
MC/DC0%0/3
$params->{'dimension'} || $headers->host || $url->host
$params->{"dimension"} $headers->host $url->host
Truth table75%3/4
A or B || C
A= $params->{"dimension"}B= $headers->hostC= $url->host
ABCresult
1XX1
01X1
0011
0000
66 9 ▶ my $family = $params->{family} || $defaults->{family} || $headers->host || $url->host;
Condition77%7/9
$params->{'family'} || $defaults->{'family'} || $headers->host || $url->host
l!l&&r!l&&!r
540
$params->{"family"} or $defaults->{"family"} or $headers->host
l!l&&r!l&&!r
504
$params->{"family"} or $defaults->{"family"}
l!l&&r!l&&!r
414
MC/DC0%0/4
$params->{'family'} || $defaults->{'family'} || $headers->host || $url->host
$params->{"family"} $defaults->{"family"} $headers->host $url->host
Truth table60%3/5
A or B or C || D
A= $params->{"family"}B= $defaults->{"family"}C= $headers->hostD= $url->host
ABCDresult
1XXX1
01XX1
001X1
00011
00000
67 9 ▶ my $log_level = $defaults->{log_level} || 'debug';
Condition50%1/2
$defaults->{'log_level'} || 'debug'
l!l
09
MC/DC0%0/1
$defaults->{'log_level'} || 'debug'
$defaults->{'log_level'}
Truth table50%1/2
A
A= $defaults->{'log_level'}
Aresult
11
00
68  
69 9 my $code_chart = $self->chart("${family}_code")->title("HTTP Status code for $family")
70 ->context('httpcheck.code')->family($family)->units('#');
71  
72 9 ▶ if ($code_chart->dimension($dimension)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_chart->dimension($dimension)) 1 8 9
73 1 logf(warnings => 'Family "%s" already has dimension "%s".', $family, $dimension);
74 1 return undef;
75 }
76  
77 8 my $time_chart = $self->chart("${family}_time")->title("Response time for $family")
78 ->context('httpcheck.responsetime')->family($family)->units('ms');
79  
80 8 $code_chart->dimension($dimension => {});
81 8 $time_chart->dimension($dimension => {});
82  
83 6 ▶ my $update = sub ($tx, $t0, $err = undef) {
Subroutine __ANON__: called CC=7 SCAR=22.1
84 6 ▶ $err ||= $tx->error;
Condition33%1/3
$err ||= $tx->error
l!l&&r!l&&!r
006
MC/DC0%0/2
$err ||= $tx->error
$err $tx->error
Truth table33%1/3
A ||= B
A= $errB= $tx->error
ABresult
1X1
011
000
85 6 my $req = $tx->req;
86 6 ▶ my $code = $tx->res->code // 0;
Condition50%1/2
$tx->res->code // 0
l!l
60
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
Aresult
11
00
87 6 ▶ my @msg = ($req->method, $req->url, $err || {code => $code}, $req->headers->to_hash(1));
Condition50%1/2
$err || {'code', $code}
l!l
06
MC/DC0%0/1
$err || {'code', $code}
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
88 6 ▶ logf(($code >= 200 && $code < 300 ? $log_level : 'warnings'), '%s %s == %s %s', @msg);
Branch: 1 branch
BranchTrueFalseTotal
$code >= 200 && $code < 300 ? : 0 6 6
Condition33%1/3
$code >= 200 and $code < 300
!ll&&!rl&&r
060
MC/DC0%0/2
$code >= 200 and $code < 300
$code >= 200 $code < 300
Truth table33%1/3
A and B
A= $code >= 200B= $code < 300
ABresult
0X0
100
111
89  
90 6 $time_chart->dimension($dimension => {value => int(1000 * (time - $t0))});
91 6 $code_chart->dimension($dimension => {value => $code});
92 8 };
93  
94 8 my @data;
95 8 push @data, $headers->to_hash(1);
96 push @data,
97 exists $params->{json} ? (json => $params->{json})
98 : exists $params->{form} ? (form => $params->{form})
99 : exists $params->{body} ? ($params->{body})
100 8 ▶ : ();
Branch: 3 branches
BranchTrueFalseTotal
exists $params->{'json'} ? : 1 7 8
exists $params->{'form'} ? : 1 6 7
exists $params->{'body'} ? : 1 5 6
101  
102 8 ▶ my $http_method = $params->{method} || 'GET';
Condition100%2/2
$params->{'method'} || 'GET'
l!l
17
MC/DC100%1/1
$params->{'method'} || 'GET'
$params->{'method'}
Truth table100%2/2
A
A= $params->{'method'}
Aresult
11
00
103 8 logf(debug => 'Tracking %s %s %s', $http_method, $url, $data[0]);
104 8 return [[$http_method, $url->to_unsafe_string, @data], $update];
105 }
106  
107 1 1;
108  
109 =encoding utf8
110  
111 =head1 NAME
112  
113 Mojo::Netdata::Collector::HTTP - A website collector for Mojo::Netdata
114  
115 =head1 SYNOPSIS
116  
117 =head2 Config
118  
119 Below is an example C</etc/netdata/mojo.conf.d/http.conf.pl> config file. Note
120 that the file can have any name and you have have as many as you want, as long
121 as it has the C<.conf.pl> extension.
122  
123 {
124 # Required
125 collector => 'Mojo::Netdata::Collector::HTTP',
126  
127 # Optional
128 concurrency => 4, # Number of HTTP requests at the same time
129 insecure => 0, # Set to "1" to allow insecure SSL/TLS connections
130 connect_timeout => 5, # Max time for the connection to be established
131 request_timeout => 5, # Max time for the whole request to complete
132 proxy => 1, # Set to "0" to disable proxy auto-detect
133 update_every => 30, # How often to run the "jobs" below
134 user_agent => '...', # Custom User-Agent name
135  
136 # Default values, unless defined in the job
137 family => 'default-family-name',
138 headers => {'X-Merged-With' => 'headers inside job config'},
139  
140 # Required - List of URLs and an optional config hash (object)
141 jobs => [
142  
143 # List of URLs to check (Config is optional)
144 'https://superwoman.example.com',
145 'https://superman.example.com',
146  
147 # URL and config parameters
148 'https://example.com' => {
149 method => 'GET', # GET (Default), HEAD, POST, ...
150 headers => {'X-Foo' => 'bar'}, # HTTP headers
151  
152 # Replace "host" in the URL with this IP and set the "Host" header
153 via => '192.168.2.1',
154  
155 # Set "dimension" to get a custom label in the chart.
156 # Default to the "Host" header or the host part of the URL.
157 dimension => 'foo', # Default: "example.com"
158  
159 # Set "family" to group multiple domains together in one chart,
160 # Default to the "Host" header or the host part of the URL.
161 family => 'bar', # Default: "example.com"
162  
163 # Only one of these can be present
164 json => {...}, # JSON HTTP body
165 form => {key => $value}, # Form data
166 body => '...', # Raw HTTP body
167 },
168 ],
169 };
170  
171 =head2 Health
172  
173 Here is an example C</etc/netdata/health.d/mojo-http.conf> file:
174  
175 template: web_server_code
176 on: httpcheck.code
177 class: Errors
178 type: Web Server
179 component: HTTP endpoint
180 plugin: mojo
181 lookup: max -5m absolute foreach *
182 every: 1m
183 warn: $this >= 300 && $this < 500
184 crit: $this >= 500 && $this != 503
185 to: webmaster
186  
187 template: web_server_up
188 on: httpcheck.code
189 class: Errors
190 type: Web Server
191 component: HTTP endpoint
192 plugin: mojo
193 lookup: min -5m absolute foreach *
194 every: 1m
195 crit: $this == 0
196 units: up/down
197 to: webmaster
198  
199 =head1 DESCRIPTION
200  
201 L<Mojo::Netdata::Collector::HTTP> is a collector that can chart web page
202 response time and HTTP status codes.
203  
204 =head1 ATTRIBUTES
205  
206 =head2 concurrency
207  
208 $int = $collector->concurrency;
209  
210 Number of requests that should be performed at the same time. Default is 4.
211  
212 =head2 jobs
213  
214 $array_ref = $collector->jobs;
215  
216 A list of jobs generated by L</register>.
217  
218 =head2 type
219  
220 $str = $collector->type;
221  
222 Defaults to "HTTP".
223  
224 =head2 ua
225  
226 $ua = $collector->ua;
227  
228 Holds a L<Mojo::UserAgent>.
229  
230 =head2 update_every
231  
232 $num = $chart->update_every;
233  
234 Default value is 30. See L<Mojo::Netdata::Collector/update_every> for more
235 details.
236  
237 =head1 METHODS
238  
239 =head2 register
240  
241 $collector = $collector->register(\%config, $netdata);
242  
243 Returns a L<$collector> object if any "jobs" are defined in C<%config>. Will
244 also set L</update_every> from C<%config> or use L<Mojo::Netdata/update_every>
245 if it is 10 or greater.
246  
247 =head2 update_p
248  
249 $p = $collector->update_p;
250  
251 Gathers information about the "jobs" registered.
252  
253 =head1 SEE ALSO
254  
255 L<Mojo::Netdata> and L<Mojo::Netdata::Collector>.
256  
257 =cut
↑ Summary Netdata/Util.pm »