blib/lib/OpenTelemetry/Instrumentation/namespace.pm

filter: Statementstmt 88.7% 110 / 124 Branchbran 69.3% 43 / 62 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 23 / 23 Podpod 16.6% 1 / 6 totaltotal 77.8% 183 / 235 CC 40 SCAR 24.7
CC
40
Cov
80%
wrap_subroutines
30.0
wrap_module
11.3
install
5.0
CRAP 11.8
↑ Summary blib/lib/OpenTelemetry/Instrumentation/caller.pm »
1 package OpenTelemetry::Instrumentation::namespace;
2 # ABSTRACT: OpenTelemetry instrumentation for a namespace
3  
4 2 our $VERSION = '0.033';
5  
6 2 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use experimental 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Class::Method::Modifiers 'install_modifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Devel::Peek;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util 'pairs';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use OpenTelemetry::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use OpenTelemetry;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Ref::Util qw( is_regexpref is_coderef is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use namespace::clean ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use parent 'OpenTelemetry::Instrumentation';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 my $LOGGER = OpenTelemetry::Common::internal_logger;
23 2 my %CACHE;
24  
25 sub install {
26 3 ▶ my $class = shift;
Subroutine install: called CC=5 SCAR=16.2
Pod documented
27 3 my ( $rules, $options ) = $class->parse_options(@_);
28  
29 3 ▶ return !!1 unless @$rules;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rules 1 2 3
30  
31 # Loop over loaded modules
32 2 for my $module ( keys %INC ) {
33 651 ▶ if ( lc $module eq $module ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $module eq $module) 48 603 651
34 # $LOGGER->trace("Not auto-instrumenting $module because it is a pragma");
35 48 next;
36 }
37  
38 603 ▶ if ( $module =~ /^[0-9]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^[0-9]/u) 0 603 603
39 # $LOGGER->trace("Not auto-instrumenting $module because it is a version");
40 0 next;
41 }
42  
43 603 $class->wrap_module( $module, $rules, $options )
44 }
45  
46 2 $class->wrap_require( $rules, $options );
47  
48 2 return !!1;
49 }
50  
51 ▶sub wrap_require ($class, $rules, $options ) {
Subroutine wrap_require: called CC=1 SCAR=0.0
Pod undocumented
52 2 my $old_hook = ${^HOOK}{require__before};
53 ${^HOOK}{require__before} = sub {
54 102 ▶ my ($name) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
55  
56 102 my $return;
57 102 ▶ $return = $old_hook->($name) if $old_hook;
Branch: 1 branch
BranchTrueFalseTotal
if $old_hook 14 88 102
58  
59 return sub {
60 102 ▶ $return->() if ref $return && is_coderef $return;
Subroutine __ANON__: called CC=4 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if ref $return and is_coderef($return) 14 88 102
Condition66%2/3
ref $return and is_coderef($return)
!ll&&!rl&&r
88014
MC/DC50%1/2
ref $return and is_coderef($return)
ref $return is_coderef($return)
Truth table66%2/3
A and B
A= ref $returnB= is_coderef($return)
ABresult
0X0
100
111
61 102 ▶ return unless $INC{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{$name} 0 102 102
62 102 $class->wrap_module($name, $rules, $options);
63 102 };
64 2 };
65 }
66  
67 ▶sub parse_options ( $class, @config ) {
Subroutine parse_options: called CC=5 SCAR=16.1
Pod undocumented
68 5 my %options = (
69 -ignore_constants => 1,
70 -ignore_private => 1,
71 -ignore_import => 1,
72 -prefer_instrumentations => 1,
73 );
74  
75 5 my @rules;
76 5 for ( pairs @config ) {
77 5 ▶ unless ( $_->[0] =~ /^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->[0] =~ /^-/u) 4 1 5
78 4 push @rules, @$_;
79 4 next;
80 }
81  
82 1 $options{ $_->[0] } = $_->[1];
83 }
84  
85 5 ▶ if ( my $path = delete $options{-from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = delete $options{"-from_file"}) 1 4 5
86 1 try {
87 1 $LOGGER->trace("Loading OpenTelemetry namespace configuration from $path");
88 1 my $loaded = YAML::PP::LoadFile($path);
89 1 ▶ @rules = ( is_hashref($loaded) ? %$loaded : @$loaded, @rules );
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($loaded) ? : 1 0 1
90 }
91 catch ($e) {
92 0 $LOGGER->warn(
93 "Could not load configuration for OpenTelemetry namespace instrumentation: $e"
94 );
95 }
96 }
97  
98 5 return ( \@rules, \%options );
99 }
100  
101 ▶sub wrap_module ( $class, $module, $rules, $options ) {
Subroutine wrap_module: called CC=7 SCAR=24.2
Pod undocumented
102 705 my $package = $module =~ s/\//::/gr;
103 705 $package =~ s/\.p[ml]$//;
104  
105 705 ▶ if ( $package =~ /^::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /^::/u) 0 705 705
106 # $LOGGER->trace("Not auto-instrumenting $package because it is not a package");
107 0 return;
108 }
109  
110 705 ▶ if ( $package =~ /^OpenTelemetry/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /^OpenTelemetry/u) 38 667 705
111 # $LOGGER->trace("Not auto-instrumenting $package because it is itself an OpenTelemetry class");
112 38 return;
113 }
114  
115 # TODO
116 667 ▶ if ( $package =~ /^(?:B|Exporter|Test2|Plack|XSLoader)(?:::|$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /^(?:B|Exporter|Test2|Plack|XSLoader)(?:::|$)/u) 287 380 667
117 # $LOGGER->trace("Not auto-instrumenting $package because it is not currently supported");
118 287 return;
119 }
120  
121 380 ▶ if ( my $instrumentation = $class->for_package($package) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $instrumentation = $class->for_package($package)) 0 380 380
122 0 ▶ if ( $options->{-prefer_instrumentations} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"-prefer_instrumentations"}) 0 0 0
123 # $LOGGER->trace(
124 # "Not auto-instrumenting $package because $instrumentation is installed in this system. You can ignore this by disabling -prefer_instrumentations"
125 # );
126  
127 0 return;
128 }
129  
130 0 my $notional = Module::Runtime::module_notional_filename($instrumentation);
131 0 ▶ if ( $INC{$notional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{$notional}) 0 0 0
132 # $LOGGER->trace(
133 # "Not auto-instrumenting $package because $instrumentation has already been loaded"
134 # );
135 0 return;
136 }
137 }
138  
139 380 $class->wrap_package( $package, $rules, $options );
140 }
141  
142 ▶sub wrap_package ( $class, $package, $rules, $options ) {
Subroutine wrap_package: called CC=5 SCAR=16.1
Pod undocumented
143 # Check the assumed package of the module against each package rule
144 380 for my $pair ( pairs @$rules ) {
145 759 my ( $matcher, $rules ) = @$pair;
146  
147 # If this rule does not apply to this package
148 # move to the next rule
149 759 ▶ next if is_regexpref($matcher)
Branch: 2 branches
BranchTrueFalseTotal
if is_regexpref($matcher) ? !($package =~ /$matcher/u) : $package ne $matcher 755 4 759
is_regexpref($matcher) ? : 419 340 759
150 ? $package !~ $matcher
151 : $package ne $matcher;
152  
153 # Since this rule applies to this package,
154 # we abort if this rule is 'ignore'
155 4 ▶ return unless $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless $rules 1 3 4
156  
157 3 $class->wrap_subroutines( $package, $rules, $options )
158 }
159 }
160  
161 ▶sub wrap_subroutines ( $class, $package, $rules, $options ) {
Subroutine wrap_subroutines: called CC=18 SCAR=34.0
Pod undocumented
162 5 my $default_rules = [ qr/.*/ => 1 ];
163  
164 # Normalise rules
165 5 ▶ $rules = $default_rules unless ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules 3 2 5
166 5 ▶ $rules = [ %$rules ] if is_hashref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if is_hashref($rules) 0 5 5
167  
168 5 my $subs = namespace::clean->get_functions($package);
169  
170 # The package rule has matched
171 # Loop over the subroutines in the package
172 5 ▶ SUB: while ( my ( $subname, $coderef ) = each %$subs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$subs) 10 5 15
173 10 my $fullname = "${package}::${subname}";
174  
175 10 ▶ if ( $subname =~ /^(?:un)?import$/ && $options->{-ignore_import} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subname =~ /^(?:un)?import$/u and $options->{"-ignore_import"}) 0 10 10
Condition33%1/3
$subname =~ /^(?:un)?import$/u and $options->{"-ignore_import"}
!ll&&!rl&&r
1000
MC/DC0%0/2
$subname =~ /^(?:un)?import$/u and $options->{"-ignore_import"}
$subname =~ /^(?:un)?import$/u $options->{"-ignore_import"}
Truth table33%1/3
A and B
A= $subname =~ /^(?:un)?import$/uB= $options->{"-ignore_import"}
ABresult
0X0
100
111
176 # $LOGGER->trace(
177 # "Not auto-instrumenting $fullname because -ignore_import was set",
178 # );
179 0 next;
180 }
181  
182 10 ▶ if ( uc($subname) eq $subname && $options->{-ignore_constants} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $subname eq $subname and $options->{"-ignore_constants"}) 0 10 10
Condition33%1/3
uc $subname eq $subname and $options->{"-ignore_constants"}
!ll&&!rl&&r
1000
MC/DC0%0/2
uc $subname eq $subname and $options->{"-ignore_constants"}
uc $subname eq $subname $options->{"-ignore_constants"}
Truth table33%1/3
A and B
A= uc $subname eq $subnameB= $options->{"-ignore_constants"}
ABresult
0X0
100
111
183 # $LOGGER->trace(
184 # "Not auto-instrumenting $fullname because -ignore_constants was set",
185 # );
186 0 next;
187 }
188  
189 10 ▶ if ( $subname =~ /^_/ && $options->{-ignore_private} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subname =~ /^_/u and $options->{"-ignore_private"}) 0 10 10
Condition33%1/3
$subname =~ /^_/u and $options->{"-ignore_private"}
!ll&&!rl&&r
1000
MC/DC0%0/2
$subname =~ /^_/u and $options->{"-ignore_private"}
$subname =~ /^_/u $options->{"-ignore_private"}
Truth table33%1/3
A and B
A= $subname =~ /^_/uB= $options->{"-ignore_private"}
ABresult
0X0
100
111
190 # $LOGGER->trace(
191 # "Not auto-instrumenting $fullname because -ignore_private was set",
192 # );
193 0 next;
194 }
195  
196 # Skip imported functions.
197 # See https://stackoverflow.com/a/3685262/807650
198 10 ▶ if ( my $gv = Devel::Peek::CvGV($coderef) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gv = Devel::Peek::CvGV($coderef)) 10 0 10
199 10 ▶ if ( *$gv{PACKAGE} ne $package ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$gv{"PACKAGE"} ne $package) 0 10 10
200 # $LOGGER->trace(
201 # "Not auto-instrumenting $fullname because it is imported from a different package"
202 # );
203 0 next;
204 }
205 }
206  
207 10 ▶ if ( defined prototype $coderef ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined prototype $coderef) 0 10 10
208 # $LOGGER->trace(
209 # "Not auto-instrumenting $fullname because it has a prototype"
210 # );
211 0 next;
212 }
213  
214 10 for ( pairs @$rules ) {
215 13 my ( $matcher, $spanner ) = @$_;
216  
217 13 ▶ next unless $subname =~ $matcher;
Branch: 1 branch
BranchTrueFalseTotal
unless $subname =~ /$matcher/u 4 9 13
218 9 ▶ next SUB unless $spanner;
Branch: 1 branch
BranchTrueFalseTotal
unless $spanner 0 9 9
219  
220 # Avoid double-wrapping subs
221 9 ▶ if ( $CACHE{$package}{$subname}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CACHE{$package}{$subname}++) 0 9 9
222 # $LOGGER->trace(
223 # "Not auto-instrumenting $fullname because we have already done so"
224 # );
225 0 next;
226 }
227  
228 $LOGGER->info(
229 9 "Adding OpenTelemetry auto-instrumentation for $fullname"
230 );
231  
232 $spanner = sub {
233 8 ▶ my ( $package, $subname, $orig, @args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
234 OpenTelemetry
235 ->tracer_provider
236 ->tracer( name => $package, version => $package->VERSION )
237 ->in_span(
238 8 ▶ "${package}::${subname}" => sub { $orig->(@args) },
Subroutine __ANON__: called CC=1 SCAR=0.0
239 8 );
240 9 ▶ } unless is_coderef $spanner;
Branch: 1 branch
BranchTrueFalseTotal
unless is_coderef($spanner) 9 0 9
241  
242 install_modifier $package => around => $subname => sub {
243 8 ▶ local @_ = ( $package, $subname, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
244 8 goto $spanner;
245 9 };
246 }
247 }
248 }
249  
250 2 1;
↑ Summary blib/lib/OpenTelemetry/Instrumentation/caller.pm »