File Coverage

blib/lib/AWS/XRay/Segment.pm
Criterion Covered Total %
statement 32 35 91.4
branch 6 8 75.0
condition 1 3 33.3
subroutine 8 11 72.7
pod 0 3 0.0
total 47 60 78.3


line stmt bran cond sub pod time code
1             package AWS::XRay::Segment;
2              
3 13     13   222 use 5.012000;
  13         44  
4 13     13   57 use strict;
  13         24  
  13         210  
5 13     13   48 use warnings;
  13         29  
  13         319  
6              
7 13     13   7282 use JSON::XS ();
  13         33441  
  13         274  
8 13     13   67 use Time::HiRes ();
  13         20  
  13         3533  
9              
10             my $header = qq|{"format":"json","version":1}\n|;
11             my $json = JSON::XS->new;
12              
13             sub new {
14 4436     4436 0 5600 my $class = shift;
15 4436         4561 my $src = shift;
16              
17 4436 100       10238 return bless {}, "${class}::NoTrace" if !$AWS::XRay::ENABLED;
18              
19 2224         3528 my $segment = {
20             id => AWS::XRay::new_id(),
21             start_time => Time::HiRes::time(),
22             trace_id => $AWS::XRay::TRACE_ID,
23             %$src,
24             };
25 2224 100       53642 if (my $parent_id = $AWS::XRay::SEGMENT_ID) {
26             # This is a sub segment.
27 712         1130 $segment->{parent_id} = $parent_id;
28 712         1019 $segment->{type} = "subsegment";
29 712         932 $segment->{namespace} = "remote";
30             }
31 2224         4168 bless $segment, $class;
32             }
33              
34             # alias for backward compatibility
35             *send = \&close;
36              
37             sub close {
38 2224     2224 0 2430 my $self = shift;
39 2224   33     8063 $self->{end_time} //= Time::HiRes::time();
40 2224 50       4111 my $sock = AWS::XRay::sock() or return;
41 2224         86291 $sock->print($header, $json->encode({%$self}));
42             }
43              
44             sub trace_header {
45 2     2 0 11 my $self = shift;
46 2         11 my $h = sprintf("Root=%s;Parent=%s", $self->{trace_id}, $self->{id});
47 2 50       7 if (defined $AWS::XRay::SAMPLED) {
48 2         6 $h .= ";Sampled=$AWS::XRay::SAMPLED";
49             }
50 2         7 return $h;
51             }
52              
53             package AWS::XRay::Segment::NoTrace;
54              
55             sub new {
56 0     0     my $class = shift;
57 0           bless {}, $class;
58             }
59              
60       0     sub close {
61             # do not anything
62             }
63              
64             sub trace_header {
65 0     0     "";
66             }
67              
68             1;