blib/lib/AWS/XRay.pm

filter: Statementstmt 83.1% 133 / 160 Branchbran 56.9% 33 / 58 Conditioncond 50.0% 11 / 22 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 93.3% 28 / 30 Podpod 72.7% 8 / 11 totaltotal 73.2% 216 / 295 SCAR 40.8
CC
36
Cov
74%
auto_flush
30.0
capture
18.1
__ANON__
6.0
CRAP 59.4
↑ Summary blib/lib/AWS/XRay/Segment.pm »
1 package AWS::XRay;
2  
3 10 ▶use 5.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Types::Serialiser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use AWS::XRay::Segment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use AWS::XRay::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 our @EXPORT_OK = qw/ new_trace_id capture capture_from trace /;
17  
18 10 our $VERSION = "0.10";
19  
20 10 our $TRACE_ID;
21 10 our $SEGMENT_ID;
22 10 our $ENABLED;
23 10 our $SAMPLED;
24 10 our $SAMPLING_RATE = 1;
25 1020 ▶our $SAMPLER = sub { rand() < $SAMPLING_RATE };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 10 our $AUTO_FLUSH = 1;
27  
28 10 our @PLUGINS;
29  
30 10 our $DAEMON_HOST = "127.0.0.1";
31 10 our $DAEMON_PORT = 2000;
32  
33 10 ▶if ($ENV{"AWS_XRAY_DAEMON_ADDRESS"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"AWS_XRAY_DAEMON_ADDRESS"}) 0 10 10
34 0 ($DAEMON_HOST, $DAEMON_PORT) = split /:/, $ENV{"AWS_XRAY_DAEMON_ADDRESS"};
35 }
36  
37 10 my $Sock;
38  
39 sub sampling_rate {
40 14 ▶ my $class = shift;
Subroutine sampling_rate: called CC=2 SCAR=7.0
Pod documented
41 14 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 0 14
42 14 $SAMPLING_RATE = shift;
43 }
44 14 $SAMPLING_RATE;
45 }
46  
47 sub sampler {
48 4 ▶ my $class = shift;
Subroutine sampler: called CC=2 SCAR=7.0
Pod documented
49 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
50 4 $SAMPLER = shift;
51 }
52 4 $SAMPLER;
53 }
54  
55 sub plugins {
56 1 ▶ my $class = shift;
Subroutine plugins: called CC=3 SCAR=11.1
Pod undocumented
57 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
58 1 @PLUGINS = @_;
59 1 Module::Load::load $_ for @PLUGINS;
60 }
61 1 @PLUGINS;
62 }
63  
64 sub auto_flush {
65 0 ▶ my $class = shift;
Subroutine auto_flush: not called CC=5 SCAR=34.0
Pod documented
66 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
67 0 my $auto_flush = shift;
68 0 ▶ if ($auto_flush != $AUTO_FLUSH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_flush != $AWS::XRay::AUTO_FLUSH) 0 0 0
69 0 ▶ $Sock->close if $Sock && $Sock->can("close");
Branch: 1 branch
BranchTrueFalseTotal
if $Sock and $Sock->can("close") 0 0 0
Condition0%0/3
$Sock and $Sock->can("close")
!ll&&!rl&&r
000
MC/DC0%0/2
$Sock and $Sock->can("close")
$Sock $Sock->can("close")
Truth table0%0/3
A and B
A= $SockB= $Sock->can("close")
ABresult
0X0
100
111
70 0 undef $Sock; # regenerate
71 }
72 0 $AUTO_FLUSH = $auto_flush;
73 }
74 0 $AUTO_FLUSH;
75 }
76  
77 sub sock {
78 3 ▶ $Sock //= AWS::XRay::Buffer->new(
Subroutine sock: called CC=4 SCAR=15.8
Pod undocumented
Condition57%4/7
$Sock //= "AWS::XRay::Buffer"->new("IO::Socket::INET"->new("PeerAddr", $AWS::XRay::DAEMON_HOST || "127.0.0.1", "PeerPort", $AWS::XRay::DAEMON_PORT || 2000, "Proto", "udp"), $AWS::XRay::AUTO_FLUSH)
l!l&&r!l&&!r
210
$AWS::XRay::DAEMON_HOST || '127.0.0.1'
l!l
10
$AWS::XRay::DAEMON_PORT || 2000
l!l
10
MC/DC0%0/2
$Sock //= "AWS::XRay::Buffer"->new("IO::Socket::INET"->new("PeerAddr", $AWS::XRay::DAEMON_HOST || "127.0.0.1", "PeerPort", $AWS::XRay::DAEMON_PORT || 2000, "Proto", "udp"), $AWS::XRay::AUTO_FLUSH)
$Sock "AWS::XRay::Buffer"->new("IO::Socket::INET"->new("PeerAddr", $AWS::XRay::DAEMON_HOST || "127.0.0.1", "PeerPort", $AWS::XRay::DAEMON_PORT || 2000, "Proto", "udp"), $AWS::XRay::AUTO_FLUSH)
MC/DC0%0/1
$AWS::XRay::DAEMON_HOST || '127.0.0.1'
$AWS::XRay::DAEMON_HOST
MC/DC0%0/1
$AWS::XRay::DAEMON_PORT || 2000
$AWS::XRay::DAEMON_PORT
Truth table66%2/3
A //= B
A= $SockB= "AWS::XRay::Buffer"->new("IO::Socket::INET"->new("PeerAddr", $AWS::XRay::DAEMON_HOST || "127.0.0.1", "PeerPort", $AWS::XRay::DAEMON_PORT || 2000, "Proto", "udp"), $AWS::XRay::AUTO_FLUSH)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $AWS::XRay::DAEMON_HOST
Aresult
11
00
Truth table50%1/2
A
A= $AWS::XRay::DAEMON_PORT
Aresult
11
00
79 IO::Socket::INET->new(
80 PeerAddr => $DAEMON_HOST || "127.0.0.1",
81 PeerPort => $DAEMON_PORT || 2000,
82 Proto => "udp",
83 ),
84 $AUTO_FLUSH,
85 );
86 }
87  
88 sub new_trace_id {
89 1511 ▶ sprintf(
Subroutine new_trace_id: called CC=1 SCAR=0.0
Pod documented
90 "1-%x-%s",
91 CORE::time(),
92 unpack("H*", Crypt::URandom::urandom(12)),
93 );
94 }
95  
96 sub new_id {
97 2222 ▶ unpack("H*", Crypt::URandom::urandom(8))
Subroutine new_id: called CC=1 SCAR=0.0
Pod undocumented
98 }
99  
100 # alias for backward compatibility
101 10 *trace = \&capture;
102  
103 sub capture {
104 4433 ▶ my ($name, $code) = @_;
Subroutine capture: called CC=17 SCAR=28.9
Pod documented
105 4433 my $wantarray = wantarray;
106  
107 4433 my $enabled;
108 4433 my $sampled = $SAMPLED;
109 4433 ▶ if (defined $ENABLED) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $AWS::XRay::ENABLED) { } 2412 2021 4433
elsif ($AWS::XRay::TRACE_ID) { } 0 2021 2021
110 2412 ▶ $enabled = $ENABLED ? 1 : 0; # fix true or false (not undef)
Branch: 1 branch
BranchTrueFalseTotal
$AWS::XRay::ENABLED ? : 1212 1200 2412
111 } elsif ($TRACE_ID) {
112 0 $enabled = 0; # called from parent capture
113 } else {
114 # root capture try sampling
115 2021 ▶ $sampled = $SAMPLER->() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&$AWS::XRay::SAMPLER() ? : 1009 1012 2021
116 2021 ▶ $enabled = $sampled ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sampled ? : 1009 1012 2021
117 }
118 4433 local $ENABLED = $enabled;
119 4433 local $SAMPLED = $sampled;
120  
121 4433 ▶ return $code->(AWS::XRay::Segment->new) if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 2212 2221 4433
122  
123 2221 ▶ local $TRACE_ID = $TRACE_ID // new_trace_id();
Condition66%2/3
$AWS::XRay::TRACE_ID // new_trace_id()
l!l&&r!l&&!r
71115100
MC/DC0%0/2
$AWS::XRay::TRACE_ID // new_trace_id()
$AWS::XRay::TRACE_ID new_trace_id()
Truth table66%2/3
A // B
A= $AWS::XRay::TRACE_IDB= new_trace_id()
ABresult
1X1
011
000
124  
125 2221 my $segment = AWS::XRay::Segment->new({ name => $name });
126 2221 ▶ unless (defined $segment->{type} && $segment->{type} eq "subsegment") {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $segment->{"type"} and $segment->{"type"} eq "subsegment") 1510 711 2221
Condition66%2/3
defined $segment->{"type"} and $segment->{"type"} eq "subsegment"
!ll&&!rl&&r
15100711
MC/DC50%1/2
defined $segment->{"type"} and $segment->{"type"} eq "subsegment"
defined $segment->{"type"} $segment->{"type"} eq "subsegment"
Truth table66%2/3
A and B
A= defined $segment->{"type"}B= $segment->{"type"} eq "subsegment"
ABresult
0X0
100
111
127 1510 $_->apply_plugin($segment) for @PLUGINS;
128 }
129  
130 2221 local $SEGMENT_ID = $segment->{id};
131  
132 2221 my @ret;
133 2221 eval {
134 2221 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 1 2220 2221
elsif (defined $wantarray) { } 3 2217 2220
135 1 @ret = $code->($segment);
136 }
137 elsif (defined $wantarray) {
138 3 $ret[0] = $code->($segment);
139 }
140 else {
141 2217 $code->($segment);
142 }
143 };
144 2221 my $error = $@;
145 2221 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 2221 2221
146 0 $segment->{error} = Types::Serialiser::true;
147 $segment->{cause} = {
148 0 exceptions => [
149 {
150 id => new_id(),
151 message => "$error",
152 remote => Types::Serialiser::true,
153 },
154 ],
155 };
156 }
157 2221 eval {
158 2221 $segment->close();
159 };
160 2221 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2221 2221
161 0 warn $@;
162 }
163 2221 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 2221 2221
164 2221 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 2220 2221
165 }
166  
167 sub capture_from {
168 1004 ▶ my ($header, $name, $code) = @_;
Subroutine capture_from: called CC=2 SCAR=6.9
Pod documented
169 1004 my ($trace_id, $segment_id, $sampled) = parse_trace_header($header);
170  
171 1004 ▶ local $AWS::XRay::SAMPLED = $sampled // $SAMPLER->();
Condition100%3/3
$sampled // &$AWS::XRay::SAMPLER()
l!l&&r!l&&!r
3501500
MC/DC100%2/2
$sampled // &$AWS::XRay::SAMPLER()
$sampled &$AWS::XRay::SAMPLER()
Truth table100%3/3
A // B
A= $sampledB= &$AWS::XRay::SAMPLER()
ABresult
1X1
011
000
172 1004 local $AWS::XRay::ENABLED = $AWS::XRay::SAMPLED;
173 1004 local($AWS::XRay::TRACE_ID, $AWS::XRay::SEGMENT_ID) = ($trace_id, $segment_id);
174 1004 capture($name, $code);
175 }
176  
177 sub parse_trace_header {
178 1004 ▶ my $header = shift or return;
Subroutine parse_trace_header: called CC=5 SCAR=16.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $header = shift() or return 1001 3 1004
179  
180 3 my ($trace_id, $segment_id, $sampled);
181 3 ▶ if ($header =~ /Root=([0-9a-fA-F-]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /Root=([0-9a-fA-F-]+)/u) 3 0 3
182 3 $trace_id = $1;
183 }
184 3 ▶ if ($header =~ /Parent=([0-9a-fA-F]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /Parent=([0-9a-fA-F]+)/u) 3 0 3
185 3 $segment_id = $1;
186 }
187 3 ▶ if ($header =~ /Sampled=([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /Sampled=([^;]+)/u) 3 0 3
188 3 $sampled = $1;
189 }
190 3 return ($trace_id, $segment_id, $sampled);
191 }
192  
193 sub add_capture {
194 2 ▶ my ($class, $package, @methods) = @_;
Subroutine add_capture: called CC=3 SCAR=11.0
Pod documented
195 10 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
196 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
197 2 for my $method (@methods) {
198 2 ▶ my $orig = $package->can($method) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $orig = $package->can($method) or next 0 2 2
199 2 *{"${package}::${method}"} = sub {
200 2 ▶ my @args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
201 capture(
202 $package,
203 sub {
204 2 ▶ my $segment = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
205 2 $segment->{metadata}->{method} = $method;
206 2 $segment->{metadata}->{package} = $package;
207 2 $orig->(@args);
208 },
209 2 );
210 2 };
211 }
212 }
213  
214 10 ▶if ($ENV{LAMBDA_TASK_ROOT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LAMBDA_TASK_ROOT"}) 0 10 10
215 # AWS::XRay is loaded in AWS Lambda worker.
216 # notify the Lambda Runtime that initialization is complete.
217 0 ▶ mkdir '/tmp/.aws-xray' or warn "failed to make directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir "/tmp/.aws-xray" or warn "failed to make directory: $!" 0 0 0
218 0 ▶ open my $fh, '>', '/tmp/.aws-xray/initialized' or warn "failed to create file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "/tmp/.aws-xray/initialized" or warn "failed to create file: $!" 0 0 0
219 0 close $fh;
220 0 ▶ utime undef, undef, '/tmp/.aws-xray/initialized' or warn "failed to touch file: $!";
Branch: 1 branch
BranchTrueFalseTotal
utime undef, undef, "/tmp/.aws-xray/initialized" or warn "failed to touch file: $!" 0 0 0
221  
222 # patch the capture
223 10 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
224 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
225 0 my $org = \&capture;
226 *capture = sub {
227 0 ▶ my ($trace_id, $segment_id, $sampled) = parse_trace_header($ENV{_X_AMZN_TRACE_ID});
Subroutine __ANON__: not called CC=2 SCAR=17.9
228 0 ▶ local $AWS::XRay::SAMPLED = $sampled // $SAMPLER->();
Condition0%0/3
$sampled // &$AWS::XRay::SAMPLER()
l!l&&r!l&&!r
000
MC/DC0%0/2
$sampled // &$AWS::XRay::SAMPLER()
$sampled &$AWS::XRay::SAMPLER()
Truth table0%0/3
A // B
A= $sampledB= &$AWS::XRay::SAMPLER()
ABresult
1X1
011
000
229 0 local $AWS::XRay::ENABLED = $AWS::XRay::SAMPLED;
230 0 local($AWS::XRay::TRACE_ID, $AWS::XRay::SEGMENT_ID) = ($trace_id, $segment_id);
231 0 local *capture = $org;
232 0 local *trace = $org;
233 0 capture(@_);
234 0 };
235 0 *trace = \&capture;
236 }
237  
238 10 1;
239 __END__
↑ Summary blib/lib/AWS/XRay/Segment.pm »