File Coverage

blib/lib/Net/Prometheus/Summary.pm
Criterion Covered Total %
statement 60 60 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 14 14 100.0
pod 3 3 100.0
total 84 84 100.0


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::Summary 0.16;
7              
8 11     11   151407 use v5.20;
  11         54  
9 11     11   85 use warnings;
  11         22  
  11         642  
10 11     11   62 use base qw( Net::Prometheus::Metric );
  11         18  
  11         1955  
11              
12 11     11   72 use feature qw( postderef signatures );
  11         20  
  11         1537  
13 11     11   70 no warnings qw( experimental::postderef experimental::signatures );
  11         21  
  11         563  
14              
15 11     11   117 use Carp;
  11         68  
  11         1072  
16 11     11   174 use List::Util 1.33 qw( any );
  11         276  
  11         906  
17              
18 11     11   133 use constant _type => "summary";
  11         19  
  11         7595  
19              
20             __PACKAGE__->MAKE_child_class;
21              
22             =head1 NAME
23              
24             C - summarise individual numeric observations
25              
26             =head1 SYNOPSIS
27              
28             =for highlighter language=perl
29              
30             use Net::Prometheus;
31             use Time::HiRes qw( time );
32              
33             my $client = Net::Prometheus->new;
34              
35             my $summary = $client->new_summary(
36             name => "request_seconds",
37             help => "Summary request processing time",
38             );
39              
40             sub handle_request
41             {
42             my $start = time();
43              
44             ...
45              
46             $summary->observe( time() - $start );
47             }
48              
49             =head1 DESCRIPTION
50              
51             This class provides a summary metric - a combination of a running total and a
52             counter, that can be used to report on total and average values of
53             observations, usually times. It is a subclass of L.
54              
55             =cut
56              
57             =head1 CONSTRUCTOR
58              
59             Instances of this class are not usually constructed directly, but instead via
60             the L object that will serve it:
61              
62             $summary = $prometheus->new_summary( %args )
63              
64             This takes the same constructor arguments as documented in
65             L.
66              
67             =cut
68              
69 3         9 sub new ( $class, %opts )
70 3     3 1 247132 {
  3         12  
  3         6  
71 2     2   275 $opts{labels} and any { $_ eq "quantile" } $opts{labels}->@* and
72 3 100 100     32 croak "A Summary may not have a label called 'quantile'";
73              
74 2         22 my $self = $class->SUPER::new( %opts );
75              
76 2         12 $self->{counts} = {};
77 2         8 $self->{sums} = {};
78              
79 2 100       29 if( !$self->labelcount ) {
80 1         4 $self->{counts}{""} = $self->{sums}{""} = 0;
81             }
82              
83 2         11 return $self;
84             }
85              
86             =head2 observe
87              
88             $summary->observe( @label_values, $value )
89             $summary->observe( \%labels, $value )
90              
91             $child->observe( $value )
92              
93             Increment the summary sum by the given value, and the count by 1.
94              
95             =cut
96              
97             __PACKAGE__->MAKE_child_method( 'observe' );
98 4         9 sub _observe_child ( $self, $labelkey, $value )
  4         8  
99 4     4   4 {
  4         5  
  4         7  
100 4         10 $self->{counts}{$labelkey} += 1;
101 4         18 $self->{sums} {$labelkey} += $value;
102             }
103              
104             # remove is generated automatically
105 1         2 sub _remove_child ( $self, $labelkey )
106 1     1   3 {
  1         2  
  1         2  
107 1         4 delete $self->{counts}{$labelkey};
108 1         5 delete $self->{sums}{$labelkey};
109             }
110              
111             sub clear ( $self )
112 1     1 1 1983 {
  1         3  
  1         2  
113 1         4 undef $self->{counts}->%*;
114 1         4 undef $self->{sums}->%*;
115             }
116              
117             sub samples ( $self )
118 5     5 1 8642 {
  5         11  
  5         8  
119 5         10 my $counts = $self->{counts};
120 5         10 my $sums = $self->{sums};
121              
122             return map {
123 5         24 $self->make_sample( count => $_, $counts->{$_} ),
124 7         220 $self->make_sample( sum => $_, $sums->{$_} )
125             } sort keys %$counts;
126             }
127              
128             =head1 AUTHOR
129              
130             Paul Evans
131              
132             =cut
133              
134             0x55AA;