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