| 1 | package PAGI::Middleware::Runtime; | ||||||||||||
| 2 | 1 | $PAGI::Middleware::Runtime::VERSION = '0.002002'; | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Time::HiRes qw(time); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | PAGI::Middleware::Runtime - Request timing middleware | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | use PAGI::Middleware::Builder; | ||||||||||||
| 16 | |||||||||||||
| 17 | my $app = builder { | ||||||||||||
| 18 | enable 'Runtime', | ||||||||||||
| 19 | header => 'X-Runtime', | ||||||||||||
| 20 | precision => 6; | ||||||||||||
| 21 | $my_app; | ||||||||||||
| 22 | }; | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||
| 25 | |||||||||||||
| 26 | PAGI::Middleware::Runtime measures the time taken to process a request | ||||||||||||
| 27 | and adds it as a response header. This is useful for performance | ||||||||||||
| 28 | monitoring and debugging. | ||||||||||||
| 29 | |||||||||||||
| 30 | =head1 CONFIGURATION | ||||||||||||
| 31 | |||||||||||||
| 32 | =over 4 | ||||||||||||
| 33 | |||||||||||||
| 34 | =item * header (default: 'X-Runtime') | ||||||||||||
| 35 | |||||||||||||
| 36 | The header name to use for the runtime value. | ||||||||||||
| 37 | |||||||||||||
| 38 | =item * precision (default: 6) | ||||||||||||
| 39 | |||||||||||||
| 40 | Number of decimal places for the duration in seconds. | ||||||||||||
| 41 | |||||||||||||
| 42 | =back | ||||||||||||
| 43 | |||||||||||||
| 44 | =cut | ||||||||||||
| 45 | |||||||||||||
| 46 | 3 | ▶ | sub _init { | ||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||
| 47 | 3 | my ($self, $config) = @_; | |||||||||||
| 48 | |||||||||||||
| 49 | 3 | ▶ | $self->{header} = $config->{header} // 'X-Runtime'; | ||||||||||
|
Condition100%2/2
$config->{"header"} // "X-Runtime"
MC/DC100%1/1
$config->{"header"} // "X-Runtime"
$config->{"header"}
Truth table100%2/2
A
A= $config->{"header"}
| |||||||||||||
| 50 | 3 | ▶ | $self->{precision} = $config->{precision} // 6; | ||||||||||
|
Condition100%2/2
$config->{"precision"} // 6
MC/DC100%1/1
$config->{"precision"} // 6
$config->{"precision"}
Truth table100%2/2
A
A= $config->{"precision"}
| |||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 3 | ▶ | sub wrap { | ||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 54 | 3 | my ($self, $app) = @_; | |||||||||||
| 55 | |||||||||||||
| 56 | 3 | ▶ | return async sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 57 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||
| 58 | # Only handle HTTP requests | ||||||||||||
| 59 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 1 | await $app->($scope, $receive, $send); | |||||||||||
| 61 | 1 | return; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 2 | my $start_time = time(); | |||||||||||
| 65 | |||||||||||||
| 66 | # Intercept send to add runtime header | ||||||||||||
| 67 | 4 | ▶ | my $wrapped_send = async sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 68 | 4 | my ($event) = @_; | |||||||||||
| 69 | 4 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 2 | my $duration = time() - $start_time; | |||||||||||
| 71 | 2 | my $formatted = sprintf('%.*f', $self->{precision}, $duration); | |||||||||||
| 72 | 2 | push @{$event->{headers}}, [$self->{header}, $formatted]; | |||||||||||
| 73 | } | ||||||||||||
| 74 | 4 | await $send->($event); | |||||||||||
| 75 | 2 | }; | |||||||||||
| 76 | |||||||||||||
| 77 | 2 | await $app->($scope, $receive, $wrapped_send); | |||||||||||
| 78 | 3 | }; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | 1; | |||||||||||
| 82 | |||||||||||||
| 83 | __END__ | ||||||||||||