OpenTracing/Span.pm

filter: Statementstmt 56.5% 39 / 69 Branchbran 33.3% 2 / 6 Conditioncond 24.2% 8 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 46.4% 13 / 28 Podpod 95.2% 20 / 21 totaltotal 46.8% 82 / 175 CC 19 SCAR 15.3
CC
19
Cov
45%
duration
12.0
log
12.0
parent_id
6.0
CRAP 4.6
« OpenTracing/Batch.pm ↑ Summary OpenTracing/Process.pm »
1 package OpenTracing::Span;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '1.006'; # VERSION
7 11 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 11 ▶use parent qw(OpenTracing::Common);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 OpenTracing::Span - represents an operation or parent operation
19  
20 =head1 DESCRIPTION
21  
22 The reference definition for a span is a good starting point for understanding these:
23  
24 L<https://opentracing.io/docs/overview/spans/>
25  
26 =cut
27  
28 11 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use Bytes::Random::Secure qw(random_bytes_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head2 new
32  
33 Instantiates a new span. Rarely called directly - you'd want to let the L<OpenTracing::Tracer> take
34 care of this for you.
35  
36 Takes the following named parameters:
37  
38 =over 4
39  
40 =item * C<parent> - an L<OpenTracing::Span> for a parent instance (optional)
41  
42 =item * C<parent_id> - the span ID for the parent (optional)
43  
44 =item * C<trace_id> - the current trace ID (optional)
45  
46 =item * C<operation_name> - what to use for this span name
47  
48 =back
49  
50 =cut
51  
52 203 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Pod documented
53 203 my ($class, %args) = @_;
54 203 ▶ $args{operation_name} //= (caller 1)[3];
Condition66%2/3
$args{"operation_name"} //= (caller 1)[3]
l!l&&r!l&&!r
12020
MC/DC0%0/2
$args{"operation_name"} //= (caller 1)[3]
$args{"operation_name"} (caller 1)[3]
Truth table66%2/3
A //= B
A= $args{"operation_name"}B= (caller 1)[3]
ABresult
1X1
011
000
55 203 ▶ if(my $parent = $args{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $args{"parent"}) 0 203 203
56 0 $args{parent_id} = $parent->{id};
57 0 $args{trace_id} = $parent->{trace_id};
58 }
59  
60 # Alternatively reduce { $a * 1_000_000 + $b } Time::HiRes::gettimeofday(),
61 # but the time() version benchmarks to ~3x faster
62 203 ▶ $args{start_time} //= int(Time::HiRes::time() * 1_000_000);
Condition50%1/2
$args{"start_time"} //= int &Time::HiRes::time() * 1000000
l!l
0203
MC/DC0%0/1
$args{"start_time"} //= int &Time::HiRes::time() * 1000000
$args{"start_time"}
Truth table50%1/2
A
A= $args{"start_time"}
Aresult
11
00
63 203 bless \%args, $class
64 }
65  
66 =head2 trace_id
67  
68 The trace ID for this span. Multiple spans are grouped under a single trace.
69  
70 =cut
71  
72 102 ▶sub trace_id { shift->{trace_id} //= random_bytes_hex(16) }
Subroutine trace_id: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
+(shift())->{"trace_id"} //= random_bytes_hex(16)
l!l&&r!l&&!r
11010
MC/DC0%0/2
+(shift())->{"trace_id"} //= random_bytes_hex(16)
+(shift())->{"trace_id"} random_bytes_hex(16)
Truth table66%2/3
A //= B
A= +(shift())->{"trace_id"}B= random_bytes_hex(16)
ABresult
1X1
011
000
73  
74 =head2 id
75  
76 The span ID. This should be unique.
77  
78 =cut
79  
80 102 ▶sub id { shift->{id} //= random_bytes_hex(8) }
Subroutine id: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
+(shift())->{"id"} //= random_bytes_hex(8)
l!l&&r!l&&!r
11010
MC/DC0%0/2
+(shift())->{"id"} //= random_bytes_hex(8)
+(shift())->{"id"} random_bytes_hex(8)
Truth table66%2/3
A //= B
A= +(shift())->{"id"}B= random_bytes_hex(8)
ABresult
1X1
011
000
81  
82 =head2 parent_id
83  
84 Parent span ID. 0 if there isn't one.
85  
86 =cut
87  
88 0 ▶sub parent_id { shift->{parent_id} //= '0' x 8; }
Subroutine parent_id: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"parent_id"} //= "00000000"
l!l
00
MC/DC0%0/1
+(shift())->{"parent_id"} //= "00000000"
+(shift())->{"parent_id"}
Truth table0%0/2
A
A= +(shift())->{"parent_id"}
Aresult
11
00
89  
90 =head2 flags
91  
92 Any flags relating to this span.
93  
94 =cut
95  
96 0 ▶sub flags { shift->{flags} // 0 }
Subroutine flags: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"flags"} // 0
l!l
00
MC/DC0%0/1
+(shift())->{"flags"} // 0
+(shift())->{"flags"}
Truth table0%0/2
A
A= +(shift())->{"flags"}
Aresult
11
00
97  
98 =head2 start_time
99  
100 Exact time this span started, in microseconds.
101  
102 =cut
103  
104 0 ▶sub start_time { shift->{start_time} //= int(Time::HiRes::time() * 1_000_000) }
Subroutine start_time: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"start_time"} //= int &Time::HiRes::time() * 1000000
l!l
00
MC/DC0%0/1
+(shift())->{"start_time"} //= int &Time::HiRes::time() * 1000000
+(shift())->{"start_time"}
Truth table0%0/2
A
A= +(shift())->{"start_time"}
Aresult
11
00
105  
106 =head2 start_time
107  
108 Exact time this span finished, in microseconds.
109  
110 Returns C<undef> if the span is not yet finished.
111  
112 =cut
113  
114 0 ▶sub finish_time { shift->{finish_time} }
Subroutine finish_time: not called CC=1 SCAR=6.9
Pod undocumented
115  
116 =head2 duration
117  
118 Total duration of this span, including any nested spans.
119  
120 =cut
121  
122 0 ▶sub duration {
Subroutine duration: not called CC=3 SCAR=24.8
Pod documented
123 0 my ($self) = @_;
124 0 ▶ return undef unless defined $self->{finish_time};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"finish_time"} 0 0 0
125 0 ▶ $self->{duration} //= $self->finish_time - $self->start_time;
Condition0%0/2
$self->{"duration"} //= $self->finish_time - $self->start_time
l!l
00
MC/DC0%0/1
$self->{"duration"} //= $self->finish_time - $self->start_time
$self->{"duration"}
Truth table0%0/2
A
A= $self->{"duration"}
Aresult
11
00
126 }
127  
128 =head2 operation_name
129  
130 The operation that this span represents.
131  
132 =cut
133  
134 0 ▶sub operation_name { shift->{operation_name} }
Subroutine operation_name: not called CC=1 SCAR=6.9
Pod documented
135  
136 =head2 tags
137  
138 The tags relating to this span.
139  
140 =cut
141  
142 0 ▶sub tags { shift->{tags} }
Subroutine tags: not called CC=1 SCAR=6.9
Pod documented
143  
144 =head2 tag_list
145  
146 A list of tags as L<OpenTracing::Tag> instances.
147  
148 =cut
149  
150 0 ▶sub tag_list {
Subroutine tag_list: not called CC=2 SCAR=17.9
Pod documented
151 0 ▶ my $tags = shift->{tags} //= {};
Condition0%0/2
+(shift())->{"tags"} //= {}
l!l
00
MC/DC0%0/1
+(shift())->{"tags"} //= {}
+(shift())->{"tags"}
Truth table0%0/2
A
A= +(shift())->{"tags"}
Aresult
11
00
152 0 map { OpenTracing::Tag->new(key => $_, value => $tags->{$_}) } sort keys %$tags;
153 }
154  
155 =head2 logs
156  
157 The arrayref of log entries for this span, as L<OpenTracing::Log> instances.
158  
159 =cut
160  
161 0 ▶sub logs { shift->{logs} }
Subroutine logs: not called CC=1 SCAR=6.9
Pod documented
162  
163 =head2 log_list
164  
165 A list of log entries for this span, as L<OpenTracing::Log> instances.
166  
167 =cut
168  
169 0 ▶sub log_list {
Subroutine log_list: not called CC=2 SCAR=17.9
Pod documented
170 0 ▶ (shift->{logs} //= [])->@*
Condition0%0/2
+(shift())->{"logs"} //= []
l!l
00
MC/DC0%0/1
+(shift())->{"logs"} //= []
+(shift())->{"logs"}
Truth table0%0/2
A
A= +(shift())->{"logs"}
Aresult
11
00
171 }
172  
173 =head2 log
174  
175 Records a single log message.
176  
177 =cut
178  
179 0 ▶sub log : method {
Subroutine log: not called CC=3 SCAR=24.8
Pod documented
180 0 my ($self, $message, %args) = @_;
181 0 $args{message} = $message;
182 0 ▶ my $timestamp = delete($args{timestamp}) // int(Time::HiRes::time() * 1_000_000);
Condition0%0/2
delete $args{"timestamp"} // int &Time::HiRes::time() * 1000000
l!l
00
MC/DC0%0/1
delete $args{"timestamp"} // int &Time::HiRes::time() * 1000000
delete $args{"timestamp"}
Truth table0%0/2
A
A= delete $args{"timestamp"}
Aresult
11
00
183 0 ▶ push +($self->{logs} //= [])->@*, my $log = OpenTracing::Log->new(
Condition0%0/2
$self->{"logs"} //= []
l!l
00
MC/DC0%0/1
$self->{"logs"} //= []
$self->{"logs"}
Truth table0%0/2
A
A= $self->{"logs"}
Aresult
11
00
184 tags => \%args,
185 timestamp => $timestamp
186 );
187 0 $log;
188 }
189  
190 =head2 tag
191  
192 Applies key/value tags to this span.
193  
194 The L<semantic conventions|https://github.com/opentracing/specification/blob/master/semantic_conventions.md>
195 may be of interest here.
196  
197 Example usage:
198  
199 $span->tag(
200 'http.status_code' => 200,
201 'http.url' => 'https://localhost/xxx',
202 'http.method' => 'GET'
203 );
204  
205 =cut
206  
207 0 ▶sub tag : method {
Subroutine tag: not called CC=1 SCAR=6.9
Pod documented
208 0 my ($self, %args) = @_;
209 0 @{$self->{tags}}{keys %args} = values %args;
210 0 return $self;
211 }
212  
213 =head2 references
214  
215 The references relating to this span.
216  
217 =cut
218  
219 0 ▶sub references { shift->{references} }
Subroutine references: not called CC=1 SCAR=6.9
Pod documented
220  
221 =head2 reference_list
222  
223 A list of reference entries for this span, as L<OpenTracing::Reference> instances.
224  
225 =cut
226  
227 0 ▶sub reference_list {
Subroutine reference_list: not called CC=2 SCAR=17.9
Pod documented
228 0 ▶ (shift->{references} //= [])->@*
Condition0%0/2
+(shift())->{"references"} //= []
l!l
00
MC/DC0%0/1
+(shift())->{"references"} //= []
+(shift())->{"references"}
Truth table0%0/2
A
A= +(shift())->{"references"}
Aresult
11
00
229 }
230  
231  
232 =head2 reference
233  
234 Records a reference.
235  
236 =cut
237  
238 0 ▶sub reference : method {
Subroutine reference: not called CC=2 SCAR=17.9
Pod documented
239 0 my ($self, %args) = @_;
240 0 ▶ push +($self->{references} //= [])->@*, my $reference = OpenTracing::Reference->new(%args);
Condition0%0/2
$self->{"references"} //= []
l!l
00
MC/DC0%0/1
$self->{"references"} //= []
$self->{"references"}
Truth table0%0/2
A
A= $self->{"references"}
Aresult
11
00
241 0 $reference;
242 }
243  
244 =head2 tracer
245  
246 Returns the L<OpenTracing::Tracer> for this span.
247  
248 =cut
249  
250 1 ▶sub tracer { shift->{tracer} }
Subroutine tracer: called CC=1 SCAR=0.0
Pod documented
251  
252 =head2 is_finished
253  
254 Returns true if this span is finished (has a L</finish_time>), otherwise false.
255  
256 =cut
257  
258 2 ▶sub is_finished { defined shift->{finish_time} }
Subroutine is_finished: called CC=1 SCAR=0.0
Pod documented
259  
260 =head2 finish
261  
262 Mark this span as finished (populating the L</finish_time> field).
263  
264 =cut
265  
266 1 ▶sub finish {
Subroutine finish: called CC=3 SCAR=11.2
Pod documented
267 1 my ($self, $ts) = @_;
268 1 ▶ unless($self->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_finished) 1 0 1
269 1 ▶ $ts //= int(Time::HiRes::time * 1_000_000);
Condition50%1/2
$ts //= int &Time::HiRes::time() * 1000000
l!l
01
MC/DC0%0/1
$ts //= int &Time::HiRes::time() * 1000000
$ts
Truth table50%1/2
A
A= $ts
Aresult
11
00
270 1 $self->{finish_time} = $ts;
271 1 $self->tracer->finish_span($self);
272 }
273 $self
274 1 }
275  
276 11 1;
277  
278 __END__
« OpenTracing/Batch.pm ↑ Summary OpenTracing/Process.pm »