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