File Coverage

blib/lib/Conduit/Metrics.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 43 45 95.5


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, 2026 -- leonerd@leonerd.org.uk
5              
6 6     6   130 use v5.36;
  6         56  
7             package Conduit::Metrics 0.03;
8              
9 6     6   46 use experimental 'builtin';
  6         16  
  6         69  
10 6     6   4775 use builtin qw( refaddr );
  6         915  
  6         487  
11              
12             =head1 NAME
13              
14             C - collect metrics on C using L
15              
16             =head1 DESCRIPTION
17              
18             This module contains the implementation of metrics-reporting code from
19             C to provide information about its operation into L.
20              
21             =cut
22              
23 6         48 use Metrics::Any 0.05 '$metrics',
24             strict => 1,
25 6     6   3208 name_prefix => [qw( http server )];
  6         52373  
26              
27 6     6   872 use Time::HiRes qw( gettimeofday tv_interval );
  6         18  
  6         83  
28              
29             $metrics->make_gauge( requests_in_flight =>
30             description => "Count of the number of requests received that have not yet been completed",
31             # no labels
32             );
33             $metrics->make_counter( requests =>
34             description => "Number of HTTP requests received",
35             labels => [qw( method )],
36             );
37             $metrics->make_counter( responses =>
38             description => "Number of HTTP responses served",
39             labels => [qw( method code )],
40             );
41             $metrics->make_timer( request_duration =>
42             description => "Duration of time spent processing requests",
43             # no labels
44             );
45             $metrics->make_distribution( response_bytes =>
46             description => "The size in bytes of responses sent",
47             units => "bytes",
48             # no labels
49             );
50              
51             my %request_started_time;
52              
53             sub received_request ( $, $request )
54 9     9 0 26 {
  9         20  
  9         17  
55 9 100       224 if( $metrics ) {
56 5         98 $request_started_time{refaddr $request} = [ gettimeofday ];
57              
58 5         35 $metrics->inc_gauge( requests_in_flight => );
59              
60 5         5396 $metrics->inc_counter( requests => { method => $request->method } );
61             }
62             }
63              
64 9         19 sub sent_response ( $, $response, $bytes_written )
65 9     9 0 23 {
  9         16  
  9         18  
66 9 100       48 if( $metrics ) {
67 1         25 $metrics->dec_gauge( requests_in_flight => );
68              
69 1         95 my $request = $response->request;
70 1         17 my $started_time = delete $request_started_time{refaddr $request};
71              
72 1         8 $metrics->inc_counter( responses => { method => $request->method, code => $response->code } );
73 1         1208 $metrics->report_timer( request_duration => tv_interval $started_time );
74 1         114 $metrics->report_distribution( response_bytes => $bytes_written );
75             }
76             }
77              
78             =head1 AUTHOR
79              
80             Paul Evans
81              
82             =cut
83              
84             0x55AA;