blib/lib/Mojolicious/Plugin/PrometheusTiny.pm

filter: Statementstmt 100% 52 / 52 Branchbran 73.0% 19 / 26 Conditioncond 80.0% 12 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 87.6% 99 / 113 CC 22 SCAR 15.6
CC
22
Cov
89%
register
8.2
__ANON__
5.5
__ANON__
4.2
CRAP 4.8
↑ Summary
1 package Mojolicious::Plugin::PrometheusTiny;
2 7 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use Time::HiRes qw/gettimeofday tv_interval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Prometheus::Tiny::Shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.02';
7  
8 6 ▶has prometheus => sub {Prometheus::Tiny::Shared->new()};
Subroutine __ANON__: called CC=1 SCAR=0.0
9 7 has route => undef;
10 7 has setup_metrics => undef;
11 7 has update_metrics => undef;
12  
13 6 ▶sub register($self, $app, $config = {}) {
Subroutine register: called CC=8 SCAR=21.1
Pod documented
14 6 ▶ if (my $update = $config->{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $update = $config->{"update"}) 1 5 6
15 1 $self->update_metrics($update);
16 }
17 6 ▶ $config->{worker_label} //= 1;
Condition50%1/2
$config->{"worker_label"} //= 1
l!l
06
MC/DC0%0/1
$config->{"worker_label"} //= 1
$config->{"worker_label"}
Truth table50%1/2
A
A= $config->{"worker_label"}
Aresult
11
00
18 6 ▶ $config->{method_label} //= 1;
Condition50%1/2
$config->{"method_label"} //= 1
l!l
06
MC/DC0%0/1
$config->{"method_label"} //= 1
$config->{"method_label"}
Truth table50%1/2
A
A= $config->{"method_label"}
Aresult
11
00
19 6 ▶ $app->helper(prometheus => sub {$self->prometheus});
Subroutine __ANON__: called CC=1 SCAR=0.0
20 6 $self->_setup($config);
21 6 ▶ if (my $setup = $config->{setup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $setup = $config->{"setup"}) 1 5 6
22 1 $self->setup_metrics($setup);
23 1 $app->$setup($self->prometheus);
24 }
25 6 ▶ my $prefix = $config->{route} // $app->routes->under('/');
Condition66%2/3
$config->{"route"} // $app->routes->under("/")
l!l&&r!l&&!r
150
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
26 6 ▶ $self->route($prefix->get($config->{path} // '/metrics'));
Condition100%2/2
$config->{"path"} // "/metrics"
l!l
15
MC/DC100%1/1
$config->{"path"} // "/metrics"
$config->{"path"}
Truth table100%2/2
A
A= $config->{"path"}
Aresult
11
00
27 6 our $endpoint = $self->route->to_string; # Use direct string comparison instead of $route->match
28 $self->route->to(
29 7 ▶ cb => sub($c) {
Subroutine __ANON__: called CC=2 SCAR=6.9
30 7 ▶ if (my $update = $self->update_metrics) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $update = $self->update_metrics) 2 5 7
31 2 $c->$update($self->prometheus);
32 }
33 $c->render(
34 7 text => $c->prometheus->format,
35 format => 'txt',
36 );
37 6 });
38 $app->hook(
39 210 ▶ before_dispatch => sub($c, @args) {
Subroutine __ANON__: called CC=4 SCAR=14.3
40 210 ▶ return if $c->req->url->path eq $endpoint;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->url->path eq $Mojolicious::Plugin::PrometheusTiny::endpoint 7 203 210
41 203 $c->stash('prometheus.start_time' => [ gettimeofday ]);
42 $self->prometheus->histogram_observe(
43 http_request_size_bytes => $c->req->content->body_size => {
44 $config->{worker_label} ? (worker => $$) : (),
45 203 ▶ $config->{method_label} ? (method => $c->req->method) : (),
Branch: 2 branches
BranchTrueFalseTotal
$config->{'worker_label'} ? : 203 0 203
$config->{'method_label'} ? : 203 0 203
46 },
47 );
48 }
49 6 );
50 $app->hook(
51 210 ▶ after_render => sub($c, @args) {
Subroutine __ANON__: called CC=4 SCAR=14.5
52 210 ▶ return if $c->req->url->path eq $endpoint;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->url->path eq $Mojolicious::Plugin::PrometheusTiny::endpoint 7 203 210
53 $self->prometheus->histogram_observe(
54 http_request_duration_seconds => tv_interval($c->stash('prometheus.start_time')) => {
55 $config->{worker_label} ? (worker => $$) : (),
56 203 ▶ $config->{method_label} ? (method => $c->req->method) : (),
Branch: 2 branches
BranchTrueFalseTotal
$config->{'worker_label'} ? : 203 0 203
$config->{'method_label'} ? : 203 0 203
57 },
58 );
59  
60 }
61 6 );
62 $app->hook(
63 210 ▶ after_dispatch => sub($c, @args) {
Subroutine __ANON__: called CC=5 SCAR=17.1
64 210 ▶ return if $c->req->url->path eq $endpoint;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->url->path eq $Mojolicious::Plugin::PrometheusTiny::endpoint 7 203 210
65 $self->prometheus->inc(http_requests_total => {
66 203 ▶ $config->{worker_label} ? (worker => $$) : (),
Branch: 1 branch
BranchTrueFalseTotal
$config->{'worker_label'} ? : 203 0 203
67 method => $c->req->method,
68 code => $c->res->code,
69 });
70 $self->prometheus->histogram_observe(
71 http_response_size_bytes => $c->res->body_size => {
72 $config->{worker_label} ? (worker => $$) : (),
73 203 ▶ $config->{method_label} ? (method => $c->req->method) : (),
Branch: 2 branches
BranchTrueFalseTotal
$config->{'worker_label'} ? : 203 0 203
$config->{'method_label'} ? : 203 0 203
74 code => $c->res->code,
75 },
76 );
77 }
78 6 );
79  
80 }
81  
82 6 ▶sub _setup($self, $config) {
Subroutine _setup: called CC=4 SCAR=13.9
83 6 my $p = $self->prometheus();
84 6 $p->declare('perl_info', type => 'gauge');
85 6 $p->set(perl_info => 1, { version => $^V });
86 $p->declare('http_request_duration_seconds',
87 help => 'Histogram with request processing time',
88 type => 'histogram',
89 buckets => $config->{duration_buckets}
90 6 ▶ // [ 1 .. 10, 20, 30, 60, 120, 300, 600, 1_200, 3_600, 6_000, 12_000 ],
Condition100%2/2
$config->{"duration_buckets"} // [1..10, 20, 30, 60, 120, 300, 600, 1200, 3600, 6000, 12000]
l!l
15
MC/DC100%1/1
$config->{"duration_buckets"} // [1..10, 20, 30, 60, 120, 300, 600, 1200, 3600, 6000, 12000]
$config->{"duration_buckets"}
Truth table100%2/2
A
A= $config->{"duration_buckets"}
Aresult
11
00
91 );
92 6 $p->declare('http_requests_total',
93 help => 'How many HTTP requests processed, partitioned by status code and HTTP method',
94 type => 'counter',
95 );
96 $p->declare('http_request_size_bytes',
97 help => 'Histogram containing request sizes',
98 type => 'histogram',
99 buckets => $config->{request_buckets}
100 6 ▶ // [ 1, 10, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000 ],
Condition100%2/2
$config->{"request_buckets"} // [1, 10, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
l!l
15
MC/DC100%1/1
$config->{"request_buckets"} // [1, 10, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
$config->{"request_buckets"}
Truth table100%2/2
A
A= $config->{"request_buckets"}
Aresult
11
00
101 );
102 $p->declare('http_response_size_bytes',
103 help => 'Histogram containing response sizes',
104 type => 'histogram',
105 buckets => $config->{response_buckets}
106 6 ▶ // [ 1, 10, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000 ],
Condition100%2/2
$config->{"response_buckets"} // [1, 10, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
l!l
15
MC/DC100%1/1
$config->{"response_buckets"} // [1, 10, 100, 1000, 10000, 50000, 100000, 500000, 1000000]
$config->{"response_buckets"}
Truth table100%2/2
A
A= $config->{"response_buckets"}
Aresult
11
00
107 );
108 }
109  
110 7 1;
111  
112 =encoding utf8
113  
114 =head1 NAME
115  
116 Mojolicious::Plugin::PrometheusTiny - Export metrics using Prometheus::Tiny::Shared
117  
118 =head1 SYNOPSIS
119  
120 # Mojolicious
121 $self->plugin('PrometheusTiny');
122  
123 # Mojolicious::Lite
124 plugin 'PrometheusTiny';
125  
126 # Mojolicious::Lite, with custom response buckets (seconds)
127 plugin 'PrometheusTiny' => { response_buckets => [qw/4 5 6/] };
128  
129 # You can add your own route to do access control
130 my $under = app->routes->under('/secret' =>sub {
131 my $c = shift;
132 return 1 if $c->req->url->to_abs->userinfo eq 'Bender:rocks';
133 $c->res->headers->www_authenticate('Basic');
134 $c->render(text => 'Authentication required!', status => 401);
135 return undef;
136 });
137 plugin PrometheusTiny => {
138 route => $under,
139 # You may declare additional metrics with their own TYPE and HELP...
140 setup => sub($app, $p) {
141 $p->declare('mojo_random',
142 type => 'gauge',
143 help => "Custom prometheus gauge"
144 );
145 },
146 # ...and set up a callback to update them right before exporting them
147 update => sub($c, $p) {
148 $p->set(mojo_random => rand(100));
149 },
150 };
151  
152 =head1 DESCRIPTION
153  
154 L<Mojolicious::Plugin::PrometheusTiny> is a L<Mojolicious> plugin that exports Prometheus metrics from Mojolicious.
155 It's based on L<Mojolicious::Plugin::Prometheus> but uses L<Prometheus::Tiny::Shared> instead of L<Net::Prometheus>.
156  
157 Default hooks are installed to measure requests response time and count requests by HTTP return code,
158 with optional labeling of worker PID and HTTP method.
159 It is easy to add custom metrics and update them right before the metrics are exported.
160  
161 There is no support for namespaces, subsystems or any other fancy Net::Prometheus features.
162  
163 =head1 CODE QUALITY NOTICE
164  
165 This is BETA code =head1 HELPERS
166  
167 =head2 prometheus
168  
169 Create further instrumentation into your application by using this helper which gives access to the
170 L<Prometheus::Tiny::Shared> object.
171 See L<Prometheus::Tiny> for usage.
172  
173 =head1 METHODS
174  
175 L<Mojolicious::Plugin::PrometheusTiny> inherits all methods from
176 L<Mojolicious::Plugin> and implements the following new ones.
177  
178 =head2 register
179  
180 $plugin->register($app, \%config);
181  
182 Register plugin in L<Mojolicious> application.
183  
184 C<%config> can have:
185  
186 =over 2
187  
188 =item * route
189  
190 L<Mojolicious::Routes::Route> object to attach the metrics to, defaults to generating a new one for '/'.
191  
192 Default: /
193  
194 =item * path
195  
196 The path to mount the exporter.
197  
198 Default: /metrics
199  
200 =item * prometheus
201  
202 Override the L<Prometheus::Tiny::Shared> object.
203 The default is a new singleton instance of L<Prometheus::Tiny::Shared>.
204  
205 =item * request_buckets
206  
207 Override buckets for request sizes histogram.
208  
209 Default: C<[ 1, 10, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000 ]>
210  
211 =item * response_buckets
212  
213 Override buckets for response sizes histogram.
214  
215 Default: C<[ 1, 10, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000 ]>
216  
217 =item * duration_buckets
218  
219 Override buckets for request duration setup => sub($app, $p) {
220 $p->declare('mojo_random',
221 type => 'gauge',
222 help => "Custom prometheus gauge"
223 );
224 }histogram.
225  
226 Default: C<[1..10, 20, 30, 60, 120, 300, 600, 1_200, 3_600, 6_000, 12_000]>
227  
228 =item * worker_label
229  
230 Label metrics by worker PID, which might increase significantly the number of Prometheus time series.
231  
232 Default: true
233  
234 =item * method_label
235  
236 Label metrics by HTTP method, which might increase significantly the number of Prometheus time series.
237  
238 Default: true
239  
240 =item * setup
241  
242 Coderef to be executed during setup. Receives as arguments Application and Prometheus instances.
243 Can be used to declare and/or initialize new metrics. Though it is trivial to use $app->prometheus
244 to declare metrics after plugin setup, code is more readable and easier to maintain
245 when actions are listed in their natural order.
246  
247 =item * update
248  
249 Coderef to be executed right before invoking exporter action configured in C<path>.
250 Receives as arguments Controller and Prometheus instances.
251  
252 =back
253  
254 =head1 METRICS
255  
256 This plugin exposes
257  
258 =over 2
259  
260 =item * C<http_requests_total>, request counter partitioned over HTTP method and HTTP response code
261  
262 =item * C<http_request_duration_seconds>, request duration histogram partitioned over HTTP method
263  
264 =item * C<http_request_size_bytes>, request size histogram partitioned over HTTP method
265  
266 =item * C<http_response_size_bytes>, response size histogram partitioned over HTTP method
267  
268 =back
269  
270 =head1 TO DO
271  
272 =over 2
273  
274 =item * Add optional L<Net::Prometheus::ProcessCollector>-like process metrics.
275  
276 =back
277  
278 =head1 AUTHOR
279  
280 Javier Arturo Rodriguez
281  
282 A significant part of this code has been ripped off L<Mojolicious::Plugin::Prometheus> written by Vidar Tyldum
283  
284 =head1 COPYRIGHT AND LICENSE
285  
286 Copyright (c) 2023 by Javier Arturo Rodriguez.
287  
288 This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
289  
290 =head1 SEE ALSO
291  
292 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>,
293 L<Mojolicious::Plugin::Prometheus>, L<Prometheus::Tiny>, L<Prometheus::Tiny::Shared>.
294  
295 =cut
↑ Summary