blib/lib/CGI/Application/Plugin/OpenTracing.pm

filter: Statementstmt 96.8% 306 / 316 Branchbran 80.4% 66 / 82 Conditioncond 73.0% 19 / 26 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 96.6% 58 / 60 Podpod 5.8% 1 / 17 totaltotal 88.3% 457 / 517 CC 56 SCAR 11.1
CC
56
Cov
92%
__ANON__
7.0
_gen_spec
6.0
_gen_tag_processor
5.0
CRAP 3.0
↑ Summary
1 package CGI::Application::Plugin::OpenTracing;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = 'v0.104.2';
7  
8 6 ▶use syntax 'maybe';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use OpenTracing::Implementation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use OpenTracing::GlobalTracer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use HTTP::Status qw( is_server_error :constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Time::HiRes qw( gettimeofday );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use constant CGI_LOAD_TMPL => 'CGI_APPLICATION_LOAD_TMPL';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use constant CGI_REQUEST => 'CGI_APPLICATION_REQUEST';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use constant CGI_RUN => 'CGI_APPLICATION_RUN';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use constant CGI_SETUP => 'CGI_APPLICATION_SETUP';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use constant CGI_TEARDOWN => 'CGI_APPLICATION_TEARDOWN';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use constant TRC_ACTIVE_SCOPE => 'TRC_SCOPEMANAGER_ACTIVE_SCOPE';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 our $implementation_import_name;
27 6 our @implementation_import_opts;
28  
29 6 our $TAG_JOIN_CHAR = ',';
30  
31  
32  
33 ################################################################################
34 #
35 # NOTE: please take a minute to understand the structure of this module
36 #
37 # CGI::Application::Plugin has an interesting design on itself
38 #
39 # within this code base there are three sections:
40 # - import
41 # - callbacks, as defined by CGI::Application
42 # - plugin related methods, that deal with the plugin internals
43 # - tracing specific routines
44 # - cgi related routines, that just work on the CGI::Application only
45 #
46 ################################################################################
47  
48  
49  
50 6 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
51 6 my $package = shift;
52
53 6 ( $implementation_import_name, @implementation_import_opts ) = @_;
54 6 ▶ $ENV{OPENTRACING_DEBUG} && carp "OpenTracing Implementation not defined during import\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $CGI::Application::Plugin::OpenTracing::implementation_import_name 1 5 6
Condition60%3/5
defined $CGI::Application::Plugin::OpenTracing::implementation_import_name || $ENV{'OPENTRACING_DEBUG'} && carp("OpenTracing Implementation not defined during import\n")
l!l
51
$ENV{"OPENTRACING_DEBUG"} and carp("OpenTracing Implementation not defined during import\n")
!ll
100
MC/DC0%0/3
defined $CGI::Application::Plugin::OpenTracing::implementation_import_name || $ENV{'OPENTRACING_DEBUG'} && carp("OpenTracing Implementation not defined during import\n")
defined $CGI::Application::Plugin::OpenTracing::implementation_import_name $ENV{"OPENTRACING_DEBUG"} carp("OpenTracing Implementation not defined during import\n")
Truth table0%0/4
A || B and C
A= defined $CGI::Application::Plugin::OpenTracing::implementation_import_nameB= $ENV{"OPENTRACING_DEBUG"}C= carp("OpenTracing Implementation not defined during import\n")
ABCresult
1XX1
0111
00X0
0100
55 unless defined $implementation_import_name;
56
57 6 my $caller = caller;
58 6 $caller->add_callback( init => \&init );
59 6 $caller->add_callback( prerun => \&prerun );
60 6 $caller->add_callback( postrun => \&postrun );
61 6 $caller->add_callback( load_tmpl => \&load_tmpl );
62 6 $caller->add_callback( teardown => \&teardown );
63 6 $caller->add_callback( error => \&error );
64
65  
66 6 ▶ my $run_glob = do { no strict 'refs'; \*{ $caller . '::run' } };
Subroutine BEGIN: called CC=1 SCAR=0.0
67 6 ▶ my $run_orig
Branch: 1 branch
BranchTrueFalseTotal
defined &$run_glob ? : 0 6 6
68 = defined &$run_glob
69 ? \&run_glob
70 : eval "package $caller;" # SUPER works based on the package it's defined in
71 . 'sub { my $self = shift; $self->SUPER::run(@_) }';
72 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 6 *$run_glob = _wrap_run($run_orig);
74  
75 6 return;
76 }
77  
78  
79  
80 33 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
81 33 my $class = shift;
82 33 my %args = @_;
83
84 33 ▶ my $tracer = delete $args{tracer} // OpenTracing::GlobalTracer->get_global_tracer();
Condition33%1/3
delete $args{"tracer"} // "OpenTracing::GlobalTracer"->get_global_tracer
l!l&&r!l&&!r
0330
MC/DC0%0/2
delete $args{"tracer"} // "OpenTracing::GlobalTracer"->get_global_tracer
delete $args{"tracer"} "OpenTracing::GlobalTracer"->get_global_tracer
Truth table33%1/3
A // B
A= delete $args{"tracer"}B= "OpenTracing::GlobalTracer"->get_global_tracer
ABresult
1X1
011
000
85
86 33 bless {
87 SCOPE => {
88 # one for each callback
89 },
90 TRACER => $tracer,
91 }
92 }
93  
94  
95  
96 ################################################################################
97 #
98 # Callbacks
99 #
100 ################################################################################
101  
102  
103  
104 33 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
105 33 my $cgi_app = shift;
106
107 33 my @bootstrap_options = _app_get_bootstrap_options($cgi_app);
108 33 my $bootstrapped_tracer = _opentracing_init_tracer(@bootstrap_options);
109 # unless OpenTracing::GlobalTracer->is_registered;
110 33 OpenTracing::GlobalTracer->set_global_tracer( $bootstrapped_tracer );
111
112 33 my $plugin = __PACKAGE__->new( );
113 33 $cgi_app->{__PLUGINS}{OPENTRACING} = $plugin;
114
115 33 my $tracer = $plugin->get_tracer();
116 33 my $headers = _cgi_get_http_headers($cgi_app);
117 33 my $context = $tracer->extract_context($headers);
118 33 my %request_tags = _get_request_tags($cgi_app);
119 33 my %query_params = _get_query_params_tags($cgi_app);
120 33 my %form_data = _get_form_data_tags($cgi_app);
121
122 33 $plugin->start_active_span( CGI_REQUEST, child_of => $context );
123 33 $plugin->add_tags( CGI_REQUEST, %request_tags );
124 33 $plugin->add_tags( CGI_REQUEST, %query_params );
125 33 $plugin->add_tags( CGI_REQUEST, %form_data );
126 33 $plugin->start_active_span( CGI_SETUP );
127
128 return
129 33 }
130  
131  
132  
133 33 ▶sub prerun {
Subroutine prerun: called CC=1 SCAR=0.0
Pod undocumented
134 33 my $cgi_app = shift;
135
136 33 my $plugin = _get_plugin($cgi_app);
137
138 33 my %runmode_tags = _get_runmode_tags($cgi_app);
139 33 my %baggage_items = _app_get_baggage_items($cgi_app);
140
141 33 $plugin->add_baggage_items( CGI_SETUP, %baggage_items );
142 33 $plugin->close_scope( CGI_SETUP );
143 33 $plugin->add_baggage_items( CGI_REQUEST, %baggage_items );
144 33 $plugin->add_tags( CGI_REQUEST, %runmode_tags );
145 33 $plugin->start_active_span( CGI_RUN );
146
147 return
148 33 }
149  
150  
151  
152 29 ▶sub postrun {
Subroutine postrun: called CC=1 SCAR=0.0
Pod undocumented
153 29 my $cgi_app = shift;
154
155 29 my $plugin = _get_plugin($cgi_app);
156
157 29 $plugin->close_scope( CGI_RUN );
158 29 $plugin->start_active_span( CGI_TEARDOWN );
159
160 return
161 29 }
162  
163  
164  
165 0 ▶sub load_tmpl {
Subroutine load_tmpl: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my $cgi_app = shift;
167
168 0 my $plugin = _get_plugin($cgi_app);
169
170 0 $plugin->close_scope( CGI_LOAD_TMPL );
171
172 return
173 0 }
174  
175  
176  
177 29 ▶sub teardown {
Subroutine teardown: called CC=2 SCAR=6.9
Pod documented
178 29 my $cgi_app = shift;
179
180 29 my $plugin = _get_plugin($cgi_app);
181
182 29 my %http_status_tags = _get_http_status_tags($cgi_app);
183 29 ▶ my $error = 1
Branch: 1 branch
BranchTrueFalseTotal
if &is_server_error([_cgi_get_header_status($cgi_app)]->[0]) 1 28 29
184 if is_server_error([_cgi_get_header_status($cgi_app)]->[0]);
185
186 29 $plugin->close_scope( CGI_TEARDOWN );
187 29 $plugin->add_tags( CGI_REQUEST, %http_status_tags );
188 29 $plugin->add_tags( CGI_REQUEST, maybe error => $error );
189 29 $plugin->close_scope( CGI_REQUEST );
190
191 return
192 29 }
193  
194  
195  
196 7 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod undocumented
197 7 my ($cgi_app, $error) = @_;
198
199 7 my $plugin = _get_plugin($cgi_app);
200
201 7 ▶ return if not $cgi_app->error_mode(); # we're dying
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi_app->error_mode 3 4 7
202
203 4 $plugin->add_tags(TRC_ACTIVE_SCOPE,
204 error => 1,
205 message => $error,
206 grep_error_tags( $plugin->get_tags(TRC_ACTIVE_SCOPE) ),
207 );
208
209 # run span should continue
210 4 my $root = $plugin->get_scope(CGI_RUN)->get_span;
211
212 4 my $tracer = $plugin->get_tracer();
213 4 _cascade_set_failed_spans($tracer, $error, $root);
214
215 4 return;
216 }
217  
218  
219  
220 ################################################################################
221 #
222 # Plugin methods - These do not require the CGI-App
223 #
224 ################################################################################
225  
226  
227  
228 0 ▶sub set_tracer {
Subroutine set_tracer: not called CC=1 SCAR=6.9
Pod undocumented
229 0 my $plugin = shift;
230 0 my $tracer = shift;
231
232 0 $plugin->{TRACER} = $tracer;
233 }
234  
235 189 ▶sub get_tracer {
Subroutine get_tracer: called CC=1 SCAR=0.0
Pod undocumented
236 189 my $plugin = shift;
237
238 return $plugin->{TRACER}
239 189 }
240  
241 128 ▶sub start_active_span {
Subroutine start_active_span: called CC=1 SCAR=0.0
Pod undocumented
242 128 my $plugin = shift;
243 128 my $scope_name = shift;
244 128 my %params = @_;
245
246 128 my $operation_name = lc $scope_name;
247
248 128 my $tracer = $plugin->get_tracer();
249 128 my $scope = $tracer->start_active_span( $operation_name, %params );
250
251 128 $plugin->{SCOPE}{$scope_name} = $scope;
252 }
253  
254 202 ▶sub add_tags {
Subroutine add_tags: called CC=2 SCAR=7.0
Pod undocumented
255 202 my $plugin = shift;
256 202 my $scope_name = shift;
257 202 my %tags = @_;
258
259 202 my $span = $plugin->get_span($scope_name);
260 202 ▶ $span && $span->add_tags(%tags);
Branch: 1 branch
BranchTrueFalseTotal
$span and $span->add_tags(%tags) 202 0 202
261 }
262  
263 12 ▶sub get_tags {
Subroutine get_tags: called CC=3 SCAR=11.4
Pod undocumented
264 12 my $plugin = shift;
265 12 my $scope_name = shift;
266
267 12 ▶ my $span = $plugin->get_span($scope_name) or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $span = $plugin->get_span($scope_name) or return () 12 0 12
268 12 ▶ $span && $span->get_tags();
Branch: 1 branch
BranchTrueFalseTotal
$span and $span->get_tags 12 0 12
269 }
270  
271 66 ▶sub add_baggage_items {
Subroutine add_baggage_items: called CC=2 SCAR=7.0
Pod undocumented
272 66 my $plugin = shift;
273 66 my $scope_name = shift;
274 66 my %baggage_items = @_;
275
276 66 my $span = $plugin->get_span($scope_name);
277 66 ▶ $span && $span->add_baggage_items( %baggage_items );
Branch: 1 branch
BranchTrueFalseTotal
$span and $span->add_baggage_items(%baggage_items) 66 0 66
278 }
279  
280 120 ▶sub close_scope {
Subroutine close_scope: called CC=2 SCAR=7.0
Pod undocumented
281 120 my $plugin = shift;
282 120 my $scope_name = shift;
283
284 120 my $scope = $plugin->get_scope($scope_name);
285 120 ▶ $scope && $scope->close;
Branch: 1 branch
BranchTrueFalseTotal
$scope and $scope->close 120 0 120
286 }
287  
288 280 ▶sub get_span {
Subroutine get_span: called CC=2 SCAR=7.0
Pod undocumented
289 280 my $plugin = shift;
290 280 my $scope_name = shift;
291
292 280 my $scope = $plugin->get_scope($scope_name);
293 280 ▶ $scope && $scope->get_span;
Branch: 1 branch
BranchTrueFalseTotal
$scope and $scope->get_span 280 0 280
294 }
295  
296 404 ▶sub get_scope {
Subroutine get_scope: called CC=2 SCAR=6.9
Pod undocumented
297 404 my $plugin = shift;
298 404 my $scope_name = shift;
299
300 404 ▶ return $plugin->get_tracer()->get_scope_manager->get_active_scope()
Branch: 1 branch
BranchTrueFalseTotal
if $scope_name eq "TRC_SCOPEMANAGER_ACTIVE_SCOPE" 20 384 404
301 if $scope_name eq TRC_ACTIVE_SCOPE;
302
303 384 return $plugin->{SCOPE}{$scope_name};
304 }
305  
306  
307  
308 ################################################################################
309 #
310 # OpenTracing
311 #
312 ################################################################################
313  
314  
315  
316 33 ▶sub _opentracing_init_tracer {
Subroutine _opentracing_init_tracer: called CC=2 SCAR=7.1
317 33 my @bootstrap_options = @_;
318
319 33 ▶ my $bootstrapped_tracer =
Branch: 1 branch
BranchTrueFalseTotal
$CGI::Application::Plugin::OpenTracing::implementation_import_name ? : 33 0 33
320 $implementation_import_name ?
321 OpenTracing::Implementation->bootstrap_tracer(
322 $implementation_import_name,
323 @implementation_import_opts,
324 @bootstrap_options,
325 )
326 :
327 OpenTracing::Implementation->bootstrap_default_tracer(
328 @implementation_import_opts,
329 @bootstrap_options,
330 )
331 ;
332
333 33 return $bootstrapped_tracer;
334 }
335  
336  
337  
338 8 ▶sub _cascade_set_failed_spans {
Subroutine _cascade_set_failed_spans: called CC=5 SCAR=16.1
339 8 my ($tracer, $error, $root_span) = @_;
340 8 ▶ my $root_addr = refaddr($root_span) if defined $root_span;
Branch: 1 branch
BranchTrueFalseTotal
if defined $root_span 4 4 8
341  
342 8 ▶ while (my $scope = $tracer->get_scope_manager->get_active_scope()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scope = $tracer->get_scope_manager->get_active_scope) 20 4 24
343 20 my $span = $scope->get_span();
344 20 ▶ last if defined $root_addr and $root_addr eq refaddr($span);
Branch: 1 branch
BranchTrueFalseTotal
if defined $root_addr and $root_addr eq builtin::refaddr($span) 4 16 20
Condition100%3/3
defined $root_addr and $root_addr eq builtin::refaddr($span)
!ll&&!rl&&r
1154
MC/DC100%2/2
defined $root_addr and $root_addr eq builtin::refaddr($span)
defined $root_addr $root_addr eq builtin::refaddr($span)
Truth table100%3/3
A and B
A= defined $root_addrB= $root_addr eq builtin::refaddr($span)
ABresult
0X0
100
111
345
346 # $span->add_tags(error => 1, message => $error);
347 16 $scope->close();
348 }
349 8 return;
350 }
351  
352  
353  
354 12 ▶sub grep_error_tags {
Subroutine grep_error_tags: called CC=1 SCAR=0.0
Pod undocumented
355 12 my %tags = @_;
356
357 return (
358 maybe 'error' => $tags{'error'},
359 maybe 'message' => $tags{'message'},
360 12 maybe 'error.kind' => $tags{'error.kind'},
361 )
362 }
363  
364  
365 ################################################################################
366 #
367 # CGI – purely CGI related
368 #
369 ################################################################################
370  
371  
372  
373 33 ▶sub _cgi_get_run_mode {
Subroutine _cgi_get_run_mode: called CC=1 SCAR=0.0
374 33 my $cgi_app = shift;
375
376 33 my $run_mode = $cgi_app->get_current_runmode();
377
378 33 return $run_mode
379 }
380  
381  
382  
383 33 ▶sub _cgi_get_run_method {
Subroutine _cgi_get_run_method: called CC=1 SCAR=0.0
384 33 my $cgi_app = shift;
385
386 33 my $run_mode = $cgi_app->get_current_runmode();
387 33 my $run_methode = { $cgi_app->run_modes }->{ $run_mode };
388
389 33 return $run_methode
390 }
391  
392  
393 62 ▶sub _cgi_get_header_status {
Subroutine _cgi_get_header_status: called CC=4 SCAR=13.9
394 62 my $cgi_app = shift;
395  
396 62 my %headers = $cgi_app->header_props();
397 62 my $status = $headers{-status};
398
399 62 ▶ return $status
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 62 62
400 unless wantarray;
401
402 62 ▶ my $status_code = [ ( $status // '' ) =~ /^\s*(\d{3})/ ]->[0];
Condition100%2/2
$status // ""
l!l
1844
MC/DC100%1/1
$status // ""
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
403 62 ▶ my $status_mess = [ ( $status // '' ) =~ /^\s*\d{3}\s*(.+)\s*$/ ]->[0];
Condition100%2/2
$status // ""
l!l
1844
MC/DC100%1/1
$status // ""
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
404
405 62 return ($status_code, $status_mess);
406 }
407  
408  
409  
410 ################################################################################
411 #
412 # CGI Query – operates indirectly on CGI->Query
413 #
414 ################################################################################
415  
416  
417  
418 33 ▶sub _cgi_get_query_http_method {
Subroutine _cgi_get_query_http_method: called CC=1 SCAR=0.0
419 33 my $cgi_app = shift;
420
421 33 my $query = $cgi_app->query();
422
423 33 return $query->request_method();
424 }
425  
426  
427 33 ▶sub _cgi_get_http_headers {
Subroutine _cgi_get_http_headers: called CC=1 SCAR=0.0
428 33 my $cgi_app = shift;
429  
430 33 my $query = $cgi_app->query();
431  
432 33 HTTP::Headers->new(map { s/^HTTP_//r => $query->http($_) } $query->http);
433 }
434  
435 33 ▶sub _cgi_get_query_http_url {
Subroutine _cgi_get_query_http_url: called CC=1 SCAR=0.0
436 33 my $cgi_app = shift;
437
438 33 my $query = $cgi_app->query();
439
440 33 return $query->url(-path => 1);
441 }
442  
443  
444  
445 33 ▶sub _cgi_get_query_content_type_is_form {
Subroutine _cgi_get_query_content_type_is_form: called CC=4 SCAR=14.3
446 33 my $cgi_app = shift;
447
448 33 my $query = $cgi_app->query();
449
450 33 my $content_type = $query->content_type();
451
452 33 ▶ return if not defined $content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_type 22 11 33
453 11 ▶ return 1 if $content_type =~ m{\Amultipart/form-data};
Branch: 1 branch
BranchTrueFalseTotal
if $content_type =~ m[\Amultipart/form-data] 0 11 11
454 11 ▶ return 1 if $content_type =~ m{\Aapplication/x-www-form-urlencoded};
Branch: 1 branch
BranchTrueFalseTotal
if $content_type =~ m[\Aapplication/x-www-form-urlencoded] 11 0 11
455 0 return;
456 }
457  
458  
459  
460 ################################################################################
461 #
462 # Tags - getting the various tags for the request span
463 #
464 ################################################################################
465  
466  
467  
468 33 ▶sub _get_request_tags {
Subroutine _get_request_tags: called CC=1 SCAR=0.0
469 33 my $cgi_app = shift;
470
471 33 my %tags = (
472 'component' => 'CGI::Application',
473 maybe 'http.method' => _cgi_get_query_http_method($cgi_app),
474 maybe 'http.url' => _cgi_get_query_http_url($cgi_app),
475 );
476
477  
478 33 return %tags
479 }
480  
481  
482  
483 33 ▶sub _get_query_params_tags {
Subroutine _get_query_params_tags: called CC=4 SCAR=13.9
484 33 my $cgi_app = shift;
485
486 33 my $processor = _gen_tag_processor($cgi_app,
487 $cgi_app->can('opentracing_process_tags_query_params'),
488 $cgi_app->can('opentracing_process_tags'),
489 );
490
491 33 my %processed_params;
492
493 33 my $query = $cgi_app->query();
494 33 foreach my $param ($query->url_param()) {
495 53 ▶ next unless defined $param; # huh ???
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 53 53
496 53 my @values = $query->url_param($param);
497 53 my $processed_value = $cgi_app->$processor($param, \@values);
498 53 ▶ next unless defined $processed_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $processed_value 7 46 53
499
500 46 $processed_params{"http.query.$param"} = $processed_value;
501 }
502 33 return %processed_params;
503 }
504  
505  
506  
507 33 ▶sub _get_form_data_tags {
Subroutine _get_form_data_tags: called CC=4 SCAR=13.9
508 33 my $cgi_app = shift;
509 33 ▶ return unless _cgi_get_query_content_type_is_form($cgi_app);
Branch: 1 branch
BranchTrueFalseTotal
unless _cgi_get_query_content_type_is_form($cgi_app) 22 11 33
510
511 11 my $processor = _gen_tag_processor($cgi_app,
512 $cgi_app->can('opentracing_process_tags_form_fields'),
513 $cgi_app->can('opentracing_process_tags'),
514 );
515
516 11 my %processed_params = ();
517
518 11 my %params = $cgi_app->query->Vars();
519 11 ▶ while (my ($param_name, $param_value) = each %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 25 11 36
520 25 my $processed_value = $cgi_app->$processor(
521 $param_name, [ split /\0/, $param_value ]
522 );
523 25 ▶ next unless defined $processed_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $processed_value 4 21 25
524 21 $processed_params{"http.form.$param_name"} = $processed_value
525 }
526
527 11 return %processed_params;
528 }
529  
530  
531  
532 33 ▶sub _get_runmode_tags {
Subroutine _get_runmode_tags: called CC=1 SCAR=0.0
533 33 my $cgi_app = shift;
534
535 33 my %tags = (
536 maybe 'run_mode' => _cgi_get_run_mode($cgi_app),
537 maybe 'run_method' => _cgi_get_run_method($cgi_app),
538 );
539 33 return %tags
540 }
541  
542  
543  
544 33 ▶sub _get_http_status_tags {
Subroutine _get_http_status_tags: called CC=3 SCAR=11.2
545 33 my $cgi_app = shift;
546
547 33 my ($status_code, $status_mess) = _cgi_get_header_status($cgi_app);
548 33 ▶ $status_code //= HTTP_OK;
Condition100%2/2
$status_code //= 200
l!l
1122
MC/DC100%1/1
$status_code //= 200
$status_code
Truth table100%2/2
A
A= $status_code
Aresult
11
00
549 33 ▶ $status_mess //= HTTP::Status::status_message($status_code);
Condition33%1/3
$status_mess //= &HTTP::Status::status_message($status_code)
l!l&&r!l&&!r
0330
MC/DC0%0/2
$status_mess //= &HTTP::Status::status_message($status_code)
$status_mess &HTTP::Status::status_message($status_code)
Truth table33%1/3
A //= B
A= $status_messB= &HTTP::Status::status_message($status_code)
ABresult
1X1
011
000
550
551 33 my %tags = (
552 maybe 'http.status_code' => $status_code,
553 maybe 'http.status_message' => $status_mess,
554 );
555 33 return %tags
556 }
557  
558  
559  
560 ################################################################################
561 #
562 # App - specific routines that interact with the calling CGI-App
563 #
564 ################################################################################
565  
566  
567  
568 33 ▶sub _app_get_bootstrap_options {
Subroutine _app_get_bootstrap_options: called CC=2 SCAR=6.9
569 33 my $cgi_app = shift;
570
571 33 ▶ return unless $cgi_app->can('opentracing_bootstrap_options');
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi_app->can("opentracing_bootstrap_options") 32 1 33
572
573 1 my @bootstrap_options = $cgi_app->opentracing_bootstrap_options( );
574
575 return @bootstrap_options
576 1 }
577  
578  
579  
580 33 ▶sub _app_get_baggage_items {
Subroutine _app_get_baggage_items: called CC=2 SCAR=6.9
581 33 my $cgi_app = shift;
582
583 33 ▶ return unless $cgi_app->can('opentracing_baggage_items');
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi_app->can("opentracing_baggage_items") 32 1 33
584
585 1 my %baggage_items = $cgi_app->opentracing_baggage_items( );
586
587
588 1 return %baggage_items
589 }
590  
591  
592  
593 ################################################################################
594 #
595 # CGI Application Plugin
596 #
597 ################################################################################
598  
599  
600  
601 102 ▶sub _get_plugin {
Subroutine _get_plugin: called CC=1 SCAR=0.0
602 102 my $cgi_app = shift;
603
604 102 return $cgi_app->{__PLUGINS}{OPENTRACING};
605 }
606  
607  
608  
609 ################################################################################
610 #
611 # some internals
612 #
613 ################################################################################
614  
615  
616  
617 44 ▶sub _gen_tag_processor {
Subroutine _gen_tag_processor: called CC=5 SCAR=16.1
618 44 my $cgi_app = shift;
619
620 44 ▶ my $joiner = sub { join $TAG_JOIN_CHAR, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
621
622 44 my (@specs, $fallback);
623 44 foreach my $spec_gen (@_) {
624 88 ▶ next if not defined $spec_gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec_gen 60 28 88
625
626 28 my ($spec, $spec_fallback) = _gen_spec($spec_gen->());
627 28 ▶ $fallback ||= $spec_fallback;
Condition100%3/3
$fallback ||= $spec_fallback
l!l&&r!l&&!r
51211
MC/DC100%2/2
$fallback ||= $spec_fallback
$fallback $spec_fallback
Truth table100%3/3
A ||= B
A= $fallbackB= $spec_fallback
ABresult
1X1
011
000
628 28 push @specs, $spec;
629 }
630 44 ▶ $fallback ||= $joiner;
Condition66%2/3
$fallback ||= $joiner
l!l&&r!l&&!r
12320
MC/DC0%0/2
$fallback ||= $joiner
$fallback $joiner
Truth table66%2/3
A ||= B
A= $fallbackB= $joiner
ABresult
1X1
011
000
631
632 78 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
633 78 my ($cgi_app, $name, $values) = @_;
634
635 78 my $processor = $fallback;
636 78 foreach my $spec (@specs) {
637 62 my ($matched, $spec_processor) = $spec->($name);
638 62 ▶ $processor = $spec_processor if $matched;
Branch: 1 branch
BranchTrueFalseTotal
if $matched 31 31 62
639 }
640
641 78 ▶ return if not defined $processor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $processor 9 69 78
642 69 ▶ return $processor if not ref $processor;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $processor 10 59 69
643  
644 59 ▶ if (ref $processor eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $processor eq "CODE") 59 0 59
645 59 my $processed = $processor->(@$values);
646 59 ▶ $processed = $joiner->(@$processed) if ref $processed eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $processed eq "ARRAY" 1 58 59
647 59 return $processed;
648 }
649
650 0 croak "Invalid processor for param `$name`: ", ref $processor;
651 44 };
652 }
653  
654  
655  
656 28 ▶sub _gen_spec {
Subroutine _gen_spec: called CC=6 SCAR=17.9
657 28 my @def = @_;
658
659 28 my $fallback;
660 28 ▶ $fallback = pop @def if @def % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @def % 2 != 0 14 14 28
661
662 28 my (%direct_match, @regex);
663 28 ▶ while (my ($cond, $processor) = splice @def, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@def, 0, 2)) 33 28 61
664 33 ▶ if (ref $cond eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cond eq 'Regexp') { } 1 32 33
665 1 push @regex, [ $cond => $processor ];
666 }
667 else {
668 32 ▶ foreach my $name (ref $cond eq 'ARRAY' ? @$cond : $cond) {
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'ARRAY' ? : 1 31 32
669 33 $direct_match{$name} = $processor;
670 }
671 }
672 }
673 62 ▶ my $spec = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
674 62 my ($name) = @_;
675
676 # return match state separately to differentiate from undef processors
677 62 ▶ return (1, $direct_match{$name}) if exists $direct_match{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $direct_match{$name} 28 34 62
678
679 34 foreach (@regex) {
680 3 my ($re, $processor) = @$_;
681 3 ▶ return (1, $processor) if $name =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /$re/ 3 0 3
682 }
683 31 return;
684 28 };
685
686 28 return ($spec, $fallback);
687 }
688  
689  
690  
691 6 ▶sub _wrap_run {
Subroutine _wrap_run: called CC=1 SCAR=0.0
692 6 my ($orig) = @_;
693  
694 33 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
695 33 my $cgi_app = shift;
696  
697 33 my $res;
698 33 my $wantarray = wantarray; # eval has its own
699 33 my $ok = eval {
700 33 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 33 33
701 0 $res = [ $cgi_app->$orig(@_) ];
702 }
703 else {
704 33 $res = $cgi_app->$orig(@_);
705 }
706 29 1;
707 };
708 33 ▶ return $wantarray ? @$res : $res if $ok;
Branch: 2 branches
BranchTrueFalseTotal
if $ok 29 4 33
$wantarray ? : 0 29 29
709  
710 4 my $error = $@;
711
712 4 $cgi_app->header_add(-status => HTTP_INTERNAL_SERVER_ERROR);
713
714 4 my $plugin = _get_plugin($cgi_app);
715
716 4 $plugin->add_tags(CGI_REQUEST,
717 _get_http_status_tags($cgi_app),
718 grep_error_tags( $plugin->get_tags(TRC_ACTIVE_SCOPE) ),
719 error => 1,
720 message => $error,
721 );
722
723 4 $plugin->add_tags(TRC_ACTIVE_SCOPE,
724 error => 1,
725 message => $error,
726 grep_error_tags( $plugin->get_tags(TRC_ACTIVE_SCOPE) ),
727 );
728
729 4 my $tracer = $plugin->get_tracer();
730 4 _cascade_set_failed_spans($tracer, $error);
731  
732 4 die $error;
733 6 };
734 }
735  
736  
737  
738 6 1;
↑ Summary