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
|
8
|
|
|
8
|
|
67552
|
use strict; |
|
8
|
|
|
|
|
22
|
|
|
8
|
|
|
|
|
229
|
|
9
|
8
|
|
|
8
|
|
46
|
use warnings; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
217
|
|
10
|
8
|
|
|
8
|
|
43
|
use base qw( Net::Prometheus::Metric ); |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
1495
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
13
|
|
|
|
|
|
|
|
14
|
8
|
|
|
8
|
|
59
|
use Carp; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
438
|
|
15
|
|
|
|
|
|
|
|
16
|
8
|
|
|
8
|
|
48
|
use constant _type => "counter"; |
|
8
|
|
|
|
|
25
|
|
|
8
|
|
|
|
|
2246
|
|
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
|
93
|
my $class = shift; |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
19
|
my $self = $class->SUPER::new( @_ ); |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
11
|
$self->{values} = {}; |
68
|
|
|
|
|
|
|
|
69
|
2
|
50
|
|
|
|
10
|
$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
|
|
8
|
my $self = shift; |
94
|
4
|
|
|
|
|
9
|
my ( $labelkey, $delta ) = @_; |
95
|
4
|
100
|
|
|
|
11
|
defined $delta or $delta = 1; |
96
|
4
|
50
|
|
|
|
12
|
$delta >= 0 or |
97
|
|
|
|
|
|
|
croak "Cannot increment a counter by a negative value"; |
98
|
|
|
|
|
|
|
|
99
|
4
|
|
|
|
|
12
|
$self->{values}{$labelkey} += $delta; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub samples |
103
|
|
|
|
|
|
|
{ |
104
|
3
|
|
|
3
|
1
|
496
|
my $self = shift; |
105
|
|
|
|
|
|
|
|
106
|
3
|
|
|
|
|
6
|
my $values = $self->{values}; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
return map { |
109
|
3
|
|
|
|
|
12
|
$self->make_sample( undef, $_, $values->{$_} ) |
|
3
|
|
|
|
|
26
|
|
110
|
|
|
|
|
|
|
} sort keys %$values; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 AUTHOR |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Paul Evans |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
0x55AA; |