Mojolicious/Plugin/Prometheus.pm

filter: Statementstmt 93.2% 111 / 119 Branchbran 79.1% 19 / 24 Conditioncond 76.1% 16 / 21 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 84.0% 37 / 44 Podpod 100% 1 / 1 totaltotal 85.9% 189 / 220 CC 26 SCAR 18.6
CC
26
Cov
89%
register
19.1
_start
4.8
_register
3.0
CRAP 6.4
↑ Summary Mojolicious/Plugin/Prometheus/Collector/Perl.pm »
1 package Mojolicious::Plugin::Prometheus;
2 12 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use Mojolicious::Plugin::Prometheus::Collector::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use IPC::ShareLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Net::Prometheus;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Time::HiRes qw/gettimeofday tv_interval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Mojolicious::Plugin::Prometheus::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Prometheus::MetricRenderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 our $VERSION = '1.5.1';
12  
13 12 has prometheus => \&_prometheus;
14 12 has guard => \&_guard;
15 12 has share => \&_share;
16 0 ▶has route => sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 7 ▶has shm_key => sub { $$ };
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 10 ▶has global_collectors => sub { Mojo::Collection->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 # Attributes to hold the different metrics that are registered
22 0 ▶has http_request_duration_seconds => sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
23 0 ▶has http_request_size_bytes => sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 0 ▶has http_response_size_bytes => sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 0 ▶has http_requests_total => sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26  
27 # Configuration for the default metric types
28 11 ▶has config => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 {
30 http_request_duration_seconds => {
31 buckets => [.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10],
32 labels => [qw/worker method/],
33 234 ▶ cb => sub($c) { $$, $c->req->method, tv_interval($c->stash('prometheus.start_time')) },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 },
35 http_request_size_bytes => {
36 buckets => [1, 50, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000],
37 labels => [qw/worker method/],
38 234 ▶ cb => sub($c) { $$, $c->req->method, $c->req->content->body_size },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 },
40 http_response_size_bytes => {
41 buckets => [5, 50, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000],
42 labels => [qw/worker method code/],
43 234 ▶ cb => sub($c) { $$, $c->req->method, $c->res->code, $c->res->content->body_size },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 },
45 http_requests_total => {
46 labels => [qw/worker method code/],
47 219 ▶ cb => sub($c) { $$, $c->req->method, $c->res->code },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 },
49 perl_collector => {
50 enabled => 1,
51 39 ▶ labels_cb => sub { { worker => $$ } },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 },
53 process_collector => {
54 enabled => 1,
55 20 ▶ labels_cb => sub { { worker => $$ } },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 },
57 }
58 11 };
59  
60 11 ▶sub register($self, $app, $config = {}) {
Subroutine register: called CC=19 SCAR=29.5
Pod documented
61 11 ▶ $self->shm_key($config->{shm_key}) if $config->{shm_key};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"shm_key"} 3 8 11
62  
63 11 for(keys $self->config->%*) {
64 66 ▶ next unless $config->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$_} 65 1 66
65 1 $self->config->{$_} = { $self->config->{$_}->%*, $config->{$_}->%* };
66 }
67  
68 # Present _only_ for a short while for backward compat
69 11 ▶ $self->config->{http_request_duration_seconds}{buckets} = $config->{duration_buckets} if $config->{duration_buckets};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"duration_buckets"} 1 10 11
70 11 ▶ $self->config->{http_request_size_bytes}{buckets} = $config->{request_buckets} if $config->{request_buckets};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"request_buckets"} 1 10 11
71 11 ▶ $self->config->{http_response_size_bytes}{buckets} = $config->{response_buckets} if $config->{response_buckets};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"response_buckets"} 1 10 11
72  
73 # Net::Prometheus instance can be overridden in its entirety
74 11 ▶ $self->prometheus($config->{prometheus}) if $config->{prometheus};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"prometheus"} 1 10 11
75  
76 # Only the two built-in servers are supported for now
77 11 ▶ $app->hook(before_server_start => sub { $self->_start(@_, $config) });
Subroutine __ANON__: called CC=1 SCAR=0.0
78  
79 $self->http_request_duration_seconds(
80 $self->prometheus->new_histogram(
81 namespace => $config->{namespace} // undef,
82 subsystem => $config->{subsystem} // undef,
83 name => "http_request_duration_seconds",
84 help => "Histogram with request processing time",
85 labels => $self->config->{http_request_duration_seconds}{labels},
86 buckets => $self->config->{http_request_duration_seconds}{buckets},
87 )
88 11 ▶ );
Condition75%3/4
$config->{"namespace"} // undef
l!l
110
$config->{"subsystem"} // undef
l!l
011
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
89  
90 $self->http_request_size_bytes(
91 $self->prometheus->new_histogram(
92 namespace => $config->{namespace} // undef,
93 subsystem => $config->{subsystem} // undef,
94 name => "http_request_size_bytes",
95 help => "Histogram containing request sizes",
96 labels => $self->config->{http_request_size_bytes}{labels},
97 buckets => $self->config->{http_request_size_bytes}{buckets},
98 )
99 11 ▶ );
Condition75%3/4
$config->{"namespace"} // undef
l!l
110
$config->{"subsystem"} // undef
l!l
011
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
100  
101 $self->http_response_size_bytes(
102 $self->prometheus->new_histogram(
103 namespace => $config->{namespace} // undef,
104 subsystem => $config->{subsystem} // undef,
105 name => "http_response_size_bytes",
106 help => "Histogram containing response sizes",
107 labels => $self->config->{http_response_size_bytes}{labels},
108 buckets => $self->config->{http_response_size_bytes}{buckets},
109 )
110 11 ▶ );
Condition75%3/4
$config->{"namespace"} // undef
l!l
110
$config->{"subsystem"} // undef
l!l
011
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
111  
112 $self->http_requests_total(
113 $self->prometheus->new_counter(
114 namespace => $config->{namespace} // undef,
115 subsystem => $config->{subsystem} // undef,
116 name => "http_requests_total",
117 help => "How many HTTP requests processed, partitioned by status code and HTTP method.",
118 labels => $self->config->{http_requests_total}{labels},
119 )
120 11 ▶ );
Condition75%3/4
$config->{"namespace"} // undef
l!l
110
$config->{"subsystem"} // undef
l!l
011
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
121  
122 $app->hook(
123 234 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 234 my ($c) = @_;
125 234 $c->stash('prometheus.start_time' => [gettimeofday]);
126 234 $self->http_request_size_bytes->observe($self->config->{http_request_size_bytes}{cb}->($c));
127 }
128 11 );
129  
130 $app->hook(
131 234 ▶ after_render => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 234 my ($c) = @_;
133 234 $self->http_request_duration_seconds->observe($self->config->{http_request_duration_seconds}{cb}->($c));
134 }
135 11 );
136  
137 $app->hook(
138 234 ▶ after_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 234 my ($c) = @_;
140 234 $self->http_requests_total->inc($self->config->{http_requests_total}{cb}->($c));
141 234 $self->http_response_size_bytes->observe($self->config->{http_response_size_bytes}{cb}->($c));
142 }
143 11 );
144  
145 # Create common helper methods
146 11 ▶ $app->helper('prometheus.instance' => sub { $self->prometheus });
Subroutine __ANON__: called CC=1 SCAR=0.0
147 11 $app->helper('prometheus.register' => \&_register);
148  
149 # Plugin-internal helper methods
150 11 $app->helper('prometheus.collect' => \&_collect);
151 11 ▶ $app->helper('prometheus.guard' => sub { $self->guard });
Subroutine __ANON__: called CC=1 SCAR=0.0
152 11 ▶ $app->helper('prometheus.global_collectors' => sub { $self->global_collectors });
Subroutine __ANON__: called CC=1 SCAR=0.0
153  
154 # Create the endpoint that should serve metrics
155 11 ▶ my $prefix = $config->{route} // $app->routes->under('/');
Condition66%2/3
$config->{"route"} // $app->routes->under("/")
l!l&&r!l&&!r
1100
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
156 11 ▶ $self->route($prefix->get($config->{path} // '/metrics'));
Condition100%2/2
$config->{"path"} // "/metrics"
l!l
110
MC/DC100%1/1
$config->{"path"} // "/metrics"
$config->{"path"}
Truth table100%2/2
A
A= $config->{"path"}
Aresult
11
00
157 11 $self->route->to(cb => \&_metrics);
158 }
159  
160 13 ▶sub _metrics($c) {
Subroutine _metrics: called CC=1 SCAR=0.0
161 13 $c->render(text => $c->prometheus->collect, format => 'txt');
162 }
163  
164 22 ▶sub _register($c, $collector, $scope = 'worker') {
Subroutine _register: called CC=3 SCAR=11.0
165 22 ▶ return $c->prometheus->instance->register($collector) if $scope eq 'worker';
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "worker" 21 1 22
166 1 return push $c->prometheus->global_collectors->@*, $collector;
167 }
168  
169 13 ▶sub _collect($c) {
Subroutine _collect: called CC=1 SCAR=0.0
170 # Update stats for current worker
171 13 ▶ $c->prometheus->guard->_change(sub { $_->{$$} = $c->prometheus->instance->render });
Subroutine __ANON__: called CC=1 SCAR=0.0
172  
173 # Fetch stats for all worker-specific collectors
174 13 my $worker_stats = Mojo::Collection->new(keys %{$c->prometheus->guard->_fetch})
175 ->sort
176 13 ▶ ->map(sub { ($c->prometheus->guard->_fetch->{$_}) })
Subroutine __ANON__: called CC=1 SCAR=0.0
177 13 ->join("\n");
178  
179 # Fetch stats for global / on-demand collectors
180 13 my $renderer = Prometheus::MetricRenderer->new;
181 my $global_stats = $c->prometheus->global_collectors
182 1 ▶ ->map(sub { [ $_->collect({}) ] })
Subroutine __ANON__: called CC=1 SCAR=0.0
183 1 ▶ ->map(sub { $renderer->render($_) })
Subroutine __ANON__: called CC=1 SCAR=0.0
184 13 ->join("\n");
185  
186 13 return $worker_stats."\n".$global_stats."\n";
187 }
188  
189 10 ▶sub _share($self) {
Subroutine _share: called CC=2 SCAR=7.6
190 10 ▶ IPC::ShareLite->new(-key => $self->shm_key, -create => 1, -destroy => 0) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
"IPC::ShareLite"->new("-key", $self->shm_key, "-create", 1, "-destroy", 0) or die $! 10 0 10
191 }
192  
193 10 ▶sub _guard($self) {
Subroutine _guard: called CC=1 SCAR=0.0
194 10 Mojolicious::Plugin::Prometheus::Guard->new(share => $self->share);
195 }
196  
197 20 ▶sub _start {
Subroutine _start: called CC=4 SCAR=15.8
198 20 my ($self, $server, $app, $config) = @_;
199 20 ▶ return unless $server->isa('Mojo::Server::Daemon');
Branch: 1 branch
BranchTrueFalseTotal
unless $server->isa("Mojo::Server::Daemon") 0 20 20
200  
201 Mojo::IOLoop->next_tick(
202 20 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
203 20 my $labels = $self->config->{process_collector}{labels_cb}->();
204 20 my $collector = Net::Prometheus::ProcessCollector->new(labels => [%$labels]);
205 20 $app->prometheus->register($collector);
206 }
207 20 ▶ ) if $self->config->{process_collector}{enabled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"process_collector"}{"enabled"} 20 0 20
208  
209 # Remove stopped workers
210 $server->on(
211 0 ▶ reap => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 my ($server, $pid) = @_;
213 0 ▶ $self->guard->_change(sub { delete $_->{$pid} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 }
215 20 ▶ ) if $server->isa('Mojo::Server::Prefork');
Branch: 1 branch
BranchTrueFalseTotal
if $server->isa("Mojo::Server::Prefork") 0 20 20
216 }
217  
218 10 ▶sub _prometheus($self) {
Subroutine _prometheus: called CC=2 SCAR=7.0
219 10 my $prometheus = Net::Prometheus->new(disable_process_collector => 1, disable_perl_collector => 1);
220  
221 # Adding the Perl-collector, if enabled
222 10 ▶ if($self->config->{perl_collector}{enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"perl_collector"}{"enabled"}) 10 0 10
223 10 my $perl_collector = Mojolicious::Plugin::Prometheus::Collector::Perl->new($self->config->{perl_collector});
224 10 $prometheus->register($perl_collector);
225 }
226 10 return $prometheus;
227 };
228  
229 12 1;
230  
231 __END__
↑ Summary Mojolicious/Plugin/Prometheus/Collector/Perl.pm »