| 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::Histogram 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 => "histogram"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 11 | ▶ | use constant DEFAULT_BUCKETS => [ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 0.005, | ||||||||||||||||||||||||||||
| 22 | 0.01, 0.025, 0.05, 0.075, | ||||||||||||||||||||||||||||
| 23 | 0.1, 0.25, 0.5, 0.75, | ||||||||||||||||||||||||||||
| 24 | 1.0, 2.5, 5.0, 7.5, | ||||||||||||||||||||||||||||
| 25 | 10 | ||||||||||||||||||||||||||||
| 26 | 11 | ]; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 11 | __PACKAGE__->MAKE_child_class; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 NAME | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | C<Net::Prometheus::Histogram> - count the distribution of numeric observations | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =for highlighter language=perl | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | use Net::Prometheus; | ||||||||||||||||||||||||||||
| 39 | use Time::HiRes qw( time ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my $client = Net::Prometheus->new; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my $histogram = $client->new_histogram( | ||||||||||||||||||||||||||||
| 44 | name => "request_seconds", | ||||||||||||||||||||||||||||
| 45 | help => "Summary request processing time", | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub handle_request | ||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | my $start = time(); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ... | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | $summary->observe( time() - $start ); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | This class provides a histogram metric - a count of the distribution of | ||||||||||||||||||||||||||||
| 60 | individual numerical observations into distinct buckets. These are usually | ||||||||||||||||||||||||||||
| 61 | reports of times. It is a subclass of L<Net::Prometheus::Metric>. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Instances of this class are not usually constructed directly, but instead via | ||||||||||||||||||||||||||||
| 68 | the L<Net::Prometheus> object that will serve it: | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | $histogram = $prometheus->new_histogram( %args ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | This takes the same constructor arguments as documented in | ||||||||||||||||||||||||||||
| 73 | L<Net::Prometheus::Metric>, and additionally the following: | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =over | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =item buckets => ARRAY | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | A reference to an ARRAY containing numerical upper bounds for the buckets. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item bucket_min => NUM | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item bucket_max => NUM | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =item buckets_per_decade => ARRAY[ NUM ] | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | I<Since version 0.10.> | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | A more flexible alternative to specifying literal bucket sizes. The values | ||||||||||||||||||||||||||||
| 90 | given in C<buckets_per_decade> are repeated, multiplied by various powers of | ||||||||||||||||||||||||||||
| 91 | 10 to generate values between C<bucket_min> (or a default of 0.001 if not | ||||||||||||||||||||||||||||
| 92 | supplied) and C<bucket_max> (or a default of 1000 if not supplied). | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =back | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | sub new ( $class, %opts ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | 7 | ▶ | if( !$opts{buckets} and grep { m/^bucket/ } keys %opts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $opts{"buckets"} and grep {/^bucket/u;} keys %opts
MC/DC100%2/2
not $opts{"buckets"} and grep {/^bucket/u;} keys %opts
not $opts{"buckets"}
grep {/^bucket/u;} keys %opts
Truth table100%3/3
not(A) and B
A= not $opts{"buckets"}B= grep {/^bucket/u;} keys %opts
| |||||||||||||||||||||||||||||
| 101 | 3 | _gen_buckets( \%opts ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 7 | ▶ | my $buckets = $opts{buckets} || DEFAULT_BUCKETS; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'buckets'} || [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10]
MC/DC100%1/1
$opts{'buckets'} || [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10]
$opts{'buckets'}
Truth table100%2/2
A
A= $opts{'buckets'}
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | $buckets->[$_] > $buckets->[$_-1] or | ||||||||||||||||||||||||||||
| 107 | 7 | ▶ | croak "Histogram bucket limits must be monotonically-increasing" for 1 .. $#$buckets; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | $opts{labels} and any { $_ eq "le" } $opts{labels}->@* and | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 6 | ▶ | croak "A Histogram may not have a label called 'le'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opts{"labels"} and &any(sub {
$_ eq "le";
}
, @{$opts{"labels"}})
MC/DC100%2/2
$opts{"labels"} and &any(sub {
$_ eq "le";
}
, @{$opts{"labels"}})
$opts{"labels"}
&any(sub {
$_ eq "le";
}
, @{$opts{"labels"}})
Truth table100%3/3
A and B
A= $opts{"labels"}B= &any(sub {
$_ eq "le";
}
, @{$opts{"labels"}})
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 5 | my $self = $class->SUPER::new( %opts ); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 5 | $self->{bounds} = [ @$buckets ]; # clone it | |||||||||||||||||||||||||||
| 115 | 5 | $self->{bucketcounts} = {}; | |||||||||||||||||||||||||||
| 116 | 5 | $self->{sums} = {}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | ▶ | if( !$self->labelcount ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 4 | $self->{bucketcounts}{""} = [ ( 0 ) x ( @$buckets + 1 ) ]; | |||||||||||||||||||||||||||
| 120 | 4 | $self->{sums}{""} = 0; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | return $self; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | ▶ | sub _gen_buckets ( $opts ) | ||||||||||||||||||||||||||
|
Subroutine
_gen_buckets: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 3 | ▶ | my $min = $opts->{bucket_min} // 1E-3; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"bucket_min"} // "0.001"
MC/DC0%0/1
$opts->{"bucket_min"} // "0.001"
$opts->{"bucket_min"}
Truth table50%1/2
A
A= $opts->{"bucket_min"}
| |||||||||||||||||||||||||||||
| 129 | 3 | ▶ | my $max = $opts->{bucket_max} // 1E3; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"bucket_max"} // 1000
MC/DC100%1/1
$opts->{"bucket_max"} // 1000
$opts->{"bucket_max"}
Truth table100%2/2
A
A= $opts->{"bucket_max"}
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | my @values_per_decade = ( $opts->{buckets_per_decade} // [ 1 ] )->@*; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"buckets_per_decade"} // [1]
MC/DC100%1/1
$opts->{"buckets_per_decade"} // [1]
$opts->{"buckets_per_decade"}
Truth table100%2/2
A
A= $opts->{"buckets_per_decade"}
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | my $power = 0; | |||||||||||||||||||||||||||
| 134 | 3 | my $value; | |||||||||||||||||||||||||||
| 135 | my @buckets; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 3 | ▶ | while( ( $value = 10 ** $power ) >= $min ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 9 | unshift @buckets, map { $_ * $value } @values_per_decade; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 9 | $power--; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | $power = 1; | |||||||||||||||||||||||||||
| 144 | 3 | ▶ | while( ( $value = 10 ** $power ) <= $max ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 9 | push @buckets, map { $_ * $value } @values_per_decade; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 9 | $power++; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Trim overgenerated ends | ||||||||||||||||||||||||||||
| 151 | 3 | ▶ | @buckets = grep { $min <= $_ and $_ <= $max } @buckets; | ||||||||||||||||||||||||||
|
Condition66%2/3
$min <= $_ && $_ <= $max
MC/DC50%1/2
$min <= $_ && $_ <= $max
$min <= $_
$_ <= $max
Truth table66%2/3
A && B
A= $min <= $_B= $_ <= $max
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 3 | $opts->{buckets} = \@buckets; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 bucket_bounds | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | @bounds = $histogram->bucket_bounds; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Returns the bounding values for each of the buckets, excluding the final | ||||||||||||||||||||||||||||
| 161 | C<+Inf> bucket. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 4 | ▶ | sub bucket_bounds ( $self ) | ||||||||||||||||||||||||||
|
Subroutine
bucket_bounds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 4 | return $self->{bounds}->@*; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 observe | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | $histogram->observe( @label_values, $value ); | ||||||||||||||||||||||||||||
| 173 | $histogram->observe( \%labels, $value ); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $child->observe( $value ); | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Increment the histogram sum by the given value, and each bucket count by 1 | ||||||||||||||||||||||||||||
| 178 | where the value is less than or equal to the bucket upper bound. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 11 | __PACKAGE__->MAKE_child_method( 'observe' ); | |||||||||||||||||||||||||||
| 183 | 5 | ▶ | sub _observe_child ( $self, $labelkey, $value ) | ||||||||||||||||||||||||||
|
Subroutine
_observe_child: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 5 | my $bounds = $self->{bounds}; | |||||||||||||||||||||||||||
| 186 | 5 | ▶ | my $buckets = $self->{bucketcounts}{$labelkey} ||= [ ( 0 ) x ( @$bounds + 1 ) ]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"bucketcounts"}{$labelkey} ||= [(0) x (@$bounds + 1)]
MC/DC100%1/1
$self->{"bucketcounts"}{$labelkey} ||= [(0) x (@$bounds + 1)]
$self->{"bucketcounts"}{$labelkey}
Truth table100%2/2
A
A= $self->{"bucketcounts"}{$labelkey}
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 5 | ▶ | $value <= $bounds->[$_] and $buckets->[$_]++ for 0 .. $#$bounds; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 5 | $buckets->[scalar @$bounds]++; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 5 | $self->{sums}{$labelkey} += $value; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # remove is generated automatically | ||||||||||||||||||||||||||||
| 195 | 1 | ▶ | sub _remove_child ( $self, $labelkey ) | ||||||||||||||||||||||||||
|
Subroutine
_remove_child: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 196 | { | ||||||||||||||||||||||||||||
| 197 | 1 | delete $self->{bucketcounts}{$labelkey}; | |||||||||||||||||||||||||||
| 198 | 1 | delete $self->{sums}{$labelkey}; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | ▶ | sub clear ( $self ) | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | { | ||||||||||||||||||||||||||||
| 203 | 1 | undef $self->{bucketcounts}->%*; | |||||||||||||||||||||||||||
| 204 | 1 | undef $self->{sums}->%*; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 6 | ▶ | sub samples ( $self ) | ||||||||||||||||||||||||||
|
Subroutine
samples: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||
| 209 | 6 | my $bounds = $self->{bounds}; | |||||||||||||||||||||||||||
| 210 | 6 | my $bucketcounts = $self->{bucketcounts}; | |||||||||||||||||||||||||||
| 211 | 6 | my $sums = $self->{sums}; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | return map { | ||||||||||||||||||||||||||||
| 214 | 6 | my $labelkey = $_; | |||||||||||||||||||||||||||
| 215 | 8 | my $buckets = $bucketcounts->{$labelkey}; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | $self->make_sample( count => $labelkey, $buckets->[-1] ), | ||||||||||||||||||||||||||||
| 218 | $self->make_sample( sum => $labelkey, $sums->{$labelkey} ), | ||||||||||||||||||||||||||||
| 219 | ( map { | ||||||||||||||||||||||||||||
| 220 | 8 | $self->make_sample( bucket => $labelkey, $buckets->[$_], [ le => $bounds->[$_] ] ) | |||||||||||||||||||||||||||
| 221 | } 0 .. $#$bounds ), | ||||||||||||||||||||||||||||
| 222 | $self->make_sample( bucket => $labelkey, $buckets->[-1], [ le => "+Inf" ] ); | ||||||||||||||||||||||||||||
| 223 | } sort keys %$sums; | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 11 | 0x55AA; | |||||||||||||||||||||||||||