| 1 | package OpenTelemetry::Instrumentation; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Top-level interface for OpenTelemetry instrumentations | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | our $VERSION = '0.040'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use List::Util 'uniqstr'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Module::Runtime (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Module::Pluggable search_path => [qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | OpenTelemetry::Instrumentation | ||||||||||||||||||||||||||||
| 15 | OpenTelemetry::Integration | ||||||||||||||||||||||||||||
| 16 | 3 | )]; | |||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Ref::Util qw( is_coderef is_hashref is_arrayref ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use OpenTelemetry::Common (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | my $logger = OpenTelemetry::Common::internal_logger; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # To be overriden by instrumentations | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub dependencies { } | ||||||||||||||||||||||||||
|
Subroutine
dependencies: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub uninstall { } # experimental | ||||||||||||||||||||||||||
|
Subroutine
uninstall: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | my %REGISTRY; | |||||||||||||||||||||||||||
| 28 | 7 | ▶ | my sub find_instrumentations { | ||||||||||||||||||||||||||
|
Subroutine
find_instrumentations: called CC=9 SCAR=24.0
| |||||||||||||||||||||||||||||
| 29 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | 7 | ▶ | return if %REGISTRY; # Runs once and caches results | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Inlined from OpenTelemetry::Common to read Perl-specific config | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $legacy_support = $ENV{OTEL_PERL_USE_LEGACY_INSTRUMENTATIONS} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"OTEL_PERL_USE_LEGACY_INSTRUMENTATIONS"} // 1
MC/DC0%0/1
$ENV{"OTEL_PERL_USE_LEGACY_INSTRUMENTATIONS"} // 1
$ENV{"OTEL_PERL_USE_LEGACY_INSTRUMENTATIONS"}
Truth table50%1/2
A
A= $ENV{"OTEL_PERL_USE_LEGACY_INSTRUMENTATIONS"}
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $legacy_support | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | = $legacy_support =~ /^true$/i ? 1 | ||||||||||||||||||||||||||||
| 36 | : $legacy_support =~ /^false$/i ? 0 | ||||||||||||||||||||||||||||
| 37 | : $legacy_support; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # We sort the plugins so that we prefer the Instrumentation namespace | ||||||||||||||||||||||||||||
| 40 | 1 | for ( sort $class->plugins ) { | |||||||||||||||||||||||||||
| 41 | 4 | ▶ | last if /^OpenTelemetry::Integration::/ && !$legacy_support; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/^OpenTelemetry::Integration::/ and not $legacy_support
MC/DC0%0/2
/^OpenTelemetry::Integration::/ and not $legacy_support
/^OpenTelemetry::Integration::/
not $legacy_support
Truth table66%2/3
A and not(B)
A= /^OpenTelemetry::Integration::/B= not $legacy_support
| |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | $REGISTRY{ s/^OpenTelemetry::(?:Instrumentation|Integration):://r } ||= $_ | ||||||||||||||||||||||||||
|
Condition33%1/3
$REGISTRY{s/^OpenTelemetry::(?:Instrumentation|Integration):://r} ||= $_
MC/DC0%0/2
$REGISTRY{s/^OpenTelemetry::(?:Instrumentation|Integration):://r} ||= $_
$REGISTRY{s/^OpenTelemetry::(?:Instrumentation|Integration):://r}
$_
Truth table33%1/3
A ||= B
A= $REGISTRY{s/^OpenTelemetry::(?:Instrumentation|Integration):://r}B= $_
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | return; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 6 | ▶ | sub for_package ($class, $package, @) { | ||||||||||||||||||||||||||
|
Subroutine
for_package: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 6 | find_instrumentations($class); | |||||||||||||||||||||||||||
| 50 | 6 | ▶ | $REGISTRY{$package // ''}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$package // ""
MC/DC100%1/1
$package // ""
$package
Truth table100%2/2
A
A= $package
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | my @installed; | ||||||||||||||||||||||||||||
| 54 | 5 | ▶ | sub import ( $class, @args ) { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=15 SCAR=27.4
| |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | return unless @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | my $all = $args[0] =~ /^[:-]all$/ && shift @args; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args[0] =~ /^[:-]all$/ && shift @args
MC/DC50%1/2
$args[0] =~ /^[:-]all$/ && shift @args
$args[0] =~ /^[:-]all$/
shift @args
Truth table66%2/3
A && B
A= $args[0] =~ /^[:-]all$/B= shift @args
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | my %configuration; | |||||||||||||||||||||||||||
| 60 | 4 | ▶ | while ( my $key = shift @args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | my $options = is_hashref($args[0]) || is_arrayref($args[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
is_hashref($args[0]) or is_arrayref($args[0])
MC/DC0%0/2
is_hashref($args[0]) or is_arrayref($args[0])
is_hashref($args[0])
is_arrayref($args[0])
Truth table33%1/3
A or B
A= is_hashref($args[0])B= is_arrayref($args[0])
| |||||||||||||||||||||||||||||
| 62 | ? shift @args : {}; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Legacy namespace support. If we are loading an integration | ||||||||||||||||||||||||||||
| 65 | # by name which does not exist in INC in the new namespace, | ||||||||||||||||||||||||||||
| 66 | # but does exist in the legacy namespace, we use the legacy | ||||||||||||||||||||||||||||
| 67 | # name instead. | ||||||||||||||||||||||||||||
| 68 | 2 | my $instrumentation = $class->for_package($key); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | unless ( $instrumentation ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | $logger->warn( | |||||||||||||||||||||||||||
| 72 | "Unable to load OpenTelemetry instrumentation for $key: Can't locate any suitable module in \@INC (you may need to install OpenTelemetry::Instrumentation::$key) (\@INC entries checked: @INC)", | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | 1 | next; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | $configuration{$instrumentation} = $options; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | if ($all) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | find_instrumentations($class); | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | $configuration{$_} //= {} for values %REGISTRY; | ||||||||||||||||||||||||||
|
Condition50%1/2
$configuration{$_} //= {}
MC/DC0%0/1
$configuration{$_} //= {}
$configuration{$_}
Truth table50%1/2
A
A= $configuration{$_}
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4 | for my $package ( keys %configuration ) { | |||||||||||||||||||||||||||
| 86 | 5 | try { | |||||||||||||||||||||||||||
| 87 | 5 | $logger->tracef('Loading %s', $package); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 5 | Module::Runtime::require_module($package); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # We only load dependencies if we are not loading every module | ||||||||||||||||||||||||||||
| 92 | 4 | ▶ | unless ($all) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1 | Module::Runtime::require_module($_) for $package->dependencies; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | my $config = $configuration{ $package }; | |||||||||||||||||||||||||||
| 97 | 4 | ▶ | my $ok = $package->install( is_hashref $config ? %$config : @$config ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | if ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | push @installed, $package; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 3 | $logger->tracef("$package did not install itself"); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | catch ($e) { | ||||||||||||||||||||||||||||
| 108 | # Just a warning, if we're loading everything then | ||||||||||||||||||||||||||||
| 109 | # we shouldn't cause chaos just because something | ||||||||||||||||||||||||||||
| 110 | # doesn't happen to be available. | ||||||||||||||||||||||||||||
| 111 | 1 | $logger->warnf('Unable to load %s: %s', $package, $e); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | sub unimport ( $class, @args ) { | ||||||||||||||||||||||||||
|
Subroutine
unimport: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 117 | 5 | ▶ | @args = @installed unless @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 5 | $_->uninstall for @args; | |||||||||||||||||||||||||||
| 119 | 5 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | 1; | |||||||||||||||||||||||||||