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
|
11
|
|
|
11
|
|
79404
|
use strict; |
|
11
|
|
|
|
|
37
|
|
|
11
|
|
|
|
|
388
|
|
9
|
11
|
|
|
11
|
|
58
|
use warnings; |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
343
|
|
10
|
11
|
|
|
11
|
|
78
|
use base qw( Net::Prometheus::Metric ); |
|
11
|
|
|
|
|
34
|
|
|
11
|
|
|
|
|
1808
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
13
|
|
|
|
|
|
|
|
14
|
11
|
|
|
11
|
|
84
|
use Carp; |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
720
|
|
15
|
11
|
|
|
11
|
|
77
|
use List::Util 1.33 qw( any ); |
|
11
|
|
|
|
|
238
|
|
|
11
|
|
|
|
|
782
|
|
16
|
|
|
|
|
|
|
|
17
|
11
|
|
|
11
|
|
93
|
use constant _type => "summary"; |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
4583
|
|
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
|
1471
|
my $class = shift; |
69
|
2
|
|
|
|
|
26
|
my %opts = @_; |
70
|
|
|
|
|
|
|
|
71
|
2
|
100
|
66
|
1
|
|
16
|
$opts{labels} and any { $_ eq "quantile" } @{ $opts{labels} } and |
|
1
|
|
|
|
|
218
|
|
|
1
|
|
|
|
|
23
|
|
72
|
|
|
|
|
|
|
croak "A Summary may not have a label called 'quantile'"; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
14
|
my $self = $class->SUPER::new( @_ ); |
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
6
|
$self->{counts} = {}; |
77
|
1
|
|
|
|
|
3
|
$self->{sums} = {}; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
|
|
|
6
|
if( !$self->labelcount ) { |
80
|
1
|
|
|
|
|
4
|
$self->{counts}{""} = $self->{sums}{""} = 0; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
4
|
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
|
|
3
|
my $self = shift; |
101
|
1
|
|
|
|
|
3
|
my ( $labelkey, $value ) = @_; |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
5
|
$self->{counts}{$labelkey} += 1; |
104
|
1
|
|
|
|
|
4
|
$self->{sums} {$labelkey} += $value; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub samples |
108
|
|
|
|
|
|
|
{ |
109
|
2
|
|
|
2
|
1
|
625
|
my $self = shift; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
5
|
my $counts = $self->{counts}; |
112
|
2
|
|
|
|
|
4
|
my $sums = $self->{sums}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return map { |
115
|
2
|
|
|
|
|
9
|
$self->make_sample( count => $_, $counts->{$_} ), |
116
|
2
|
|
|
|
|
13
|
$self->make_sample( sum => $_, $sums->{$_} ) |
117
|
|
|
|
|
|
|
} sort keys %$counts; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 AUTHOR |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Paul Evans |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
0x55AA; |