blib/lib/Net/Prometheus/Counter.pm

filter: Statementstmt 100% 36 / 36 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 98.2% 56 / 57 CC 5 SCAR 6.3
CC
5
Cov
98%
_inc_child
4.0
new
2.0
BEGIN
1.0
CRAP 1.9
« blib/lib/Net/Prometheus/ProcessCollector.pm ↑ Summary blib/lib/Net/Prometheus/Gauge.pm »
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, 2016-2026 -- leonerd@leonerd.org.uk
5  
6 package Net::Prometheus::Counter 0.16;
7  
8 11 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use base qw( Net::Prometheus::Metric );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use constant _type => "counter";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 __PACKAGE__->MAKE_child_class;
20  
21 =head1 NAME
22  
23 C<Net::Prometheus::Counter> - a monotonically-increasing counter metric
24  
25 =head1 SYNOPSIS
26  
27 =for highlighter language=perl
28  
29 use Net::Prometheus;
30  
31 my $client = Net::Prometheus->new;
32  
33 my $counter = $client->new_counter(
34 name => "requests",
35 help => "Number of received requests",
36 );
37  
38 sub handle_request
39 {
40 $counter->inc;
41 ...
42 }
43  
44 =head1 DESCRIPTION
45  
46 This class provides a counter metric - a value that monotonically increases,
47 usually used to represent occurrences of some event that happens within the
48 instrumented program. It is a subclass of L<Net::Prometheus::Metric>.
49  
50 =cut
51  
52 =head1 CONSTRUCTOR
53  
54 Instances of this class are not usually constructed directly, but instead via
55 the L<Net::Prometheus> object that will serve it:
56  
57 $counter = $prometheus->new_counter( %args );
58  
59 This takes the same constructor arguments as documented in
60 L<Net::Prometheus::Metric>.
61  
62 =cut
63  
64 3 ▶sub new ( $class, %opts )
Subroutine new: called CC=2 SCAR=6.9
Pod documented
65 {
66 3 my $self = $class->SUPER::new( %opts );
67  
68 3 $self->{values} = {};
69  
70 3 ▶ $self->inc( 0 ) if !$self->labelcount;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->labelcount 2 1 3
71  
72 3 return $self;
73 }
74  
75 =head1 METHODS
76  
77 =cut
78  
79 =head2 inc
80  
81 $counter->inc( @label_values, $delta );
82 $counter->inc( \%labels, $delta );
83  
84 $child->inc( $delta );
85  
86 Increment the current value for the gauge. C<$delta> will default to 1 if not
87 supplied and must be non-negative.
88  
89 =cut
90  
91 11 __PACKAGE__->MAKE_child_method( 'inc' );
92 7 ▶sub _inc_child ( $self, $labelkey, $delta = undef )
Subroutine _inc_child: called CC=4 SCAR=13.9
93 7 {
94 7 ▶ defined $delta or $delta = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $delta or $delta = 1 5 2 7
95 7 ▶ $delta >= 0 or
Branch: 1 branch
BranchTrueFalseTotal
$delta >= 0 or croak("Cannot increment a counter by a negative value") 7 0 7
96 croak "Cannot increment a counter by a negative value";
97  
98 7 $self->{values}{$labelkey} += $delta;
99 }
100  
101 # remove is generated automatically
102 1 ▶sub _remove_child ( $self, $labelkey )
Subroutine _remove_child: called CC=1 SCAR=0.0
103 {
104 1 delete $self->{values}{$labelkey};
105 }
106  
107 1 ▶sub clear ( $self )
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
108 {
109 1 undef $self->{values}->%*;
110 }
111  
112 6 ▶sub samples ( $self )
Subroutine samples: called CC=1 SCAR=0.0
Pod documented
113 {
114 6 my $values = $self->{values};
115  
116 return map {
117 6 $self->make_sample( undef, $_, $values->{$_} )
118 } sort keys %$values;
119 }
120  
121 =head1 AUTHOR
122  
123 Paul Evans <leonerd@leonerd.org.uk>
124  
125 =cut
126  
127 11 0x55AA;
« blib/lib/Net/Prometheus/ProcessCollector.pm ↑ Summary blib/lib/Net/Prometheus/Gauge.pm »