blib/lib/Net/Prometheus/Gauge.pm

filter: Statementstmt 100% 46 / 46 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 98.6% 71 / 72 CC 7 SCAR 5.7
CC
7
Cov
98%
_dec_child
3.1
_inc_child
3.0
new
2.0
CRAP 1.8
« blib/lib/Net/Prometheus/Counter.pm ↑ Summary blib/lib/Net/Prometheus/Summary.pm »
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 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use base qw( Net::Prometheus::Metric );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use constant _type => "gauge";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 __PACKAGE__->MAKE_child_class;
20  
21 =head1 NAME
22  
23 C<Net::Prometheus::Gauge> - 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<Net::Prometheus::Metric>.
49  
50 =head2 Value-Reporting Functions
51  
52 As an alternative to using the C<set> 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<Net::Prometheus> 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<Net::Prometheus::Metric>.
76  
77 =cut
78  
79 13 ▶sub new ( $class, %opts )
Subroutine new: called CC=2 SCAR=6.9
Pod documented
80 {
81 13 my $self = $class->SUPER::new( %opts );
82  
83 13 $self->{values} = {};
84 13 $self->{functions} = {};
85  
86 13 ▶ $self->inc( 0 ) if !$self->labelcount;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->labelcount 9 4 13
87  
88 13 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 11 __PACKAGE__->MAKE_child_method( 'set' );
109 16 ▶sub _set_child ( $self, $labelkey, $value )
Subroutine _set_child: called CC=1 SCAR=0.0
110 {
111 16 $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 11 __PACKAGE__->MAKE_child_method( 'set_function' );
132 2 ▶sub _set_function_child ( $self, $labelkey, $func )
Subroutine _set_function_child: called CC=1 SCAR=0.0
133 {
134 # Need to store some sort of value so we still iterate on this labelkey
135 # during ->samples
136 2 $self->{values}{$labelkey} = undef;
137 2 $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 11 __PACKAGE__->MAKE_child_method( 'inc' );
153 11 ▶sub _inc_child ( $self, $labelkey, $delta = undef )
Subroutine _inc_child: called CC=3 SCAR=11.0
154 11 {
155 11 ▶ defined $delta or $delta = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $delta or $delta = 1 10 1 11
156  
157 11 $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 11 __PACKAGE__->MAKE_child_method( 'dec' );
173 1 ▶sub _dec_child ( $self, $labelkey, $delta = undef )
Subroutine _dec_child: called CC=3 SCAR=11.2
174 1 {
175 1 ▶ defined $delta or $delta = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $delta or $delta = 1 1 0 1
176  
177 1 $self->{values}{$labelkey} -= $delta;
178 }
179  
180 # remove is generated automatically
181 1 ▶sub _remove_child ( $self, $labelkey )
Subroutine _remove_child: called CC=1 SCAR=0.0
182 {
183 1 delete $self->{values}{$labelkey};
184 }
185  
186 1 ▶sub clear ( $self )
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
187 {
188 1 undef $self->{values}->%*;
189 }
190  
191 20 ▶sub samples ( $self )
Subroutine samples: called CC=2 SCAR=6.9
Pod documented
192 {
193 20 my $values = $self->{values};
194 20 my $functions = $self->{functions};
195  
196 return map {
197 20 $self->make_sample( undef, $_,
198 27 ▶ $functions->{$_} ? $functions->{$_}->() : $values->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$functions->{$_} ? : 3 24 27
199 )
200 } sort keys %$values;
201 }
202  
203 =head1 AUTHOR
204  
205 Paul Evans <leonerd@leonerd.org.uk>
206  
207 =cut
208  
209 11 0x55AA;
« blib/lib/Net/Prometheus/Counter.pm ↑ Summary blib/lib/Net/Prometheus/Summary.pm »