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 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Net::Prometheus::Summary; |
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
71937
|
use strict; |
|
8
|
|
|
|
|
27
|
|
|
8
|
|
|
|
|
223
|
|
9
|
8
|
|
|
8
|
|
43
|
use warnings; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
244
|
|
10
|
8
|
|
|
8
|
|
40
|
use base qw( Net::Prometheus::Metric ); |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
1397
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
13
|
|
|
|
|
|
|
|
14
|
8
|
|
|
8
|
|
61
|
use Carp; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
488
|
|
15
|
8
|
|
|
8
|
|
47
|
use List::Util 1.33 qw( any ); |
|
8
|
|
|
|
|
152
|
|
|
8
|
|
|
|
|
559
|
|
16
|
|
|
|
|
|
|
|
17
|
8
|
|
|
8
|
|
72
|
use constant _type => "summary"; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
2895
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->MAKE_child_class; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 NAME |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
C - summarise individual numeric observations |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use Net::Prometheus; |
28
|
|
|
|
|
|
|
use Time::HiRes qw( time ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $client = Net::Prometheus->new; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $summary = $client->new_summary( |
33
|
|
|
|
|
|
|
name => "request_seconds", |
34
|
|
|
|
|
|
|
help => "Summary request processing time", |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub handle_request |
38
|
|
|
|
|
|
|
{ |
39
|
|
|
|
|
|
|
my $start = time(); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
... |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$summary->observe( time() - $start ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 DESCRIPTION |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
This class provides a summary metric - a combination of a running total and a |
49
|
|
|
|
|
|
|
counter, that can be used to report on total and average values of |
50
|
|
|
|
|
|
|
observations, usually times. It is a subclass of L. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Instances of this class are not usually constructed directly, but instead via |
57
|
|
|
|
|
|
|
the L object that will serve it: |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$summary = $prometheus->new_summary( %args ) |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This takes the same constructor arguments as documented in |
62
|
|
|
|
|
|
|
L. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub new |
67
|
|
|
|
|
|
|
{ |
68
|
2
|
|
|
2
|
1
|
1440
|
my $class = shift; |
69
|
2
|
|
|
|
|
9
|
my %opts = @_; |
70
|
|
|
|
|
|
|
|
71
|
2
|
100
|
66
|
1
|
|
12
|
$opts{labels} and any { $_ eq "quantile" } @{ $opts{labels} } and |
|
1
|
|
|
|
|
233
|
|
|
1
|
|
|
|
|
10
|
|
72
|
|
|
|
|
|
|
croak "A Summary may not have a label called 'quantile'"; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
9
|
my $self = $class->SUPER::new( @_ ); |
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
7
|
$self->{counts} = {}; |
77
|
1
|
|
|
|
|
2
|
$self->{sums} = {}; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
|
|
|
7
|
if( !$self->labelcount ) { |
80
|
1
|
|
|
|
|
4
|
$self->{counts}{""} = $self->{sums}{""} = 0; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
3
|
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
|
|
|
|
|
|
|
sub _observe_child |
99
|
|
|
|
|
|
|
{ |
100
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
101
|
1
|
|
|
|
|
3
|
my ( $labelkey, $value ) = @_; |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
2
|
$self->{counts}{$labelkey} += 1; |
104
|
1
|
|
|
|
|
11
|
$self->{sums} {$labelkey} += $value; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub samples |
108
|
|
|
|
|
|
|
{ |
109
|
2
|
|
|
2
|
1
|
488
|
my $self = shift; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
5
|
my $counts = $self->{counts}; |
112
|
2
|
|
|
|
|
3
|
my $sums = $self->{sums}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return map { |
115
|
2
|
|
|
|
|
8
|
$self->make_sample( count => $_, $counts->{$_} ), |
116
|
2
|
|
|
|
|
11
|
$self->make_sample( sum => $_, $sums->{$_} ) |
117
|
|
|
|
|
|
|
} sort keys %$counts; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 AUTHOR |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Paul Evans |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
0x55AA; |