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 10     10   201 use 5.012000;
  10         36  
4 10     10   51 use strict;
  10         19  
  10         193  
5 10     10   46 use warnings;
  10         20  
  10         293  
6              
7 10     10   6682 use JSON::XS ();
  10         30357  
  10         246  
8 10     10   63 use Time::HiRes ();
  10         20  
  10         3142  
9              
10             my $header = qq|{"format":"json","version":1}\n|;
11             my $json = JSON::XS->new;
12              
13             sub new {
14 4433     4433 0 6938 my $class = shift;
15 4433         5768 my $src = shift;
16              
17 4433 100       12130 return bless {}, "${class}::NoTrace" if !$AWS::XRay::ENABLED;
18              
19 2221         4595 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 2221 100       65430 if (my $parent_id = $AWS::XRay::SEGMENT_ID) {
26             # This is a sub segment.
27 711         1635 $segment->{parent_id} = $parent_id;
28 711         1133 $segment->{type} = "subsegment";
29 711         1108 $segment->{namespace} = "remote";
30             }
31 2221         5183 bless $segment, $class;
32             }
33              
34             # alias for backward compatibility
35             *send = \&close;
36              
37             sub close {
38 2221     2221 0 2939 my $self = shift;
39 2221   33     9836 $self->{end_time} //= Time::HiRes::time();
40 2221 50       4578 my $sock = AWS::XRay::sock() or return;
41 2221         103098 $sock->print($header, $json->encode({%$self}));
42             }
43              
44             sub trace_header {
45 2     2 0 16 my $self = shift;
46 2         11 my $h = sprintf("Root=%s;Parent=%s", $self->{trace_id}, $self->{id});
47 2 50       9 if (defined $AWS::XRay::SAMPLED) {
48 2         7 $h .= ";Sampled=$AWS::XRay::SAMPLED"
49             }
50 2         8 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              
69             1;