File Coverage

blib/lib/Net/Prometheus/Counter.pm
Criterion Covered Total %
statement 51 51 100.0
branch 5 6 83.3
condition n/a
subroutine 12 12 100.0
pod 3 3 100.0
total 71 72 98.6


line stmt bran cond sub pod time code
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     11   242761 use v5.20;
  11         43  
9 11     11   63 use warnings;
  11         45  
  11         708  
10 11     11   68 use base qw( Net::Prometheus::Metric );
  11         19  
  11         2071  
11              
12 11     11   81 use feature qw( postderef signatures );
  11         21  
  11         1595  
13 11     11   92 no warnings qw( experimental::postderef experimental::signatures );
  11         32  
  11         578  
14              
15 11     11   68 use Carp;
  11         20  
  11         884  
16              
17 11     11   76 use constant _type => "counter";
  11         47  
  11         5905  
18              
19             __PACKAGE__->MAKE_child_class;
20              
21             =head1 NAME
22              
23             C - 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.
49              
50             =cut
51              
52             =head1 CONSTRUCTOR
53              
54             Instances of this class are not usually constructed directly, but instead via
55             the L 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.
61              
62             =cut
63              
64 3         7 sub new ( $class, %opts )
65 3     3 1 272840 {
  3         16  
  3         7  
66 3         57 my $self = $class->SUPER::new( %opts );
67              
68 3         22 $self->{values} = {};
69              
70 3 100       21 $self->inc( 0 ) if !$self->labelcount;
71              
72 3         27 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             __PACKAGE__->MAKE_child_method( 'inc' );
92 7         14 sub _inc_child ( $self, $labelkey, $delta = undef )
  7         12  
93 7     7   13 {
  7         13  
  7         10  
94 7 100       31 defined $delta or $delta = 1;
95 7 50       18 $delta >= 0 or
96             croak "Cannot increment a counter by a negative value";
97              
98 7         31 $self->{values}{$labelkey} += $delta;
99             }
100              
101             # remove is generated automatically
102 1         3 sub _remove_child ( $self, $labelkey )
103 1     1   3 {
  1         72  
  1         3  
104 1         6 delete $self->{values}{$labelkey};
105             }
106              
107             sub clear ( $self )
108 1     1 1 2067 {
  1         3  
  1         2  
109 1         5 undef $self->{values}->%*;
110             }
111              
112             sub samples ( $self )
113 6     6 1 8316 {
  6         13  
  6         10  
114 6         12 my $values = $self->{values};
115              
116             return map {
117 6         130 $self->make_sample( undef, $_, $values->{$_} )
  8         262  
118             } sort keys %$values;
119             }
120              
121             =head1 AUTHOR
122              
123             Paul Evans
124              
125             =cut
126              
127             0x55AA;