blib/lib/OpenTracing/Integration/DBI.pm

filter: Statementstmt 73.8% 62 / 84 Branchbran 36.3% 8 / 22 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 2 totaltotal 62.9% 90 / 143 SCAR 35.3
CC
18
Cov
63%
__ANON__
20.0
load
8.2
__ANON__
6.0
CRAP 34.1
↑ Summary
1 package OpenTracing::Integration::DBI;
2 # ABSTRACT: OpenTracing APM support for DBI-based database interaction
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.001';
8 2 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
9  
10 2 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 OpenTracing::Integration::DBI - support L<DBI> tracing
18  
19 =head1 SYNOPSIS
20  
21 use OpenTracing::Integration qw(DBI);
22 my $dbh = DBI->connect(...);
23 $dbh->selectall_arrayref(qw{select * from information_schema.tables});
24  
25 =head1 DESCRIPTION
26  
27 See L<OpenTracing::Integration> for more details.
28  
29 =cut
30  
31 2 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Class::Method::Modifiers qw(install_modifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use OpenTracing::DSL qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 with qw(OpenTracing::Integration);
38  
39 2 my $loaded;
40  
41 sub type_from_sql {
42 4 ▶ my ($class, $sql) = @_;
Subroutine type_from_sql: called CC=1 SCAR=0.0
Pod undocumented
43 4 my ($type) = $sql =~ /\b(insert|select|update|delete|truncate\s+[a-z]+|copy\s+[a-z]+|show|vacuum|alter\s+[a-z]+|create\s+[a-z]+|drop\s+[a-z]+)\b/i;
44 4 return $type;
45 }
46  
47 sub load {
48 1 ▶ my ($class, $load_deps) = @_;
Subroutine load: called CC=5 SCAR=21.1
Pod undocumented
49 1 ▶ return unless $load_deps or DBI->can('connect');
Branch: 1 branch
BranchTrueFalseTotal
unless $load_deps or "DBI"->can("connect") 0 1 1
Condition33%1/3
$load_deps or "DBI"->can("connect")
l!l&&r!l&&!r
100
MC/DC0%0/2
$load_deps or "DBI"->can("connect")
$load_deps "DBI"->can("connect")
Truth table33%1/3
A or B
A= $load_depsB= "DBI"->can("connect")
ABresult
1X1
011
000
50  
51 1 ▶ unless($loaded++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded++) 1 0 1
52 1 require DBI;
53 install_modifier q{DBI::db}, around => prepare => sub {
54 1 ▶ my ($code, $dbh, $sql, @rest) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.9
55 1 my $type = $class->type_from_sql($sql);
56 return trace {
57 1 ▶ my ($span) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.5
58 try {
59 $span->tag(
60 'component' => 'DBI',
61 'span.kind' => 'client',
62 'db.operation' => 'prepare',
63 'db.statement' => $sql,
64 'db.type' => 'sql',
65 (defined $dbh->{Name} ? ('db.instance' => $dbh->{Name}) : ()),
66 1 ▶ (defined $dbh->{Username} ? ('db.user' => $dbh->{Username}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $dbh->{'Name'} ? : 1 0 1
defined $dbh->{'Username'} ? : 1 0 1
67 );
68 1 return $dbh->$code($sql, @rest);
69 1 } catch {
70 0 my $err = $@;
71 0 $span->tag(
72 error => 1,
73 );
74 0 die $@;
75 }
76 1 ▶ } operation_name => 'sql prepare: ' . ($type // 'unknown');
Condition50%1/2
$type // "unknown"
l!l
10
MC/DC0%0/1
$type // "unknown"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
77 1 };
78 install_modifier q{DBI::st}, around => execute => sub {
79 0 ▶ my ($code, $sth, @bind) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
80 0 my $sql = $sth->{Statement};
81 0 my $type = $class->type_from_sql($sql);
82 return trace {
83 0 ▶ my ($span) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
84 0 my $cursor = $sth->{CursorName};
85 0 my $dbh = $sth->{Database};
86 try {
87 $span->tag(
88 'component' => 'DBI',
89 'span.kind' => 'client',
90 'db.operation' => 'execute',
91 'db.statement' => $sql,
92 'db.type' => 'sql',
93 (defined $cursor ? ('db.cursor' => $cursor) : ()),
94 (defined $dbh->{Name} ? ('db.instance' => $dbh->{Name}) : ()),
95 0 ▶ (defined $dbh->{Username} ? ('db.user' => $dbh->{Username}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $cursor ? : 0 0 0
defined $dbh->{'Name'} ? : 0 0 0
defined $dbh->{'Username'} ? : 0 0 0
96 );
97 0 return $sth->$code(@bind);
98 0 } catch {
99 0 my $err = $@;
100 0 $span->tag(
101 error => 1,
102 );
103 0 die $@;
104 }
105 0 ▶ } operation_name => 'sql execute: ' . ($type // 'unknown');
Condition0%0/2
$type // "unknown"
l!l
00
MC/DC0%0/1
$type // "unknown"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
106 1 };
107 install_modifier q{DBI::db}, around => do => sub {
108 2 ▶ my ($code, $dbh, $sql, @rest) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.9
109 2 my $type = $class->type_from_sql($sql);
110 return trace {
111 2 ▶ my ($span) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.5
112 try {
113 $span->tag(
114 'component' => 'DBI',
115 'span.kind' => 'client',
116 'db.operation' => 'do',
117 'db.statement' => $sql,
118 'db.type' => 'sql',
119 (defined $dbh->{Name} ? ('db.instance' => $dbh->{Name}) : ()),
120 2 ▶ (defined $dbh->{Username} ? ('db.user' => $dbh->{Username}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $dbh->{'Name'} ? : 2 0 2
defined $dbh->{'Username'} ? : 2 0 2
121 );
122 2 return $dbh->$code($sql, @rest);
123 2 } catch {
124 0 my $err = $@;
125 0 $span->tag(
126 error => 1,
127 );
128 0 die $@;
129 }
130 2 ▶ } operation_name => 'sql do: ' . ($type // 'unknown');
Condition50%1/2
$type // "unknown"
l!l
20
MC/DC0%0/1
$type // "unknown"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
131 1 };
132 1 for my $op (qw(
133 selectall_arrayref
134 selectall_hashref
135 selectall_array
136 )) {
137 install_modifier q{DBI::db}, around => $op => sub {
138 1 ▶ my ($code, $dbh, $sql, @rest) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.9
139 1 my $type = $class->type_from_sql($sql);
140 return trace {
141 1 ▶ my ($span) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.5
142 try {
143 $span->tag(
144 'component' => 'DBI',
145 'span.kind' => 'client',
146 'db.operation' => 'selectall',
147 'db.statement' => $sql,
148 'db.type' => 'sql',
149 (defined $dbh->{Name} ? ('db.instance' => $dbh->{Name}) : ()),
150 1 ▶ (defined $dbh->{Username} ? ('db.user' => $dbh->{Username}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $dbh->{'Name'} ? : 1 0 1
defined $dbh->{'Username'} ? : 1 0 1
151 );
152 1 return $dbh->$code($sql, @rest);
153 1 } catch {
154 0 my $err = $@;
155 0 $span->tag(
156 error => 1,
157 );
158 0 die $@;
159 }
160 1 ▶ } operation_name => 'sql selectall: ' . ($type // 'unknown');
Condition50%1/2
$type // "unknown"
l!l
10
MC/DC0%0/1
$type // "unknown"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
161 3 };
162 }
163 }
164 }
165  
166 2 1;
167  
168 __END__
↑ Summary