blib/lib/OpenTracing/WrapScope.pm

filter: Statementstmt 100% 112 / 112 Branchbran 96.0% 48 / 50 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 21 / 21 Podpod 100% 3 / 3 totaltotal 97.9% 195 / 199 CC 30 SCAR 16.7
CC
30
Cov
98%
__ANON__
10.0
import
8.0
__ANON__
7.0
CRAP 5.3
↑ Summary bin/generate_wrap_config.pl »
1 package OpenTracing::WrapScope;
2 13 our $VERSION = 'v0.105.0';
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use B::Hooks::EndOfScope;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use OpenTracing::GlobalTracer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Sub::Info qw/sub_info/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 { # transparent caller, stolen from Hook::LexWrap
14 13 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 *CORE::GLOBAL::caller = sub (;$) {
16 402 ▶ my ($height) = ($_[0]||0);
Subroutine __ANON__: called CC=10 SCAR=23.0
Condition100%2/2
$_[0] || 0
l!l
111291
MC/DC100%1/1
$_[0] || 0
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
17 402 my $i=1;
18 402 my $name_cache;
19 402 while (1) {
20 my @caller = CORE::caller() eq 'DB'
21 686 ▶ ? do { package DB; CORE::caller($i++) }
Branch: 1 branch
BranchTrueFalseTotal
caller() eq 'DB' ? : 10 676 686
22 : CORE::caller($i++);
23 686 ▶ return if not @caller;
Branch: 1 branch
BranchTrueFalseTotal
unless @caller 17 669 686
24 669 ▶ $caller[3] = $name_cache if $name_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $name_cache 66 603 669
25 669 ▶ $name_cache = $caller[0] eq __PACKAGE__ ? $caller[3] : '';
Branch: 1 branch
BranchTrueFalseTotal
$caller[0] eq __PACKAGE__ ? : 66 603 669
26 669 ▶ next if $name_cache || $height-- != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $name_cache or $height-- != 0 284 385 669
Condition100%3/3
$name_cache or $height-- != 0
l!l&&r!l&&!r
66218385
MC/DC100%2/2
$name_cache or $height-- != 0
$name_cache $height-- != 0
Truth table100%3/3
A or B
A= $name_cacheB= $height-- != 0
ABresult
1X1
011
000
27 385 ▶ return wantarray ? @_ ? @caller : @caller[0..2] : $caller[0];
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 175 210 385
@_ ? : 170 5 175
28 }
29 13 };
30 }
31  
32 sub import {
33 14 ▶ shift; # __PACKAGE__
Subroutine import: called CC=8 SCAR=20.8
34 14 my $target_package = caller;
35  
36 14 my ($use_env, @subs, @files);
37 14 ▶ while (my (undef, $arg) = each @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $arg) = each @_) 22 14 36
38 22 ▶ if ($arg eq '-env') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq '-env') { } 2 20 22
elsif ($arg eq '-file') { } 2 18 20
39 2 $use_env = 1;
40 }
41 elsif ($arg eq '-file') {
42 2 ▶ my (undef, $next) = each @_ or last;
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $next) = each @_ or last 0 2 2
43 2 ▶ push @files, ref $next eq 'ARRAY' ? @$next : $next;
Branch: 1 branch
BranchTrueFalseTotal
ref $next eq 'ARRAY' ? : 1 1 2
44 }
45 else {
46 18 push @subs, _qualify_sub($arg, $target_package);
47 }
48 }
49 14 ▶ if ($use_env and $ENV{OPENTRACING_WRAPSCOPE_FILE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_env and $ENV{"OPENTRACING_WRAPSCOPE_FILE"}) 2 12 14
Condition66%2/3
$use_env and $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
!ll&&!rl&&r
1202
MC/DC50%1/2
$use_env and $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
$use_env $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
Truth table66%2/3
A and B
A= $use_envB= $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
ABresult
0X0
100
111
50 2 push @files, split ':', $ENV{OPENTRACING_WRAPSCOPE_FILE};
51 }
52 14 push @subs, map { _load_sub_spec($_) } grep { -f } map { glob } @files;
53  
54 14 ▶ on_scope_end { install_wrapped(uniq @subs) };
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 14 return;
57 }
58  
59 sub install_wrapped {
60 18 ▶ foreach my $sub (@_) {
Subroutine install_wrapped: called CC=3 SCAR=11.0
Pod documented
61 37 my $full_sub = _qualify_sub($sub, scalar caller);
62  
63 37 ▶ if (not defined &$sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$sub) 2 35 37
64 2 warnings::warn "Couldn't find sub: $sub";
65 1 next;
66 }
67  
68 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 13 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 35 *$sub = wrapped(\&$sub);
71 }
72 17 return;
73 }
74  
75 sub wrapped {
76 35 ▶ my ($coderef) = @_;
Subroutine wrapped: called CC=1 SCAR=0.0
Pod documented
77 35 my $info = sub_info($coderef);
78  
79 return sub {
80 43 ▶ my ($call_package, $call_filename, $call_line) = caller(0);
Subroutine __ANON__: called CC=7 SCAR=19.5
81 43 my $call_sub = (caller(1))[3];
82 43 my $tracer = OpenTracing::GlobalTracer->get_global_tracer;
83 my $scope = $tracer->start_active_span(
84 "$info->{package}::$info->{name}",
85 tags => {
86 'source.subname' => $info->{name},
87 'source.file' => $info->{file},
88 'source.line' => $info->{start_line},
89 'source.package' => $info->{package},
90 43 maybe
91 'caller.subname' => $call_sub,
92 'caller.file' => $call_filename,
93 'caller.line' => $call_line,
94 'caller.package' => $call_package,
95 },
96 );
97  
98 43 my $result;
99 43 my $wantarray = wantarray; # eval will have its own
100 43 my $ok = eval {
101 43 ▶ if (defined $wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wantarray) { } 4 39 43
102 4 ▶ $result = $wantarray ? [&$coderef] : &$coderef;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 2 2 4
103 }
104 else {
105 39 &$coderef;
106 }
107 42 1;
108 };
109 # TODO: message should go to logs but we don't have those yet
110 43 ▶ $scope->get_span->add_tags(error => 1, message => "$@") unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 42 43
111 43 $scope->close();
112  
113 43 ▶ die $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 42 43
114 42 ▶ return if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 38 4 42
115 4 ▶ return wantarray ? @$result : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
116 35 };
117 }
118  
119 sub _qualify_sub {
120 55 ▶ my ($sub, $pkg) = @_;
Subroutine _qualify_sub: called CC=2 SCAR=6.9
121 55 ▶ return $sub if $sub =~ /'|::/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /'|::/ 39 16 55
122 16 return "${pkg}::$sub";
123 }
124  
125 sub _load_sub_spec {
126 10 ▶ my ($filename) = @_;
Subroutine _load_sub_spec: called CC=5 SCAR=16.1
127  
128 10 ▶ open my $fh_subs, '<', $filename or die "$filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_subs, "<", $filename or die "${filename}: $!" 0 10 10
129  
130 10 my @subs;
131 10 ▶ while (<$fh_subs>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh_subs)) 19 9 28
132 19 ▶ next if /^\s*#/; # commented-out line
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 1 18 19
133 18 s/\s*#.*\Z//; # trailing comment
134 18 chomp;
135 18 ▶ croak "Unqualified subroutine: $_" if !/'|::/;
Branch: 1 branch
BranchTrueFalseTotal
unless /'|::/ 1 17 18
136 17 push @subs, $_;
137 }
138 9 close $fh_subs;
139  
140 9 return @subs;
141 }
142  
143 sub wrap_from_file {
144 3 ▶ my ($filename) = @_;
Subroutine wrap_from_file: called CC=1 SCAR=0.0
Pod documented
145 3 install_wrapped( _load_sub_spec($filename) );
146 2 return;
147 }
148  
149  
150 13 1;
↑ Summary bin/generate_wrap_config.pl »