blib/lib/Mojolicious/Plugin/Prometheus.pm

filter: Statementstmt 91.1% 82 / 90 Branchbran 70.0% 7 / 10 Conditioncond 76.4% 26 / 34 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 77.4% 24 / 31 Podpod 100% 1 / 1 totaltotal 80.8% 148 / 183 CC 23 SCAR 18.1
CC
23
Cov
86%
register
16.2
_start
3.6
_guard
3.1
CRAP 6.1
↑ Summary
1 package Mojolicious::Plugin::Prometheus;
2 11 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use Time::HiRes qw/gettimeofday tv_interval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Net::Prometheus;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use IPC::ShareLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '1.4.1';
8  
9 9 ▶has prometheus => sub { Net::Prometheus->new(disable_process_collector => 1) };
Subroutine __ANON__: called CC=1 SCAR=0.0
10 0 ▶has route => sub {undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 0 ▶has http_request_duration_seconds => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 undef;
13 11 };
14 0 ▶has http_request_size_bytes => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 0 undef;
16 11 };
17 0 ▶has http_response_size_bytes => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 undef;
19 11 };
20 0 ▶has http_requests_total => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 undef;
22 11 };
23  
24  
25 10 ▶sub register {
Subroutine register: called CC=16 SCAR=27.9
Pod documented
26 10 my ($self, $app, $config) = @_;
27  
28 10 ▶ $self->{key} = $config->{shm_key} || '12345';
Condition100%2/2
$config->{'shm_key'} || '12345'
l!l
28
MC/DC100%1/1
$config->{'shm_key'} || '12345'
$config->{'shm_key'}
Truth table100%2/2
A
A= $config->{'shm_key'}
Aresult
11
00
29  
30 10 ▶ $self->prometheus($config->{prometheus}) if $config->{prometheus};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"prometheus"} 1 9 10
31 10 ▶ $app->helper(prometheus => sub { $self->prometheus });
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 # Only the two built-in servers are supported for now
34 10 ▶ $app->hook(before_server_start => sub { $self->_start(@_, $config) });
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 $self->http_request_duration_seconds(
37 $self->prometheus->new_histogram(
38 namespace => $config->{namespace} // undef,
39 subsystem => $config->{subsystem} // undef,
40 name => "http_request_duration_seconds",
41 help => "Histogram with request processing time",
42 labels => [qw/worker method/],
43 buckets => $config->{duration_buckets} // undef,
44 )
45 10 ▶ );
Condition66%4/6
$config->{"namespace"} // undef
l!l
19
$config->{"subsystem"} // undef
l!l
010
$config->{"duration_buckets"} // undef
l!l
010
MC/DC100%1/1
$config->{"namespace"} // undef
$config->{"namespace"}
MC/DC0%0/1
$config->{"subsystem"} // undef
$config->{"subsystem"}
MC/DC0%0/1
$config->{"duration_buckets"} // undef
$config->{"duration_buckets"}
Truth table100%2/2
A
A= $config->{"namespace"}
Aresult
11
00
Truth table50%1/2
A
A= $config->{"subsystem"}
Aresult
11
00
Truth table50%1/2
A
A= $config->{"duration_buckets"}
Aresult
11
00
46  
47 $self->http_request_size_bytes(
48 $self->prometheus->new_histogram(
49 namespace => $config->{namespace} // undef,
50 subsystem => $config->{subsystem} // undef,
51 name => "http_request_size_bytes",
52 help => "Histogram containing request sizes",
53 labels => [qw/worker method/],
54 buckets => $config->{request_buckets}
55 10 ▶ // [(1, 50, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000)],
Condition83%5/6
$config->{"namespace"} // undef
l!l
19
$config->{"subsystem"} // undef
l!l
010
$config->{"request_buckets"} // [1, 50, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
l!l
19
MC/DC100%1/1
$config->{"namespace"} // undef
$config->{"namespace"}
MC/DC0%0/1
$config->{"subsystem"} // undef
$config->{"subsystem"}
MC/DC100%1/1
$config->{"request_buckets"} // [1, 50, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
$config->{"request_buckets"}
Truth table100%2/2
A
A= $config->{"namespace"}
Aresult
11
00
Truth table50%1/2
A
A= $config->{"subsystem"}
Aresult
11
00
Truth table100%2/2
A
A= $config->{"request_buckets"}
Aresult
11
00
56 )
57 );
58  
59 $self->http_response_size_bytes(
60 $self->prometheus->new_histogram(
61 namespace => $config->{namespace} // undef,
62 subsystem => $config->{subsystem} // undef,
63 name => "http_response_size_bytes",
64 help => "Histogram containing response sizes",
65 labels => [qw/worker method code/],
66 buckets => $config->{response_buckets}
67 10 ▶ // [(5, 50, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000)],
Condition83%5/6
$config->{"namespace"} // undef
l!l
19
$config->{"subsystem"} // undef
l!l
010
$config->{"response_buckets"} // [5, 50, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
l!l
19
MC/DC100%1/1
$config->{"namespace"} // undef
$config->{"namespace"}
MC/DC0%0/1
$config->{"subsystem"} // undef
$config->{"subsystem"}
MC/DC100%1/1
$config->{"response_buckets"} // [5, 50, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
$config->{"response_buckets"}
Truth table100%2/2
A
A= $config->{"namespace"}
Aresult
11
00
Truth table50%1/2
A
A= $config->{"subsystem"}
Aresult
11
00
Truth table100%2/2
A
A= $config->{"response_buckets"}
Aresult
11
00
68 )
69 );
70  
71 $self->http_requests_total(
72 $self->prometheus->new_counter(
73 namespace => $config->{namespace} // undef,
74 subsystem => $config->{subsystem} // undef,
75 10 ▶ name => "http_requests_total",
Condition75%3/4
$config->{"namespace"} // undef
l!l
19
$config->{"subsystem"} // undef
l!l
010
MC/DC100%1/1
$config->{"namespace"} // undef
$config->{"namespace"}
MC/DC0%0/1
$config->{"subsystem"} // undef
$config->{"subsystem"}
Truth table100%2/2
A
A= $config->{"namespace"}
Aresult
11
00
Truth table50%1/2
A
A= $config->{"subsystem"}
Aresult
11
00
76 help =>
77 "How many HTTP requests processed, partitioned by status code and HTTP method.",
78 labels => [qw/worker method code/]
79 )
80 );
81  
82 $app->hook(
83 218 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 218 my ($c) = @_;
85 218 $c->stash('prometheus.start_time' => [gettimeofday]);
86 218 $self->http_request_size_bytes->observe($$, $c->req->method,
87 $c->req->content->body_size);
88 }
89 10 );
90  
91 $app->hook(
92 218 ▶ after_render => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 218 my ($c) = @_;
94 218 $self->http_request_duration_seconds->observe($$, $c->req->method,
95 tv_interval($c->stash('prometheus.start_time')));
96 }
97 10 );
98  
99 $app->hook(
100 218 ▶ after_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 218 my ($c) = @_;
102 218 $self->http_requests_total->inc($$, $c->req->method, $c->res->code);
103 218 $self->http_response_size_bytes->observe($$, $c->req->method,
104 $c->res->code, $c->res->content->body_size);
105 }
106 10 );
107  
108  
109 10 ▶ my $prefix = $config->{route} // $app->routes->under('/');
Condition66%2/3
$config->{"route"} // $app->routes->under("/")
l!l&&r!l&&!r
190
MC/DC0%0/2
$config->{"route"} // $app->routes->under("/")
$config->{"route"} $app->routes->under("/")
Truth table66%2/3
A // B
A= $config->{"route"}B= $app->routes->under("/")
ABresult
1X1
011
000
110 10 ▶ $self->route($prefix->get($config->{path} // '/metrics'));
Condition100%2/2
$config->{"path"} // "/metrics"
l!l
19
MC/DC100%1/1
$config->{"path"} // "/metrics"
$config->{"path"}
Truth table100%2/2
A
A= $config->{"path"}
Aresult
11
00
111 $self->route->to(
112 11 ▶ cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 11 my ($c) = @_;
114 # Collect stats and render
115 11 ▶ $self->_guard->_change(sub { $_->{$$} = $app->prometheus->render });
Subroutine __ANON__: called CC=1 SCAR=0.0
116 $c->render(
117 text => join("\n",
118 35 map { ($self->_guard->_fetch->{$_}) }
119 11 sort keys %{$self->_guard->_fetch}),
120 format => 'txt'
121 );
122 }
123 10 );
124  
125 }
126  
127 57 ▶sub _guard {
Subroutine _guard: called CC=3 SCAR=11.4
128 57 my $self = shift;
129  
130 my $share = $self->{share}
131 57 ▶ ||= IPC::ShareLite->new(-key => $self->{key}, -create => 1, -destroy => 0)
Condition60%3/5
$self->{"share"} ||= "IPC::ShareLite"->new("-key", $self->{"key"}, "-create", 1, "-destroy", 0) || die($!)
l!l&&r!l&&!r
4890
'IPC::ShareLite'->new('-key', $self->{'key'}, '-create', 1, '-destroy', 0) || die($!)
l!l
90
MC/DC0%0/2
$self->{"share"} ||= "IPC::ShareLite"->new("-key", $self->{"key"}, "-create", 1, "-destroy", 0) || die($!)
$self->{"share"} 'IPC::ShareLite'->new('-key', $self->{'key'}, '-create', 1, '-destroy', 0)
Truth table66%2/3
A ||= B
A= $self->{"share"}B= 'IPC::ShareLite'->new('-key', $self->{'key'}, '-create', 1, '-destroy', 0)
ABresult
1X1
011
000
132 || die $!;
133  
134 57 return Mojolicious::Plugin::Mojolicious::_Guard->new(share => $share);
135 }
136  
137 18 ▶sub _start {
Subroutine _start: called CC=3 SCAR=12.7
138  
139 #my ($self, $app, $config) = @_;
140 18 my ($self, $server, $app, $config) = @_;
141 18 ▶ return unless $server->isa('Mojo::Server::Daemon');
Branch: 1 branch
BranchTrueFalseTotal
unless $server->isa("Mojo::Server::Daemon") 0 18 18
142  
143 Mojo::IOLoop->next_tick(
144 18 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
145 18 my $pc = Net::Prometheus::ProcessCollector->new(labels => [worker => $$]);
146 18 ▶ $self->prometheus->register($pc) if $pc;
Branch: 1 branch
BranchTrueFalseTotal
if $pc 18 0 18
147 }
148 18 );
149  
150 # Remove stopped workers
151 $server->on(
152 0 ▶ reap => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 my ($server, $pid) = @_;
154 0 ▶ $self->_guard->_change(sub { delete $_->{$pid} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 }
156 18 ▶ ) if $server->isa('Mojo::Server::Prefork');
Branch: 1 branch
BranchTrueFalseTotal
if $server->isa("Mojo::Server::Prefork") 0 18 18
157 }
158  
159  
160 package Mojolicious::Plugin::Mojolicious::_Guard;
161 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 11 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 11 ▶use Sereal qw(get_sereal_decoder get_sereal_encoder);
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 11 my ($DECODER, $ENCODER) = (get_sereal_decoder, get_sereal_encoder);
167  
168 57 ▶sub DESTROY { shift->{share}->unlock }
Subroutine DESTROY: called CC=1 SCAR=0.0
169  
170 57 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
171 57 my $self = shift->SUPER::new(@_);
172 57 $self->{share}->lock(LOCK_EX);
173 57 return $self;
174 }
175  
176 11 ▶sub _change {
Subroutine _change: called CC=2 SCAR=6.9
177 11 my ($self, $cb) = @_;
178 11 my $stats = $self->_fetch;
179 11 $cb->($_) for $stats;
180 11 $self->_store($stats);
181 }
182  
183 57 ▶sub _fetch {
Subroutine _fetch: called CC=2 SCAR=6.9
184 57 ▶ return {} unless my $data = shift->{share}->fetch;
Branch: 1 branch
BranchTrueFalseTotal
unless my $data = +(shift())->{"share"}->fetch 3 54 57
185 54 return $DECODER->decode($data);
186 }
187  
188 11 ▶sub _store { shift->{share}->store($ENCODER->encode(shift)) }
Subroutine _store: called CC=1 SCAR=0.0
189  
190 11 1;
191 __END__
↑ Summary