| 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 | ▶ | 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 | 11 | ▶ | use List::Util 1.33 qw( any ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use constant _type => "summary"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 11 | __PACKAGE__->MAKE_child_class; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | C<Net::Prometheus::Summary> - 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<Net::Prometheus::Metric>. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Instances of this class are not usually constructed directly, but instead via | ||||||||||||||||||||||||||||
| 60 | the L<Net::Prometheus> 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<Net::Prometheus::Metric>. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | sub new ( $class, %opts ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 2 | ▶ | $opts{labels} and any { $_ eq "quantile" } $opts{labels}->@* and | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | croak "A Summary may not have a label called 'quantile'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opts{"labels"} and &any(sub {
$_ eq "quantile";
}
, @{$opts{"labels"}})
MC/DC100%2/2
$opts{"labels"} and &any(sub {
$_ eq "quantile";
}
, @{$opts{"labels"}})
$opts{"labels"}
&any(sub {
$_ eq "quantile";
}
, @{$opts{"labels"}})
Truth table100%3/3
A and B
A= $opts{"labels"}B= &any(sub {
$_ eq "quantile";
}
, @{$opts{"labels"}})
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | my $self = $class->SUPER::new( %opts ); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | $self->{counts} = {}; | |||||||||||||||||||||||||||
| 77 | 2 | $self->{sums} = {}; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | if( !$self->labelcount ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $self->{counts}{""} = $self->{sums}{""} = 0; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | 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 | 11 | __PACKAGE__->MAKE_child_method( 'observe' ); | |||||||||||||||||||||||||||
| 98 | 4 | ▶ | sub _observe_child ( $self, $labelkey, $value ) | ||||||||||||||||||||||||||
|
Subroutine
_observe_child: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | 4 | $self->{counts}{$labelkey} += 1; | |||||||||||||||||||||||||||
| 101 | 4 | $self->{sums} {$labelkey} += $value; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # remove is generated automatically | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub _remove_child ( $self, $labelkey ) | ||||||||||||||||||||||||||
|
Subroutine
_remove_child: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 1 | delete $self->{counts}{$labelkey}; | |||||||||||||||||||||||||||
| 108 | 1 | delete $self->{sums}{$labelkey}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub clear ( $self ) | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 1 | undef $self->{counts}->%*; | |||||||||||||||||||||||||||
| 114 | 1 | undef $self->{sums}->%*; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 5 | ▶ | sub samples ( $self ) | ||||||||||||||||||||||||||
|
Subroutine
samples: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | 5 | my $counts = $self->{counts}; | |||||||||||||||||||||||||||
| 120 | 5 | my $sums = $self->{sums}; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | return map { | ||||||||||||||||||||||||||||
| 123 | 5 | $self->make_sample( count => $_, $counts->{$_} ), | |||||||||||||||||||||||||||
| 124 | 7 | $self->make_sample( sum => $_, $sums->{$_} ) | |||||||||||||||||||||||||||
| 125 | } sort keys %$counts; | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | 0x55AA; | |||||||||||||||||||||||||||