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

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