| 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::Gauge 0.16; |
|
7
|
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
238709
|
use v5.20; |
|
|
11
|
|
|
|
|
42
|
|
|
9
|
11
|
|
|
11
|
|
60
|
use warnings; |
|
|
11
|
|
|
|
|
21
|
|
|
|
11
|
|
|
|
|
658
|
|
|
10
|
11
|
|
|
11
|
|
78
|
use base qw( Net::Prometheus::Metric ); |
|
|
11
|
|
|
|
|
19
|
|
|
|
11
|
|
|
|
|
5954
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
11
|
|
|
11
|
|
120
|
use feature qw( postderef signatures ); |
|
|
11
|
|
|
|
|
25
|
|
|
|
11
|
|
|
|
|
1564
|
|
|
13
|
11
|
|
|
11
|
|
72
|
no warnings qw( experimental::postderef experimental::signatures ); |
|
|
11
|
|
|
|
|
22
|
|
|
|
11
|
|
|
|
|
510
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
11
|
|
|
11
|
|
64
|
use Carp; |
|
|
11
|
|
|
|
|
22
|
|
|
|
11
|
|
|
|
|
752
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
11
|
|
|
11
|
|
58
|
use constant _type => "gauge"; |
|
|
11
|
|
|
|
|
20
|
|
|
|
11
|
|
|
|
|
8793
|
|
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->MAKE_child_class; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 NAME |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
C - a snapshot value-reporting 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 $gauge = $client->new_gauge( |
|
34
|
|
|
|
|
|
|
name => "users", |
|
35
|
|
|
|
|
|
|
help => "Number of current users", |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my %users; |
|
39
|
|
|
|
|
|
|
... |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$gauge->set( scalar keys %users ); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This class provides a gauge metric - an arbitrary value that observes some |
|
46
|
|
|
|
|
|
|
snapshot of state at some instant in time. This is often used to report on the |
|
47
|
|
|
|
|
|
|
current usage of resources by the instrumented program, in a way that can |
|
48
|
|
|
|
|
|
|
decrease as well as increase. It is a subclass of L. |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head2 Value-Reporting Functions |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
As an alternative to using the C method to update the value of the gauge, |
|
53
|
|
|
|
|
|
|
a callback function can be used instead which should return the current value |
|
54
|
|
|
|
|
|
|
to report for that gauge. This function is invoked at collection time, meaning |
|
55
|
|
|
|
|
|
|
the reported value is up-to-date. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
These functions are invoked inline as part of the collection process, so they |
|
58
|
|
|
|
|
|
|
should be as small and lightweight as possible. Typical applications involve |
|
59
|
|
|
|
|
|
|
reporting the size of an array or hash within the implementation's code. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$gauge->set_function( sub { scalar @items } ); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$gauge->set_function( sub { scalar keys %things } ); |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Instances of this class are not usually constructed directly, but instead via |
|
70
|
|
|
|
|
|
|
the L object that will serve it: |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
$gauge = $prometheus->new_gauge( %args ); |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
This takes the same constructor arguments as documented in |
|
75
|
|
|
|
|
|
|
L. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
|
78
|
|
|
|
|
|
|
|
|
79
|
13
|
|
|
|
|
30
|
sub new ( $class, %opts ) |
|
80
|
13
|
|
|
13
|
1
|
290849
|
{ |
|
|
13
|
|
|
|
|
44
|
|
|
|
13
|
|
|
|
|
22
|
|
|
81
|
13
|
|
|
|
|
91
|
my $self = $class->SUPER::new( %opts ); |
|
82
|
|
|
|
|
|
|
|
|
83
|
13
|
|
|
|
|
54
|
$self->{values} = {}; |
|
84
|
13
|
|
|
|
|
31
|
$self->{functions} = {}; |
|
85
|
|
|
|
|
|
|
|
|
86
|
13
|
100
|
|
|
|
55
|
$self->inc( 0 ) if !$self->labelcount; |
|
87
|
|
|
|
|
|
|
|
|
88
|
13
|
|
|
|
|
102
|
return $self; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 METHODS |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 set |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$gauge->set( @label_values, $value ); |
|
98
|
|
|
|
|
|
|
$gauge->set( \%labels, $value ); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$child->set( $value ); |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Sets the current value for the gauge. |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
If the gauge has any labels defined, the values for them must be given first. |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__PACKAGE__->MAKE_child_method( 'set' ); |
|
109
|
16
|
|
|
|
|
27
|
sub _set_child ( $self, $labelkey, $value ) |
|
|
16
|
|
|
|
|
76
|
|
|
110
|
16
|
|
|
16
|
|
44
|
{ |
|
|
16
|
|
|
|
|
30
|
|
|
|
16
|
|
|
|
|
37
|
|
|
111
|
16
|
|
|
|
|
90
|
$self->{values}{$labelkey} = $value; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 set_function |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$gauge->set_function( @label_values, $func ); |
|
117
|
|
|
|
|
|
|
$gauge->set_function( \%labels, $func ); |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$child->set_function( $func ); |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Sets a value-returning callback function for the gauge. If the gauge is |
|
122
|
|
|
|
|
|
|
labeled, each label combination requires its own function. |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
When invoked, the function will be passed no arguments and is expected to |
|
125
|
|
|
|
|
|
|
return a single value |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$value = $func->(); |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__PACKAGE__->MAKE_child_method( 'set_function' ); |
|
132
|
2
|
|
|
|
|
5
|
sub _set_function_child ( $self, $labelkey, $func ) |
|
|
2
|
|
|
|
|
4
|
|
|
133
|
2
|
|
|
2
|
|
6
|
{ |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
4
|
|
|
134
|
|
|
|
|
|
|
# Need to store some sort of value so we still iterate on this labelkey |
|
135
|
|
|
|
|
|
|
# during ->samples |
|
136
|
2
|
|
|
|
|
5
|
$self->{values}{$labelkey} = undef; |
|
137
|
2
|
|
|
|
|
10
|
$self->{functions}{$labelkey} = $func; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 inc |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$gauge->inc( @label_values, $delta ); |
|
143
|
|
|
|
|
|
|
$gauge->inc( \%labels, $delta ); |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$child->inc( $delta ); |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Increment the current value for the gauge. C<$delta> will default to 1 if not |
|
148
|
|
|
|
|
|
|
supplied. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
__PACKAGE__->MAKE_child_method( 'inc' ); |
|
153
|
11
|
|
|
|
|
18
|
sub _inc_child ( $self, $labelkey, $delta = undef ) |
|
|
11
|
|
|
|
|
38
|
|
|
154
|
11
|
|
|
11
|
|
20
|
{ |
|
|
11
|
|
|
|
|
22
|
|
|
|
11
|
|
|
|
|
15
|
|
|
155
|
11
|
100
|
|
|
|
34
|
defined $delta or $delta = 1; |
|
156
|
|
|
|
|
|
|
|
|
157
|
11
|
|
|
|
|
47
|
$self->{values}{$labelkey} += $delta; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 dec |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$gauge->dec( @label_values, $delta ); |
|
163
|
|
|
|
|
|
|
$gauge->dec( \%labels, $delta ); |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$child->dec( $delta ); |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Decrement the current value for the gauge. C<$delta> will default to 1 if not |
|
168
|
|
|
|
|
|
|
supplied. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__PACKAGE__->MAKE_child_method( 'dec' ); |
|
173
|
1
|
|
|
|
|
3
|
sub _dec_child ( $self, $labelkey, $delta = undef ) |
|
|
1
|
|
|
|
|
2
|
|
|
174
|
1
|
|
|
1
|
|
3
|
{ |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
1
|
|
|
175
|
1
|
50
|
|
|
|
5
|
defined $delta or $delta = 1; |
|
176
|
|
|
|
|
|
|
|
|
177
|
1
|
|
|
|
|
6
|
$self->{values}{$labelkey} -= $delta; |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# remove is generated automatically |
|
181
|
1
|
|
|
|
|
3
|
sub _remove_child ( $self, $labelkey ) |
|
182
|
1
|
|
|
1
|
|
3
|
{ |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
2
|
|
|
183
|
1
|
|
|
|
|
6
|
delete $self->{values}{$labelkey}; |
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub clear ( $self ) |
|
187
|
1
|
|
|
1
|
1
|
1220
|
{ |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
2
|
|
|
188
|
1
|
|
|
|
|
5
|
undef $self->{values}->%*; |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub samples ( $self ) |
|
192
|
20
|
|
|
20
|
1
|
9365
|
{ |
|
|
20
|
|
|
|
|
80
|
|
|
|
20
|
|
|
|
|
34
|
|
|
193
|
20
|
|
|
|
|
40
|
my $values = $self->{values}; |
|
194
|
20
|
|
|
|
|
43
|
my $functions = $self->{functions}; |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
return map { |
|
197
|
20
|
|
|
|
|
93
|
$self->make_sample( undef, $_, |
|
198
|
27
|
100
|
|
|
|
836
|
$functions->{$_} ? $functions->{$_}->() : $values->{$_} |
|
199
|
|
|
|
|
|
|
) |
|
200
|
|
|
|
|
|
|
} sort keys %$values; |
|
201
|
|
|
|
|
|
|
} |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 AUTHOR |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Paul Evans |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
0x55AA; |