File Coverage

blib/lib/Sentry/Tracing/Span.pm
Criterion Covered Total %
statement 66 66 100.0
branch 4 4 100.0
condition 1 2 50.0
subroutine 15 15 100.0
pod 0 7 0.0
total 86 94 91.4


line stmt bran cond sub pod time code
1             package Sentry::Tracing::Span;
2 9     9   287184 use Mojo::Base -base, -signatures;
  9         23  
  9         79  
3              
4 9     9   7312 use HTTP::Status qw(status_message);
  9         36815  
  9         1268  
5 9     9   5172 use Readonly;
  9         41474  
  9         664  
6 9     9   4726 use Sentry::Tracing::Status;
  9         31  
  9         88  
7 9     9   4539 use Sentry::Tracing::Transaction;
  9         28  
  9         107  
8 9     9   4678 use Sentry::Util qw(uuid4);
  9         56  
  9         799  
9 9     9   69 use Time::HiRes qw(time);
  9         14  
  9         93  
10              
11             Readonly my $SPAN_ID_LENGTH => 16;
12              
13             # https://develop.sentry.dev/sdk/unified-api/tracing
14              
15             # Hexadecimal string representing a uuid4 value. The length is exactly 32
16             # characters. Dashes are not allowed. Has to be lowercase
17             has span_id => sub { substr(uuid4(), 0, $SPAN_ID_LENGTH) };
18              
19             # Optional. A map or list of tags for this event. Each tag must be less than 200
20             # characters.
21             has tags => sub { {} };
22              
23             # Required. Determines which trace the Span belongs to. The value should be 16
24             # random bytes encoded as a hex string (32 characters long).
25             has trace_id => sub { uuid4() };
26              
27             # Recommended. Short code identifying the type of operation the span is
28             # measuring.
29             has op => undef;
30              
31             # Optional. Longer description of the span's operation, which uniquely
32             # identifies the span but is consistent across instances of the span.
33             has description => undef;
34              
35             # Required. A timestamp representing when the measuring started. The format is
36             # either a string as defined in RFC 3339 or a numeric (integer or float) value
37             # representing the number of seconds that have elapsed since the Unix epoch. The
38             # start_timestamp value must be greater or equal the timestamp value, otherwise
39             # the Span is discarded as invalid.
40             has start_timestamp => time;
41              
42             # Required. A timestamp representing when the measuring finished. The format is
43             # either a string as defined in RFC 3339 or a numeric (integer or float) value
44             # representing the number of seconds that have elapsed since the Unix epoch.
45             has timestamp => undef;
46              
47             # Optional. Describes the status of the Span/Transaction.
48             has status => undef;
49              
50             # Optional. Arbitrary data associated with this Span.
51             has data => undef;
52              
53             has parent_span_id => undef;
54              
55             # Was this span chosen to be sent as part of the sample?
56             has sampled => undef;
57              
58             has spans => sub { [] };
59             has transaction => undef;
60             has request => undef;
61              
62 10     10 0 6622 sub start_child ($self, $span_context = {}) {
  10         21  
  10         15  
  10         21  
63 10         50 my $child_span = Sentry::Tracing::Span->new({
64             $span_context->%*,
65             parent_span_id => $self->span_id,
66             sampled => $self->sampled,
67             trace_id => $self->trace_id,
68             start_timestamp => time,
69             });
70              
71 10         311 push $self->spans->@*, $child_span;
72              
73 10         30 $child_span->transaction($self->transaction);
74              
75 10         92 return $child_span;
76             }
77              
78 35     35 0 141 sub get_trace_context ($self) {
  35         59  
  35         54  
79             return {
80 35         100 data => $self->data,
81             description => $self->description,
82             op => $self->op,
83             parent_span_id => $self->parent_span_id,
84             span_id => $self->span_id,
85             status => $self->status,
86             tags => $self->tags,
87             trace_id => $self->trace_id,
88             };
89             }
90              
91 2     2 0 158 sub TO_JSON ($self) {
  2         5  
  2         4  
92             return {
93 2         8 data => $self->data,
94             description => $self->description,
95             op => $self->op,
96             parent_span_id => $self->parent_span_id,
97             span_id => $self->span_id,
98             start_timestamp => $self->start_timestamp,
99             status => $self->status,
100             tags => $self->tags,
101             timestamp => $self->timestamp,
102             trace_id => $self->trace_id,
103             };
104             }
105              
106 4     4 0 6144 sub to_trace_parent ($self) {
  4         9  
  4         25  
107 4         9 my $sampled_string = '';
108              
109 4 100       11 if (defined $self->sampled) {
110 2 100       14 $sampled_string = $self->sampled ? '-1' : '0';
111             }
112              
113 4         24 return $self->trace_id . '-' . $self->span_id . $sampled_string;
114             }
115              
116 14     14 0 343103 sub set_tag ($self, $key, $value) {
  14         54  
  14         28  
  14         31  
  14         22  
117 14         48 $self->tags({ $self->tags->%*, $key => $value });
118             }
119              
120 12     12 0 1489 sub set_http_status ($self, $status) {
  12         56  
  12         23  
  12         18  
121 12         83 $self->set_tag('http.status_code' => $status);
122 12         280 $self->status(Sentry::Tracing::Status->from_http_code($status));
123             }
124              
125 19     19 0 1833 sub finish ($self) {
  19         58  
  19         32  
126 19         140 $self->timestamp(time);
127             }
128              
129 13     13   261 sub _collect_spans ($self) {
  13         20  
  13         18  
130 13         44 my @spans = map { ($_, $_->_collect_spans()->@*) } $self->spans->@*;
  2         16  
131 13   50     75 return \@spans || [];
132             }
133              
134             1;