blib/lib/Net/Prometheus/Metric.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 88.4% 23 / 26 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 93.1% 27 / 29 Podpod 75.0% 9 / 12 totaltotal 94.0% 159 / 169 CC 29 SCAR 12.9
CC
29
Cov
96%
labels
9.1
new
9.0
make_sample
4.0
CRAP 3.6
« blib/lib/Net/Prometheus/Histogram.pm ↑ Summary blib/lib/Net/Prometheus.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::Metric 0.16;
7  
8 16 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 our @CARP_NOT = qw( Net::Prometheus );
16  
17 16 ▶use meta 0.009; # GvCVu bugfix
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶no warnings 'meta::experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 16 ▶use Ref::Util qw( is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 16 ▶use Net::Prometheus::Types qw( Sample MetricSamples );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 16 ▶use constant CHILDCLASS => "Net::Prometheus::Metric::_Child";
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 C<Net::Prometheus::Metric> - the base class for observed metrics
29  
30 =head1 DESCRIPTION
31  
32 =for highlighter language=perl
33  
34 This class provides the basic methods shared by the concrete subclasses,
35  
36 =over 2
37  
38 =item *
39  
40 L<Net::Prometheus::Gauge> - a snapshot value-reporting metric
41  
42 =item *
43  
44 L<Net::Prometheus::Counter> - a monotonically-increasing counter metric
45  
46 =item *
47  
48 L<Net::Prometheus::Summary> - summarise individual numeric observations
49  
50 =item *
51  
52 L<Net::Prometheus::Histogram> - count the distribution of numeric observations
53  
54 =back
55  
56 =cut
57  
58 =head1 CONSTRUCTOR
59  
60 =cut
61  
62 =head2 new
63  
64 $metric = Net::Prometheus::Metric->new(
65 name => $name,
66 help => $help,
67 );
68  
69 The constructor is not normally used directly by instrumented code. Instead it
70 is more common to use one of the C<new_*> methods on the containing
71 L<Net::Prometheus> client instance so that the new metric is automatically
72 registered as a collector, and gets exported by the render method.
73  
74 $metric = $prometheus->new_counter(
75 name => $name,
76 help => $help,
77 );
78  
79 In either case, it returns a newly-constructed metric.
80  
81 Takes the following named arguments:
82  
83 =over
84  
85 =item namespace => STR
86  
87 =item subsystem => STR
88  
89 Optional strings giving the namespace and subsystem name parts of the variable
90 name.
91  
92 =item name => STR
93  
94 The basename of the exported variable.
95  
96 =item help => STR
97  
98 Descriptive help text for the variable.
99  
100 =item labels => ARRAY of STR
101  
102 Optional ARRAY reference giving the names of labels for the metric.
103  
104 =back
105  
106 =cut
107  
108 37 ▶sub new ( $class, %args )
Subroutine new: called CC=9 SCAR=22.0
Pod documented
109 {
110 defined $args{name} or
111 37 ▶ croak "Required 'name' argument missing";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"name"} or croak("Required 'name' argument missing") 36 1 37
112 defined $args{help} or
113 36 ▶ croak "Required 'help' argument missing";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"help"} or croak("Required 'help' argument missing") 35 1 36
114  
115 35 my $fullname = join "_", grep { defined } $args{namespace}, $args{subsystem}, $args{name};
116  
117 35 ▶ my $labellist = $args{labels} || [];
Condition100%2/2
$args{'labels'} || []
l!l
1322
MC/DC100%1/1
$args{'labels'} || []
$args{'labels'}
Truth table100%2/2
A
A= $args{'labels'}
Aresult
11
00
118  
119 # See
120 # https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
121 35 ▶ $fullname =~ m/^[a-zA-Z_:][a-zA-Z0-9_:]*$/ or
Branch: 1 branch
BranchTrueFalseTotal
$fullname =~ /^[a-zA-Z_:][a-zA-Z0-9_:]*$/u or croak("Invalid metric name '${fullname}'") 34 1 35
122 croak "Invalid metric name '$fullname'";
123  
124 $_ =~ m/^[a-zA-Z_][a-zA-Z0-9_]*$/ or
125 34 ▶ croak "Invalid label name '$_'" for @$labellist;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u or croak("Invalid label name '${_}'") 14 1 15
126 $_ =~ m/^__/ and
127 33 ▶ croak "Label name '$_' is reserved" for @$labellist;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^__/u and croak("Label name '${_}' is reserved") 1 13 14
128  
129 return bless {
130 fullname => $fullname,
131 help => $args{help},
132 32 labels => $labellist,
133 labelvalues => {},
134 }, $class;
135 }
136  
137 =head1 METHODS
138  
139 =cut
140  
141 =head2 fullname
142  
143 $fullname = $metric->fullname;
144  
145 Returns the full name for the metric. This is formed by joining any of the
146 defined values for C<namespace>, C<subsystem> and C<name> with C<'_'>.
147  
148 =cut
149  
150 182 ▶sub fullname ( $self )
Subroutine fullname: called CC=1 SCAR=0.0
Pod documented
151 {
152 182 return $self->{fullname};
153 }
154  
155 =head2 labelcount
156  
157 $labels = $metric->labelcount;
158  
159 Returns the number of labels defined for this metric.
160  
161 =cut
162  
163 113 ▶sub labelcount ( $self )
Subroutine labelcount: called CC=1 SCAR=0.0
Pod documented
164 {
165 113 return scalar $self->{labels}->@*;
166 }
167  
168 =head2 labels
169  
170 $child = $metric->labels( @values );
171  
172 $child = $metric->labels( { name => $value, name => $value, ... } );
173  
174 Returns a child metric to represent the general one with the given set of
175 labels. The label values may be provided either in a list corresponding to the
176 list of label names given at construction time, or by name in a single HASH
177 reference.
178  
179 The child instance supports the same methods to control the value of the
180 reported metric as the parent metric object, except that any label values are
181 already provided.
182  
183 This object may be cached for efficiency.
184  
185 =cut
186  
187 60 ▶sub labels ( $self, @values )
Subroutine labels: called CC=9 SCAR=22.1
Pod documented
188 {
189 60 ▶ if( @values == 1 and is_hashref( $values[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values == 1 and is_hashref($values[0])) 19 41 60
Condition100%3/3
@values == 1 and is_hashref($values[0])
!ll&&!rl&&r
291219
MC/DC100%2/2
@values == 1 and is_hashref($values[0])
@values == 1 is_hashref($values[0])
Truth table100%3/3
A and B
A= @values == 1B= is_hashref($values[0])
ABresult
0X0
100
111
190 19 my $labels = $self->{labels};
191 19 my $href = $values[0];
192  
193 defined $href->{$_} or croak "No value for $_ label given"
194 19 ▶ for @$labels;
Branch: 1 branch
BranchTrueFalseTotal
defined $href->{$_} or croak("No value for $_ label given") 21 0 21
195  
196 19 @values = $href->@{ @$labels };
197 }
198  
199 60 my $labelcount = $self->labelcount;
200 60 ▶ @values >= $labelcount or
Branch: 1 branch
BranchTrueFalseTotal
@values >= $labelcount or croak("Insufficient values given for labels") 60 0 60
201 croak "Insufficient values given for labels";
202 60 ▶ @values == $labelcount or
Branch: 1 branch
BranchTrueFalseTotal
@values == $labelcount or croak("Too many values given for labels") 60 0 60
203 croak "Too many values given for labels";
204  
205 length $values[$_] or
206 60 ▶ croak "Value for $self->{labels}[$_] may not empty" for 0 .. $#values;
Branch: 1 branch
BranchTrueFalseTotal
length $values[$_] or croak(qq[Value for $self->{"labels"}[$_] may not empty]) 38 1 39
207  
208 my $labelkey = join "\x00", map {
209 # Encode \x00 or \x01 as \x{01}0 or \x{01}1 in order to escape the \x00
210 # but preserve full lexical ordering
211 59 my $value = $_;
212 38 $value =~ s/\x01/\x011/g;
213 38 $value =~ s/\x00/\x010/g;
214 38 $value;
215 } @values;
216  
217 59 $self->{labelvalues}{$labelkey} = \@values;
218  
219 59 return $self->CHILDCLASS->new(
220 $self, $labelkey
221 );
222 }
223  
224 {
225 package
226 Net::Prometheus::Metric::_Child;
227  
228 16 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
229 16 METRIC => 0,
230 LABELKEY => 1,
231 16 };
232  
233 59 ▶ sub new ( $class, $metric, $labelkey )
Subroutine new: called CC=1 SCAR=0.0
234 {
235 59 return bless [ $metric, $labelkey ], $class;
236 }
237  
238 50 ▶ sub metric ( $self ) { $self->[METRIC] }
Subroutine metric: called CC=1 SCAR=0.0
239 59 ▶ sub labelkey ( $self ) { $self->[LABELKEY] }
Subroutine labelkey: called CC=1 SCAR=0.0
240 }
241  
242 # A metaclass method for declaring the child class
243 16 ▶sub MAKE_child_class ( $class )
Subroutine MAKE_child_class: called CC=1 SCAR=0.0
Pod undocumented
244 {
245 # The careful ordering of these two changes should make it possible to
246 # further subclass metrics and metric child classes recursively
247 44 my $childclass_metapkg = meta::get_package( "${class}::_Child" );
248 44 $childclass_metapkg->get_or_add_symbol( '@ISA' )->reference->@* =
249 $class->CHILDCLASS;
250  
251 44 my $class_metapkg = meta::get_package( $class );
252 30 $class_metapkg->add_named_sub(
253 30 ▶ CHILDCLASS => sub ( $ = undef ) { "${class}::_Child" }
Subroutine __ANON__: called CC=2 SCAR=6.9
Pod documented
254 44 );
255  
256 # All Metric subclasses should support ->remove
257 44 $class->MAKE_child_method( 'remove' );
258 }
259  
260 # A metaclass method for declaring what Metric subclass methods are proxied
261 # via child instances
262 121 ▶sub MAKE_child_method ( $class, $method )
Subroutine MAKE_child_method: called CC=1 SCAR=0.0
Pod undocumented
263 {
264 121 my $class_metapkg = meta::get_package( $class );
265  
266 24 ▶ $class_metapkg->add_named_sub( $method => sub ( $self, @args ) {
Subroutine inc: called CC=2 SCAR=6.9
Subroutine __ANON__: called CC=2 SCAR=6.9
Pod documented
267 24 ▶ my @values = splice @args, 0, is_hashref( $args[0] ) ? 1 : $self->labelcount;
Branch: 2 branches
BranchTrueFalseTotal
is_hashref($args[0]) ? : 11 13 24
is_hashref($args[0]) ? : 8 17 25
268  
269 24 $self->labels( @values )->$method( @args );
270 121 } );
271  
272 121 my $childclass_metapkg = meta::get_package( "${class}::_Child" );
273  
274 121 my $childmethod = "_${method}_child";
275 17 ▶ $childclass_metapkg->add_named_sub( $method => sub ( $self, @args ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine set: called CC=1 SCAR=0.0
276 17 $self->metric->$childmethod( $self->labelkey, @args );
277 121 } );
278 }
279  
280 =head2 make_sample
281  
282 $sample = $metric->make_sample( $suffix, $labelkey, $value, $extralabels );
283  
284 Returns a new L<Net::Prometheus::Types/Sample> structure to represent the
285 given value, by expanding the opaque C<$labelkey> value into its actual label
286 names and values and appending the given suffix (which may be an empty string)
287 to the metric's fullname. If provided, the suffix will be separated by an
288 underscore C<'_'>. If provided, C<$extralabels> provides more label names and
289 values to be added to the sample.
290  
291 =cut
292  
293 163 ▶sub make_sample ( $self, $suffix, $labelkey, $value, $extralabels = undef )
Subroutine make_sample: called CC=4 SCAR=13.9
Pod documented
294 163 {
295 163 my $labelnames = $self->{labels};
296 163 my $labelvalues = $self->{labelvalues}{$labelkey};
297  
298 return Sample(
299 ( $suffix ? $self->fullname . "_$suffix" : $self->fullname ),
300 163 ▶ [ ( map { $labelnames->[$_], $labelvalues->[$_] } 0 .. $#$labelnames ), ( $extralabels || [] )->@* ],
Branch: 1 branch
BranchTrueFalseTotal
$suffix ? : 118 45 163
Condition100%2/2
$extralabels || []
l!l
8875
MC/DC100%1/1
$extralabels || []
$extralabels
Truth table100%2/2
A
A= $extralabels
Aresult
11
00
301 $value,
302 );
303 }
304  
305 13 ▶sub collect ( $self, $ )
Subroutine collect: called CC=1 SCAR=0.0
Pod undocumented
306 {
307 return MetricSamples(
308 $self->fullname, $self->_type, $self->{help},
309 13 [ $self->samples ],
310 );
311 }
312  
313 =head2 samples
314  
315 @samples = $metric->samples;
316  
317 An abstract method in this class, this method is intended to be overridden by
318 subclasses.
319  
320 Called during the value collection process, this method should return a list
321 of L<Net::Prometheus::Types/Sample> instances containing the values to report
322 from this metric.
323  
324 =cut
325  
326 0 ▶sub samples ( $self )
Subroutine samples: not called CC=1 SCAR=6.9
Pod documented
327 {
328 0 croak "Abstract Net::Prometheus::Metric->samples invoked directly";
329 }
330  
331 =head2 remove
332  
333 $metric->remove( @values );
334  
335 $metric->remove( { name => $value, name => $value, ... } );
336  
337 I<Since version 0.14.>
338  
339 Removes a single labelset from the metric. This stops it being reported by
340 future calls to L</samples>.
341  
342 =cut
343  
344 # created by MAKE_child_class
345  
346 =head2 clear
347  
348 $metric->clear;
349  
350 I<Since version 0.14.>
351  
352 Removes all the labelsets from the metric, resetting it back to an initial
353 empty state.
354  
355 =cut
356  
357 # must be created by each child class
358 0 ▶sub clear ( $self )
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
359 {
360 0 croak "Abstract Net::Prometheus::Metric->clear invoked directly";
361 }
362  
363 =head1 AUTHOR
364  
365 Paul Evans <leonerd@leonerd.org.uk>
366  
367 =cut
368  
369 16 0x55AA;
« blib/lib/Net/Prometheus/Histogram.pm ↑ Summary blib/lib/Net/Prometheus.pm »