File Coverage

blib/lib/IO/Async/Metrics.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 19 19 100.0


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, 2020 -- leonerd@leonerd.org.uk
5              
6             package IO::Async::Metrics;
7              
8 100     100   684 use strict;
  100         197  
  100         3084  
9 100     100   502 use warnings;
  100         200  
  100         14057  
10              
11             # Metrics support is entirely optional
12             our $METRICS;
13             eval {
14             require Metrics::Any and
15             Metrics::Any->VERSION( '0.05' ) and
16             Metrics::Any->import( '$METRICS',
17             name_prefix => [qw( io_async )],
18             );
19             };
20              
21             =head1 NAME
22              
23             C - report metrics about C to C
24              
25             =head1 DESCRIPTION
26              
27             This module contains the implementation of metrics-reporting code from
28             C to provide information about its operation into L.
29              
30             =cut
31              
32             sub import
33             {
34 161     161   444 my $class = shift;
35 161         546 my $caller = caller;
36 161         466 my ( $varname ) = @_;
37              
38 161         878 $varname =~ s/^\$//;
39              
40 100     100   770 no strict 'refs';
  100         363  
  100         25061  
41 161         415 *{"${caller}::${varname}"} = \$METRICS;
  161         6746  
42             }
43              
44             =head1 METRICS
45              
46             The following metrics are reported:
47              
48             =head2 io_async_forks
49              
50             A counter giving the number of times that C has been called by the
51             L.
52              
53             =head2 io_async_notifiers
54              
55             A gauge giving the number of L currently registered
56             with the Loop.
57              
58             =head2 io_async_processing
59              
60             A time distribution giving the amount of time spent processing IO events. This
61             time does not include the time spent blocking on the underlying kernel system
62             call to wait for IO events, but only the time spent in userland afterwards to
63             handle them.
64              
65             =head2 io_async_resolver_lookups
66              
67             A labelled counter giving the number of attempted lookups by the
68             L.
69              
70             This metric has one label, C, containing the type of lookup;
71             e.g. C.
72              
73             =head2 io_async_resolver_failures
74              
75             A labelled counter giving the number of Resolver lookups that failed. This is
76             labelled as for C.
77              
78             =head2 io_async_stream_read
79              
80             A counter giving the number of bytes read by L instances.
81             Note that for SSL connections, this will only be able to count bytes of
82             plaintext, not ciphertext, and thus will be a slight under-estimate in this
83             case.
84              
85             =head2 io_async_stream_written
86              
87             A counter giving the number of bytes written by Stream instances. Note again
88             for SSL connections this will only be able to count bytes of plaintext.
89              
90             =cut
91              
92             if( defined $METRICS ) {
93             # Loop metrics
94             $METRICS->make_gauge( notifiers =>
95             description => "Number of IO::Async::Notifiers registered with the Loop",
96             );
97             $METRICS->make_counter( forks =>
98             description => "Number of times IO::Async has fork()ed a process",
99             );
100             $METRICS->make_timer( processing_time =>
101             name => [qw( processing )],
102             description => "Time spent by IO::Async:Loop processing IO",
103             # Override bucket generation
104             bucket_min => 0.001, bucket_max => 1, # 1msec to 1sec
105             buckets_per_decade => [qw( 1 2.5 5 )],
106             );
107             $METRICS->make_gauge( loops =>
108             description => "Count of IO::Async::Loop instances by class",
109             labels => [qw( class )],
110             );
111              
112             # Resolver metrics
113             $METRICS->make_counter( resolver_lookups =>
114             name => [qw( resolver lookups )],
115             description => "Number of IO::Async::Resolver lookups by type",
116             labels => [qw( type )],
117             );
118             $METRICS->make_counter( resolver_failures =>
119             name => [qw( resolver failures )],
120             description => "Number of IO::Async::Resolver lookups that failed by type",
121             labels => [qw( type )],
122             );
123              
124             # Stream metrics
125             $METRICS->make_counter( stream_written =>
126             name => [qw( stream written )],
127             description => "Bytes written by IO::Async::Streams",
128             units => "bytes",
129             );
130             $METRICS->make_counter( stream_read =>
131             name => [qw( stream read )],
132             description => "Bytes read by IO::Async::Streams",
133             units => "bytes",
134             );
135             }
136              
137             =head1 AUTHOR
138              
139             Paul Evans
140              
141             =cut
142              
143             0x55AA;