| 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
MC/DC100%1/1
$_[0] || 0
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 22 | : CORE::caller($i++); | ||||||||||||||||||||||||||||
| 23 | 686 | ▶ | return if not @caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 669 | ▶ | $caller[3] = $name_cache if $name_cache; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 669 | ▶ | $name_cache = $caller[0] eq __PACKAGE__ ? $caller[3] : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 669 | ▶ | next if $name_cache || $height-- != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name_cache or $height-- != 0
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
| |||||||||||||||||||||||||||||
| 27 | 385 | ▶ | return wantarray ? @_ ? @caller : @caller[0..2] : $caller[0]; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 38 | 22 | ▶ | if ($arg eq '-env') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | 2 | $use_env = 1; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | elsif ($arg eq '-file') { | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my (undef, $next) = each @_ or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | push @files, ref $next eq 'ARRAY' ? @$next : $next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$use_env and $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | $result = $wantarray ? [&$coderef] : &$coderef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 111 | 43 | $scope->close(); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 43 | ▶ | die $@ unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 42 | ▶ | return if not defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | return wantarray ? @$result : $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 10 | my @subs; | |||||||||||||||||||||||||||
| 131 | 10 | ▶ | while (<$fh_subs>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 19 | ▶ | next if /^\s*#/; # commented-out line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 18 | s/\s*#.*\Z//; # trailing comment | |||||||||||||||||||||||||||
| 134 | 18 | chomp; | |||||||||||||||||||||||||||
| 135 | 18 | ▶ | croak "Unqualified subroutine: $_" if !/'|::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||