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
|
10
|
|
|
10
|
|
71293
|
use strict; |
|
10
|
|
|
|
|
27
|
|
|
10
|
|
|
|
|
283
|
|
9
|
10
|
|
|
10
|
|
52
|
use warnings; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
263
|
|
10
|
10
|
|
|
10
|
|
58
|
use base qw( Net::Prometheus::Metric ); |
|
10
|
|
|
|
|
28
|
|
|
10
|
|
|
|
|
1624
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
13
|
|
|
|
|
|
|
|
14
|
10
|
|
|
10
|
|
75
|
use Carp; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
640
|
|
15
|
10
|
|
|
10
|
|
66
|
use List::Util 1.33 qw( any ); |
|
10
|
|
|
|
|
219
|
|
|
10
|
|
|
|
|
665
|
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
10
|
|
81
|
use constant _type => "summary"; |
|
10
|
|
|
|
|
31
|
|
|
10
|
|
|
|
|
3906
|
|
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
|
1392
|
my $class = shift; |
69
|
2
|
|
|
|
|
7
|
my %opts = @_; |
70
|
|
|
|
|
|
|
|
71
|
2
|
100
|
66
|
1
|
|
11
|
$opts{labels} and any { $_ eq "quantile" } @{ $opts{labels} } and |
|
1
|
|
|
|
|
224
|
|
|
1
|
|
|
|
|
8
|
|
72
|
|
|
|
|
|
|
croak "A Summary may not have a label called 'quantile'"; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
8
|
my $self = $class->SUPER::new( @_ ); |
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
6
|
$self->{counts} = {}; |
77
|
1
|
|
|
|
|
2
|
$self->{sums} = {}; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
|
|
|
5
|
if( !$self->labelcount ) { |
80
|
1
|
|
|
|
|
3
|
$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
|
|
1
|
my $self = shift; |
101
|
1
|
|
|
|
|
3
|
my ( $labelkey, $value ) = @_; |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
2
|
$self->{counts}{$labelkey} += 1; |
104
|
1
|
|
|
|
|
3
|
$self->{sums} {$labelkey} += $value; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub samples |
108
|
|
|
|
|
|
|
{ |
109
|
2
|
|
|
2
|
1
|
460
|
my $self = shift; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
4
|
my $counts = $self->{counts}; |
112
|
2
|
|
|
|
|
4
|
my $sums = $self->{sums}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return map { |
115
|
2
|
|
|
|
|
6
|
$self->make_sample( count => $_, $counts->{$_} ), |
116
|
2
|
|
|
|
|
10
|
$self->make_sample( sum => $_, $sums->{$_} ) |
117
|
|
|
|
|
|
|
} sort keys %$counts; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 AUTHOR |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Paul Evans |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
0x55AA; |