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