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
|
11
|
|
|
11
|
|
72568
|
use strict; |
|
11
|
|
|
|
|
32
|
|
|
11
|
|
|
|
|
336
|
|
9
|
11
|
|
|
11
|
|
59
|
use warnings; |
|
11
|
|
|
|
|
24
|
|
|
11
|
|
|
|
|
325
|
|
10
|
11
|
|
|
11
|
|
58
|
use base qw( Net::Prometheus::Metric ); |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
1951
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
13
|
|
|
|
|
|
|
|
14
|
11
|
|
|
11
|
|
77
|
use Carp; |
|
11
|
|
|
|
|
32
|
|
|
11
|
|
|
|
|
636
|
|
15
|
|
|
|
|
|
|
|
16
|
11
|
|
|
11
|
|
85
|
use constant _type => "counter"; |
|
11
|
|
|
|
|
31
|
|
|
11
|
|
|
|
|
3322
|
|
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
|
90
|
my $class = shift; |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
15
|
my $self = $class->SUPER::new( @_ ); |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
11
|
$self->{values} = {}; |
68
|
|
|
|
|
|
|
|
69
|
2
|
50
|
|
|
|
30
|
$self->inc( 0 ) if !$self->labelcount; |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
14
|
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
|
|
9
|
my $self = shift; |
94
|
4
|
|
|
|
|
9
|
my ( $labelkey, $delta ) = @_; |
95
|
4
|
100
|
|
|
|
12
|
defined $delta or $delta = 1; |
96
|
4
|
50
|
|
|
|
11
|
$delta >= 0 or |
97
|
|
|
|
|
|
|
croak "Cannot increment a counter by a negative value"; |
98
|
|
|
|
|
|
|
|
99
|
4
|
|
|
|
|
10
|
$self->{values}{$labelkey} += $delta; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub samples |
103
|
|
|
|
|
|
|
{ |
104
|
3
|
|
|
3
|
1
|
512
|
my $self = shift; |
105
|
|
|
|
|
|
|
|
106
|
3
|
|
|
|
|
7
|
my $values = $self->{values}; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
return map { |
109
|
3
|
|
|
|
|
21
|
$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; |