blib/lib/Metrics/Any/Adapter/Prometheus.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 64.2% 9 / 14 Conditioncond 45.4% 15 / 33 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 100% 24 / 24 Podpod 9.0% 1 / 11 totaltotal 70.0% 119 / 170 CC 23 SCAR 14.6
CC
23
Cov
79%
make_counter
12.2
make_distribution
8.6
make_gauge
3.5
CRAP 4.3
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2020-2026 -- leonerd@leonerd.org.uk
5  
6 package Metrics::Any::Adapter::Prometheus 0.07;
7  
8 3 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use feature qw( signatures postderef );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶no warnings qw( experimental::signatures experimental::postderef );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Net::Prometheus::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use Net::Prometheus::Counter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Net::Prometheus::Gauge;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Net::Prometheus::Histogram 0.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 C<Metrics::Any::Adapter::Prometheus> - a metrics reporting adapter for Prometheus
24  
25 =head1 SYNOPSIS
26  
27 =for highlighter language=perl
28  
29 use Metrics::Any::Adapter 'Prometheus';
30  
31 =head1 DESCRIPTION
32  
33 This L<Metrics::Any> adapter type reports metrics to Prometheus by using
34 L<Net::Prometheus>. Each metric added to the adapter will be registered with
35 the global L<Net::Prometheus::Registry> instance.
36  
37 It becomes the calling program's responsibility to arrange for these to be
38 HTTP accessible by using the C<Net::Prometheus> API.
39  
40 Distribution metrics are exported as Histograms by default. They may
41 alternatively be exported as Summaries in order to generate smaller amounts
42 of export data, by setting the C<use_histograms> import argument to false:
43  
44 use Metrics::Any::Adapter 'Prometheus', use_histograms => 0;
45  
46 Timer metrics are implemented as distribution metrics with the units set to
47 C<seconds>.
48  
49 This adapter type supports batch mode reporting. Callbacks are invoked at the
50 beginning of the C<Net::Prometheus> C<render> method.
51  
52 =cut
53  
54 package Metrics::Any::Adapter::Prometheus::_BatchCollector
55 3 {
56 2 ▶ sub new ( $class )
Subroutine new: called CC=1 SCAR=0.0
57 {
58 2 return bless [], $class;
59 }
60  
61 10 ▶ sub collect ( $self, $ )
Subroutine collect: called CC=2 SCAR=6.9
62 {
63 10 foreach my $cb ( @$self ) { $cb->(); }
64  
65 10 return ();
66 }
67  
68 1 ▶ sub add_callback ( $self, $cb )
Subroutine add_callback: called CC=1 SCAR=0.0
69 {
70 1 push @$self, $cb;
71 }
72 }
73  
74 2 ▶sub new ( $class, %args )
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
75 {
76 my $self = bless {
77 metrics => {},
78 batch_collector => Metrics::Any::Adapter::Prometheus::_BatchCollector->new,
79 2 ▶ use_histograms => $args{use_histograms} // 1,
Condition50%1/2
$args{"use_histograms"} // 1
l!l
02
MC/DC0%0/1
$args{"use_histograms"} // 1
$args{"use_histograms"}
Truth table50%1/2
A
A= $args{"use_histograms"}
Aresult
11
00
80 }, $class;
81  
82 # Need to register this one early before metrics are created, so it runs at
83 # the right time
84 2 Net::Prometheus::Registry->register( $self->{batch_collector} );
85  
86 2 return $self;
87 }
88  
89 =head1 METHODS
90  
91 =cut
92  
93 3 ▶use constant HAVE_BATCH_MODE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 1 ▶sub add_batch_mode_callback ( $self, $cb )
Subroutine add_batch_mode_callback: called CC=1 SCAR=0.0
Pod undocumented
96 {
97 1 $self->{batch_collector}->add_callback( $cb );
98 }
99  
100 8 ▶sub mangle_name ( $self, $name )
Subroutine mangle_name: called CC=2 SCAR=6.9
Pod undocumented
101 {
102 8 ▶ $name = join "_", @$name if ref $name eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $name eq "ARRAY" 7 1 8
103  
104 # TODO: Consider lowercase, squashing unallowed chars to _,...
105  
106 8 return $name;
107 }
108  
109 1 ▶sub make_counter ( $self, $handle, %args )
Subroutine make_counter: called CC=6 SCAR=25.0
Pod undocumented
110 {
111 1 ▶ my $name = $self->mangle_name( delete $args{name} // $handle );
Condition33%1/3
delete $args{"name"} // $handle
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $args{"name"} // $handle
delete $args{"name"} $handle
Truth table33%1/3
A // B
A= delete $args{"name"}B= $handle
ABresult
1X1
011
000
112 1 ▶ my $help = delete $args{description} // "Metrics::Any counter $handle";
Condition50%1/2
delete $args{"description"} // "Metrics::Any counter $handle"
l!l
01
MC/DC0%0/1
delete $args{"description"} // "Metrics::Any counter $handle"
delete $args{"description"}
Truth table50%1/2
A
A= delete $args{"description"}
Aresult
11
00
113  
114 1 ▶ if( my $units = delete $args{units} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $units = delete $args{'units'}) { } 0 1 1
115 # Append _bytes et.al. if required
116 0 ▶ $name .= "_$units" if length $units and $name !~ m/_\Q$units\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if length $units and not $name =~ /_\Q$units\E$/u 0 0 0
Condition0%0/3
length $units and not $name =~ /_\Q$units\E$/u
!ll&&!rl&&r
000
MC/DC0%0/2
length $units and not $name =~ /_\Q$units\E$/u
length $units not $name =~ /_\Q$units\E$/u
Truth table0%0/3
A and not(B)
A= length $unitsB= not $name =~ /_\Q$units\E$/u
ABresult
0X0
100
111
117 }
118 else {
119 # Prometheus policy says unitless counters take _total suffix
120 1 $name .= "_total";
121 }
122  
123 1 $self->{metrics}{$handle} = Net::Prometheus::Registry->register(
124 Net::Prometheus::Counter->new(
125 name => $name,
126 help => $help,
127 %args,
128 )
129 );
130 }
131  
132 2 ▶sub inc_counter_by ( $self, $handle, $amount, @labelvalues )
Subroutine inc_counter_by: called CC=2 SCAR=7.6
Pod undocumented
133 {
134 2 ▶ ( $self->{metrics}{$handle} or croak "No such counter named '$handle'" )
Condition50%1/2
$self->{'metrics'}{$handle} || croak("No such counter named '${handle}'")
l!l
20
MC/DC0%0/1
$self->{'metrics'}{$handle} || croak("No such counter named '${handle}'")
$self->{'metrics'}{$handle}
Truth table50%1/2
A
A= $self->{'metrics'}{$handle}
Aresult
11
00
135 ->inc( @labelvalues, $amount );
136 }
137  
138 =head2 make_distribution
139  
140 $adapter->make_distribution( $name, %args );
141  
142 In addition to the standard arguments, the following are recognised:
143  
144 =over 4
145  
146 =item buckets => ARRAY[ NUM ]
147  
148 If present, overrides the default Histogram bucket sizes.
149  
150 =item bucket_min => NUM
151  
152 =item bucket_max => NUM
153  
154 =item buckets_per_decade => ARRAY[ NUM ]
155  
156 I<Since version 0.04.>
157  
158 A more flexible alternative to specifying literal bucket sizes. The values
159 given in C<buckets_per_decade> are repeated, multiplied by various powers of
160 10 to generate values between C<bucket_min> (or a default of 0.001 if not
161 supplied) and C<bucket_max> (or a default of 1000 if not supplied).
162  
163 For more information, see L<Net::Prometheus::Histogram>.
164  
165 =back
166  
167 =cut
168  
169 3 my %BUCKETS_FOR_UNITS = (
170 bytes => { bucket_min => 100, bucket_max => 1E8 },
171 seconds => undef, # Prometheus defaults are fine
172 );
173  
174 6 ▶sub make_distribution ( $self, $handle, %args )
Subroutine make_distribution: called CC=8 SCAR=21.5
Pod documented
175 {
176 6 ▶ my $name = $self->mangle_name( delete $args{name} // $handle );
Condition33%1/3
delete $args{"name"} // $handle
l!l&&r!l&&!r
600
MC/DC0%0/2
delete $args{"name"} // $handle
delete $args{"name"} $handle
Truth table33%1/3
A // B
A= delete $args{"name"}B= $handle
ABresult
1X1
011
000
177 6 my $units = delete $args{units};
178 6 ▶ my $help = delete $args{description} // "Metrics::Any $units distribution $handle";
Condition100%2/2
delete $args{"description"} // "Metrics::Any $units distribution $handle"
l!l
15
MC/DC100%1/1
delete $args{"description"} // "Metrics::Any $units distribution $handle"
delete $args{"description"}
Truth table100%2/2
A
A= delete $args{"description"}
Aresult
11
00
179  
180 # Append _bytes et.al. if required
181 6 ▶ $name .= "_$units" if length $units and $name !~ m/_\Q$units\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if length $units and not $name =~ /_\Q$units\E$/u 2 4 6
Condition66%2/3
length $units and not $name =~ /_\Q$units\E$/u
!ll&&!rl&&r
402
MC/DC50%1/2
length $units and not $name =~ /_\Q$units\E$/u
length $units not $name =~ /_\Q$units\E$/u
Truth table66%2/3
A and not(B)
A= length $unitsB= not $name =~ /_\Q$units\E$/u
ABresult
0X0
100
111
182  
183 6 ▶ unless( $args{buckets} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"buckets"}) 6 0 6
184 6 ▶ %args = ( $BUCKETS_FOR_UNITS{$units}->%*, %args ) if $BUCKETS_FOR_UNITS{$units};
Branch: 1 branch
BranchTrueFalseTotal
if $BUCKETS_FOR_UNITS{$units} 1 5 6
185 }
186  
187 6 ▶ my $metric_class = $self->{use_histograms} ? "Net::Prometheus::Histogram" :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'use_histograms'} ? : 6 0 6
188 "Net::Prometheus::Summary";
189  
190 6 $self->{metrics}{$handle} = Net::Prometheus::Registry->register(
191 $metric_class->new(
192 name => $name,
193 help => $help,
194 %args,
195 )
196 );
197 }
198  
199 6 ▶sub report_distribution ( $self, $handle, $amount, @labelvalues )
Subroutine report_distribution: called CC=2 SCAR=7.6
Pod undocumented
200 {
201 # TODO: Sanity-check that @labelvalues is as long as the label count
202  
203 6 ▶ ( $self->{metrics}{$handle} or croak "No such distribution named '$handle'" )
Condition50%1/2
$self->{'metrics'}{$handle} || croak("No such distribution named '${handle}'")
l!l
60
MC/DC0%0/1
$self->{'metrics'}{$handle} || croak("No such distribution named '${handle}'")
$self->{'metrics'}{$handle}
Truth table50%1/2
A
A= $self->{'metrics'}{$handle}
Aresult
11
00
204 ->observe( @labelvalues, $amount );
205 }
206  
207 3 *inc_distribution_by = \&report_distribution;
208  
209 1 ▶sub make_gauge ( $self, $handle, %args )
Subroutine make_gauge: called CC=3 SCAR=12.5
Pod undocumented
210 {
211 1 ▶ my $name = $self->mangle_name( delete $args{name} // $handle );
Condition33%1/3
delete $args{"name"} // $handle
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $args{"name"} // $handle
delete $args{"name"} $handle
Truth table33%1/3
A // B
A= delete $args{"name"}B= $handle
ABresult
1X1
011
000
212 1 ▶ my $help = delete $args{description} // "Metrics::Any gauge $handle";
Condition50%1/2
delete $args{"description"} // "Metrics::Any gauge $handle"
l!l
01
MC/DC0%0/1
delete $args{"description"} // "Metrics::Any gauge $handle"
delete $args{"description"}
Truth table50%1/2
A
A= delete $args{"description"}
Aresult
11
00
213  
214 1 $self->{metrics}{$handle} = Net::Prometheus::Registry->register(
215 Net::Prometheus::Gauge->new(
216 name => $name,
217 help => $help,
218 %args,
219 )
220 );
221 }
222  
223 1 ▶sub set_gauge_to ( $self, $handle, $amount, @labelvalues )
Subroutine set_gauge_to: called CC=2 SCAR=7.6
Pod undocumented
224 {
225 1 ▶ ( $self->{metrics}{$handle} or croak "No such gauge named '$handle'" )
Condition50%1/2
$self->{'metrics'}{$handle} || croak("No such gauge named '${handle}'")
l!l
10
MC/DC0%0/1
$self->{'metrics'}{$handle} || croak("No such gauge named '${handle}'")
$self->{'metrics'}{$handle}
Truth table50%1/2
A
A= $self->{'metrics'}{$handle}
Aresult
11
00
226 ->set( @labelvalues, $amount );
227 }
228  
229 1 ▶sub inc_gauge_by ( $self, $handle, $amount, @labelvalues )
Subroutine inc_gauge_by: called CC=2 SCAR=7.6
Pod undocumented
230 {
231 1 ▶ ( $self->{metrics}{$handle} or croak "No such gauge named '$handle'" )
Condition50%1/2
$self->{'metrics'}{$handle} || croak("No such gauge named '${handle}'")
l!l
10
MC/DC0%0/1
$self->{'metrics'}{$handle} || croak("No such gauge named '${handle}'")
$self->{'metrics'}{$handle}
Truth table50%1/2
A
A= $self->{'metrics'}{$handle}
Aresult
11
00
232 ->inc( @labelvalues, $amount );
233 }
234  
235 1 ▶sub make_timer ( $self, $handle, %args )
Subroutine make_timer: called CC=2 SCAR=7.2
Pod undocumented
236 {
237 1 ▶ $args{description} //= "Metrics::Any timer $handle";
Condition50%1/2
$args{"description"} //= "Metrics::Any timer $handle"
l!l
01
MC/DC0%0/1
$args{"description"} //= "Metrics::Any timer $handle"
$args{"description"}
Truth table50%1/2
A
A= $args{"description"}
Aresult
11
00
238  
239 1 return $self->make_distribution( $handle,
240 %args,
241 units => "seconds",
242 );
243 }
244  
245 3 *report_timer = \&report_distribution;
246 3 *inc_timer_by = \&report_distribution;
247  
248 =head1 AUTHOR
249  
250 Paul Evans <leonerd@leonerd.org.uk>
251  
252 =cut
253  
254 3 0x55AA;
↑ Summary