| 1 | package Plack::Middleware::Devel::Agent::Plack; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | Plack::Middleware::Devel::Agent::Plack - Plack Middleware Agent Debugger | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||
| 8 | |||||||||||||
| 9 | PERL5OPT='-d:Agent' plackup --port 8777 -e 'enable "Plack::Middleware::Devel::Agent::Plack";package MyFirstApp;sub {[200,[qw(Content-Type text/plain)],["hello world\n"]]}' | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||
| 12 | |||||||||||||
| 13 | This debugger is handly for tracing all calls made within a psgi app, while excluding the guts of PSGI/Plack. Why? Well most of the time we want to know what is happening in our application, not in the PSGI layer as Plack/PSGI is pretty good at what it does. | ||||||||||||
| 14 | |||||||||||||
| 15 | =cut | ||||||||||||
| 16 | |||||||||||||
| 17 | 0 | ▶ | use Modern::Perl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||
| 18 | |||||||||||||
| 19 | 1 | require Plack::Middleware; | |||||||||||
| 20 | 0 | ▶ | use parent qw( Plack::Middleware ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||
| 21 | 1 | require Plack::Util; | |||||||||||
| 22 | 1 | require Devel::Agent; | |||||||||||
| 23 | 0 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||
| 24 | 0 | ▶ | use Devel::Agent::Util qw(flush_row); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||
| 25 | |||||||||||||
| 26 | =head1 Configuration and env variables | ||||||||||||
| 27 | |||||||||||||
| 28 | At its core this is a configuration class for the debugger provided by Deve::Agent. As a result it can be programatically configigured at runtime. | ||||||||||||
| 29 | |||||||||||||
| 30 | =cut | ||||||||||||
| 31 | |||||||||||||
| 32 | 1 | our @EXCLUDE_DEFAULTS=( | |||||||||||
| 33 | |||||||||||||
| 34 | # grab the defaults from the debugger | ||||||||||||
| 35 | @DB::EXCLUDE_DEFAULTS, | ||||||||||||
| 36 | qw( | ||||||||||||
| 37 | Plack::Util::Prototype | ||||||||||||
| 38 | Pack::Util | ||||||||||||
| 39 | Plack::Util::Accessor | ||||||||||||
| 40 | Plack::Component | ||||||||||||
| 41 | Try::Tiny | ||||||||||||
| 42 | HTTP::Message::PSGI | ||||||||||||
| 43 | ) | ||||||||||||
| 44 | );#,'Plack::Middleware','Plack::Util'); | ||||||||||||
| 45 | 1 | our $VERSION=$Devel::Agent::VERSION; | |||||||||||
| 46 | our %AGENT_OPTIONS=( | ||||||||||||
| 47 | excludes=>{ | ||||||||||||
| 48 | 0 | map { ($_,1) } @EXCLUDE_DEFAULTS | |||||||||||
| 49 | }, | ||||||||||||
| 50 | filter_on_args=>\&default_filter, | ||||||||||||
| 51 | on_frame_end=>\&flush_row, | ||||||||||||
| 52 | ); | ||||||||||||
| 53 | |||||||||||||
| 54 | our %SELF_EXCLUDES=( | ||||||||||||
| 55 | qw( | ||||||||||||
| 56 | Plack::Util::response_cb 1 | ||||||||||||
| 57 | Plack::Util::__ANON__ 1 | ||||||||||||
| 58 | Plack::Util::header_remove 1 | ||||||||||||
| 59 | Plack::Util::inline_object 1 | ||||||||||||
| 60 | |||||||||||||
| 61 | ), | ||||||||||||
| 62 | map { | ||||||||||||
| 63 | 0 | (__PACKAGE__.'::'.$_,1) } | |||||||||||
| 64 | qw( | ||||||||||||
| 65 | _chunk_handler | ||||||||||||
| 66 | _start | ||||||||||||
| 67 | ) | ||||||||||||
| 68 | ); | ||||||||||||
| 69 | |||||||||||||
| 70 | 0 | ▶ | sub default_filter { | ||||||||||
|
Subroutine
default_filter: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 71 | 0 | my ($self,$frame,$args,$caller)=@_; | |||||||||||
| 72 | 0 | ▶ | if(exists $SELF_EXCLUDES{$frame->{class_method}}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 0 | return 0; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | return 1; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | =head2 ENV Options | ||||||||||||
| 80 | |||||||||||||
| 81 | =over 4 | ||||||||||||
| 82 | |||||||||||||
| 83 | =item * PSGI_TRACE_EVERY | ||||||||||||
| 84 | |||||||||||||
| 85 | This is expected to be a number, when set a trace will be implemented every PSGI_TRACE_EVERY number of requests. If not set the default is 1. | ||||||||||||
| 86 | |||||||||||||
| 87 | =back | ||||||||||||
| 88 | |||||||||||||
| 89 | =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | =head2 Class variables | ||||||||||||
| 92 | |||||||||||||
| 93 | This section documents the class variables that can be used to programatically confiigure tracing. They are all accessable as static fully qualified variables via the class path Plack::Middleware::Devel::Agent::Plack | ||||||||||||
| 94 | |||||||||||||
| 95 | =cut | ||||||||||||
| 96 | |||||||||||||
| 97 | =over 4 | ||||||||||||
| 98 | |||||||||||||
| 99 | =item * @EXCLUDE_DEFAULTS | ||||||||||||
| 100 | |||||||||||||
| 101 | This contains the extended default exclusions for plack/psgi | ||||||||||||
| 102 | |||||||||||||
| 103 | =item * %AGENT_OPTIONS | ||||||||||||
| 104 | |||||||||||||
| 105 | This contains the options that will be passed to the constructor of the debuger. | ||||||||||||
| 106 | |||||||||||||
| 107 | Some important notes: | ||||||||||||
| 108 | |||||||||||||
| 109 | excludes=>{...} | ||||||||||||
| 110 | # the excludes are defined from an extended list of classes for PSIG/Plack | ||||||||||||
| 111 | # Defaults are defined in @EXCLUDE_DEFAULTS | ||||||||||||
| 112 | |||||||||||||
| 113 | filter_on_args=>\&default_filter, | ||||||||||||
| 114 | # this filters out some additional classes defined in: %SELF_EXCLUDES | ||||||||||||
| 115 | |||||||||||||
| 116 | on_frame_end=>\&flush_row, | ||||||||||||
| 117 | # this method outputs to STDERR by default | ||||||||||||
| 118 | # it is imported from Devel::Agent::Util | ||||||||||||
| 119 | |||||||||||||
| 120 | =item * $TRACE_EVERY | ||||||||||||
| 121 | |||||||||||||
| 122 | This represents how often a psgi trace will happen, default is 1. | ||||||||||||
| 123 | |||||||||||||
| 124 | =cut | ||||||||||||
| 125 | |||||||||||||
| 126 | 1 | ▶ | our $TRACE_EVERY=$ENV{PSGI_TRACE_EVERY} || 1; | ||||||||||
|
Condition50%1/2
$ENV{'PSGI_TRACE_EVERY'} || 1
MC/DC0%0/1
$ENV{'PSGI_TRACE_EVERY'} || 1
$ENV{'PSGI_TRACE_EVERY'}
Truth table50%1/2
A
A= $ENV{'PSGI_TRACE_EVERY'}
| |||||||||||||
| 127 | 1 | our $ID=0; | |||||||||||
| 128 | |||||||||||||
| 129 | =item * $AGENT | ||||||||||||
| 130 | |||||||||||||
| 131 | This is the debugger instance, it is redefined every time a new trace starts, so make use of it as needed. | ||||||||||||
| 132 | |||||||||||||
| 133 | See: L<Devel::Agent> for more details | ||||||||||||
| 134 | |||||||||||||
| 135 | =cut | ||||||||||||
| 136 | |||||||||||||
| 137 | 0 | our $AGENT; | |||||||||||
| 138 | |||||||||||||
| 139 | =item * $BEFORE_REQUEST=sub {} | ||||||||||||
| 140 | |||||||||||||
| 141 | This is a callback that will be run before any requests objects are run, this can be handy if you want to do soemthing special before the debugger starts. | ||||||||||||
| 142 | |||||||||||||
| 143 | =cut | ||||||||||||
| 144 | |||||||||||||
| 145 | 1 | ▶ | our $BEFORE_REQUEST=sub {}; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||
| 146 | |||||||||||||
| 147 | =item * $BEFORE_TRACE=sub { my ($AGENT,$ID,$env)=@_ } | ||||||||||||
| 148 | |||||||||||||
| 149 | This callback is run before the call to $AGENT->start_trace is called, but after $BEFORE_REQUEST->() | ||||||||||||
| 150 | |||||||||||||
| 151 | =cut | ||||||||||||
| 152 | |||||||||||||
| 153 | 1 | ▶ | our $BEFORE_TRACE=sub {}; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||
| 154 | |||||||||||||
| 155 | =item * $AFTER_TRACE->($AGENT,$ID,$env,$res) | ||||||||||||
| 156 | |||||||||||||
| 157 | This is called when the the request has completed and tracing of your application is done. | ||||||||||||
| 158 | |||||||||||||
| 159 | =back | ||||||||||||
| 160 | |||||||||||||
| 161 | =cut | ||||||||||||
| 162 | |||||||||||||
| 163 | 1 | ▶ | our $AFTER_TRACE=sub {}; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||
| 164 | 0 | our $LAST_REQUEST; | |||||||||||
| 165 | |||||||||||||
| 166 | 0 | ▶ | sub call { | ||||||||||
|
Subroutine
call: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 167 | 0 | my ($self,$env)=@_; | |||||||||||
| 168 | |||||||||||||
| 169 | 0 | $BEFORE_REQUEST->(); | |||||||||||
| 170 | 0 | ++$ID; | |||||||||||
| 171 | 0 | ▶ | if($ID % $TRACE_EVERY==0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 172 | 0 | $AGENT=DB->new(%AGENT_OPTIONS); | |||||||||||
| 173 | 0 | $BEFORE_TRACE->($AGENT,$ID,$env); | |||||||||||
| 174 | 0 | $AGENT->start_trace; | |||||||||||
| 175 | } | ||||||||||||
| 176 | 0 | my $res= $self->app->($env); | |||||||||||
| 177 | 0 | $LAST_REQUEST=[$env,$res]; | |||||||||||
| 178 | |||||||||||||
| 179 | 0 | return Plack::Util::response_cb($res, \&_start); | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 0 | ▶ | sub _start { | ||||||||||
|
Subroutine
_start: not called CC=1 SCAR=6.9
| |||||||||||||
| 183 | 0 | \&_chunk_handler; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | 0 | ▶ | sub _chunk_handler { | ||||||||||
|
Subroutine
_chunk_handler: not called CC=3 SCAR=24.8
| |||||||||||||
| 187 | |||||||||||||
| 188 | 0 | ▶ | unless(defined $_[0]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 189 | 0 | ▶ | return $_[0] unless defined $AGENT; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 190 | 0 | $AGENT->stop_trace; | |||||||||||
| 191 | 0 | $AFTER_TRACE->($AGENT,$ID,$LAST_REQUEST->@*); | |||||||||||
| 192 | 0 | undef $LAST_REQUEST; | |||||||||||
| 193 | } | ||||||||||||
| 194 | |||||||||||||
| 195 | 0 | return $_[0]; | |||||||||||
| 196 | } | ||||||||||||
| 197 | |||||||||||||
| 198 | 1 | 1; | |||||||||||
| 199 | |||||||||||||
| 200 | __END__ | ||||||||||||