| 1 | package DBIx::OpenTracing; | ||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use feature qw[ state ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use syntax 'maybe'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use B; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use DBI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use List::Util qw[ sum ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use OpenTracing::GlobalTracer; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Scalar::Util qw[ blessed ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Scope::Context; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = 'v0.0.2'; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use constant TAGS_DEFAULT => ('db.type' => 'sql'); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | use constant { | ||||||||||||||||||||||||||||||||
| 18 | 2 | _DBI_EXECUTE => \&DBI::st::execute, | |||||||||||||||||||||||||||||||
| 19 | _DBI_DO => \&DBI::db::do, | ||||||||||||||||||||||||||||||||
| 20 | _DBI_SELECTALL_ARRAYREF => \&DBI::db::selectall_arrayref, | ||||||||||||||||||||||||||||||||
| 21 | _DBI_SELECTROW_ARRAYREF => \&DBI::db::selectrow_arrayref, | ||||||||||||||||||||||||||||||||
| 22 | _DBI_SELECTROW_ARRAY => \&DBI::db::selectrow_array, | ||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use constant _PP_MODE => !!$INC{'DBI/PurePerl.pm'}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | if (%DBIx::QueryLog::SKIP_PKG_MAP) { # hide from DBIx::QueryLog's caller() | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 0 | $DBIx::QueryLog::SKIP_PKG_MAP{ (__PACKAGE__) } = 1; | |||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 2 | my ($is_enabled, $is_suspended); | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 9 | ▶ | sub _numeric_result { 0 + $_[0] } | ||||||||||||||||||||||||||||||
|
Subroutine
_numeric_result: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | 12 | ▶ | sub _array_size { scalar @{ $_[0] } } | ||||||||||||||||||||||||||||||
|
Subroutine
_array_size: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | sub enable { | ||||||||||||||||||||||||||||||||
| 36 | 13 | ▶ | return if $is_enabled or $is_suspended; | ||||||||||||||||||||||||||||||
|
Subroutine
enable: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
Condition100%3/3
$is_enabled or $is_suspended
MC/DC100%2/2
$is_enabled or $is_suspended
$is_enabled
$is_suspended
Truth table100%3/3
A or B
A= $is_enabledB= $is_suspended
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 10 | state $do = _gen_wrapper(_DBI_DO, \&_numeric_result); | |||||||||||||||||||||||||||||||
| 39 | 10 | state $selectall_arrayref = _gen_wrapper(_DBI_SELECTALL_ARRAYREF, \&_array_size); | |||||||||||||||||||||||||||||||
| 40 | 10 | state $selectrow_arrayref = _gen_wrapper(_DBI_SELECTROW_ARRAYREF); | |||||||||||||||||||||||||||||||
| 41 | 10 | state $selectrow_array = _gen_wrapper(_DBI_SELECTROW_ARRAY); | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 10 | *DBI::st::execute = \&_execute; | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 10 | if (not _PP_MODE) { # everything goes through execute() in PP mode | |||||||||||||||||||||||||||||||
| 47 | 10 | *DBI::db::do = $do; | |||||||||||||||||||||||||||||||
| 48 | 10 | *DBI::db::selectall_arrayref = $selectall_arrayref; | |||||||||||||||||||||||||||||||
| 49 | 10 | *DBI::db::selectrow_arrayref = $selectrow_arrayref; | |||||||||||||||||||||||||||||||
| 50 | 10 | *DBI::db::selectrow_array = $selectrow_array; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 10 | $is_enabled = 1; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 10 | return; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | sub disable { | ||||||||||||||||||||||||||||||||
| 59 | 10 | ▶ | return unless $is_enabled; | ||||||||||||||||||||||||||||||
|
Subroutine
disable: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 62 | 8 | *DBI::st::execute = _DBI_EXECUTE; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 8 | if (not _PP_MODE) { | |||||||||||||||||||||||||||||||
| 65 | 8 | *DBI::db::do = _DBI_DO; | |||||||||||||||||||||||||||||||
| 66 | 8 | *DBI::db::selectall_arrayref = _DBI_SELECTALL_ARRAYREF; | |||||||||||||||||||||||||||||||
| 67 | 8 | *DBI::db::selectrow_arrayref = _DBI_SELECTROW_ARRAYREF; | |||||||||||||||||||||||||||||||
| 68 | 8 | *DBI::db::selectrow_array = _DBI_SELECTROW_ARRAY; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 8 | $is_enabled = 0; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 8 | return; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | sub import { enable() } | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub unimport { disable() } | ||||||||||||||||||||||||||||||
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | sub _tags_dbh { | ||||||||||||||||||||||||||||||||
| 80 | 61 | ▶ | my ($dbh) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_tags_dbh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 81 | return ( | ||||||||||||||||||||||||||||||||
| 82 | maybe | ||||||||||||||||||||||||||||||||
| 83 | 'db.user' => $dbh->{Username}, | ||||||||||||||||||||||||||||||||
| 84 | 'db.instance' => $dbh->{Name}, | ||||||||||||||||||||||||||||||||
| 85 | 61 | ); | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | sub _tags_sth { | ||||||||||||||||||||||||||||||||
| 89 | 61 | ▶ | my ($sth) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_tags_sth: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 90 | 61 | ▶ | return ('db.statement' => $sth) if !blessed($sth) or !$sth->isa('DBI::st'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($sth) and $sth->isa("DBI::st")
MC/DC50%1/2
builtin::blessed($sth) and $sth->isa("DBI::st")
builtin::blessed($sth)
$sth->isa("DBI::st")
Truth table66%2/3
A and B
A= builtin::blessed($sth)B= $sth->isa("DBI::st")
| |||||||||||||||||||||||||||||||||
| 91 | return ( | ||||||||||||||||||||||||||||||||
| 92 | _tags_dbh($sth->{Database}), | ||||||||||||||||||||||||||||||||
| 93 | 'db.statement' => $sth->{Statement}, | ||||||||||||||||||||||||||||||||
| 94 | 19 | ); | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | sub _execute { | ||||||||||||||||||||||||||||||||
| 98 | 19 | ▶ | my $sth = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_execute: called CC=5 SCAR=17.1
| |||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 19 | my $tracer = OpenTracing::GlobalTracer->get_global_tracer(); | |||||||||||||||||||||||||||||||
| 101 | 19 | my $scope = $tracer->start_active_span( | |||||||||||||||||||||||||||||||
| 102 | 'dbi_execute', | ||||||||||||||||||||||||||||||||
| 103 | tags => { TAGS_DEFAULT, _tags_sth($sth) }, | ||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||
| 105 | 19 | my $span = $scope->get_span(); | |||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 19 | my $result; | |||||||||||||||||||||||||||||||
| 108 | 19 | my $failed = !eval { $result = $sth->${ \_DBI_EXECUTE }(@_); 1 }; | |||||||||||||||||||||||||||||||
| 109 | 19 | my $error = $@; | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 19 | ▶ | if ($failed or not defined $result) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$failed or not defined $result
MC/DC0%0/2
$failed or not defined $result
$failed
not defined $result
Truth table33%1/3
A or not(B)
A= $failedB= not defined $result
| |||||||||||||||||||||||||||||||||
| 112 | 0 | $span->add_tag(error => 1); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | elsif ($sth->{NUM_OF_FIELDS} == 0) { # non-select statement | ||||||||||||||||||||||||||||||||
| 115 | 0 | $span->add_tag('db.rows' => $result + 0); | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | 19 | $scope->close(); | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 19 | ▶ | die $error if $failed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 19 | return $result; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | sub _gen_wrapper { | ||||||||||||||||||||||||||||||||
| 124 | 8 | ▶ | my ($method, $row_counter) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_gen_wrapper: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 125 | 8 | my $method_name = B::svref_2object($method)->GV->NAME; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | return sub { | ||||||||||||||||||||||||||||||||
| 128 | 42 | ▶ | my $dbh = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||
| 129 | 42 | my ($statement) = @_; | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 42 | my $tracer = OpenTracing::GlobalTracer->get_global_tracer(); | |||||||||||||||||||||||||||||||
| 132 | 42 | my $scope = $tracer->start_active_span("dbi_$method_name", | |||||||||||||||||||||||||||||||
| 133 | tags => { | ||||||||||||||||||||||||||||||||
| 134 | TAGS_DEFAULT, | ||||||||||||||||||||||||||||||||
| 135 | _tags_sth($statement), | ||||||||||||||||||||||||||||||||
| 136 | _tags_dbh($dbh), | ||||||||||||||||||||||||||||||||
| 137 | }, | ||||||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||||||
| 139 | 42 | my $span = $scope->get_span(); | |||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 42 | my $result; | |||||||||||||||||||||||||||||||
| 142 | 42 | my $wantarray = wantarray; # eval has its own | |||||||||||||||||||||||||||||||
| 143 | 42 | my $failed = !eval { | |||||||||||||||||||||||||||||||
| 144 | 42 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 5 | $result = [ $dbh->$method(@_) ]; | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||||||
| 148 | 37 | $result = $dbh->$method(@_); | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | 38 | 1; | |||||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||||||
| 152 | 42 | my $error = $@; | |||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 42 | ▶ | if ($failed or defined $dbh->err) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$failed or defined $dbh->err
MC/DC100%2/2
$failed or defined $dbh->err
$failed
defined $dbh->err
Truth table100%3/3
A or B
A= $failedB= defined $dbh->err
| |||||||||||||||||||||||||||||||||
| 155 | 10 | $span->add_tag(error => 1); | |||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | elsif (defined $row_counter) { | ||||||||||||||||||||||||||||||||
| 158 | 21 | ▶ | my $rows = sum(map { $row_counter->($_) } $wantarray ? @$result : $result); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | 21 | $span->add_tag('db.rows' => $rows); | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | 42 | $scope->close(); | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 42 | ▶ | die $error if $failed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 38 | ▶ | return $wantarray ? @$result : $result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | 8 | } | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | sub enable_temporarily { | ||||||||||||||||||||||||||||||||
| 169 | 4 | ▶ | return if $is_enabled; | ||||||||||||||||||||||||||||||
|
Subroutine
enable_temporarily: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 3 | enable(); | |||||||||||||||||||||||||||||||
| 172 | 3 | Scope::Context->up->reap(\&disable); | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | sub disable_temporarily { | ||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | return unless $is_enabled; | ||||||||||||||||||||||||||||||
|
Subroutine
disable_temporarily: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 2 | disable(); | |||||||||||||||||||||||||||||||
| 179 | 2 | Scope::Context->up->reap(\&enable); | |||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | sub suspend { | ||||||||||||||||||||||||||||||||
| 183 | 1 | ▶ | return if $is_suspended; | ||||||||||||||||||||||||||||||
|
Subroutine
suspend: called CC=2 SCAR=7.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 1 | my $was_enabled = $is_enabled; | |||||||||||||||||||||||||||||||
| 186 | 1 | disable(); | |||||||||||||||||||||||||||||||
| 187 | 1 | $is_suspended = 1; | |||||||||||||||||||||||||||||||
| 188 | 1 | ▶ | Scope::Context->up->reap(sub { $is_suspended = 0; enable() if $was_enabled }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | 2 | 1; | |||||||||||||||||||||||||||||||