Role/Span.pm

filter: Statementstmt 96.0% 96 / 100 Branchbran 78.1% 25 / 32 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 96.1% 25 / 26 Podpod 7.6% 1 / 13 totaltotal 82.9% 151 / 182 CC 20 SCAR 9.3
CC
20
Cov
90%
parent_span_id
6.0
duration
5.1
finish
4.1
CRAP 2.5
« Role/Scope.pm ↑ Summary Role/ScopeManager.pm »
1 package OpenTracing::Role::Span;
2  
3 =head1 NAME
4  
5 OpenTracing::Role::Span - Role for OpenTracing implementations.
6  
7 =head1 SYNOPSIS
8  
9 package OpenTracing::Implementation::MyBackendService::Span;
10
11 use Moo;
12
13 ...
14
15 with 'OpenTracing::Role::Span'
16
17 1;
18  
19 =cut
20  
21  
22  
23 15 our $VERSION = '0.08_003';
24  
25  
26  
27 15 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 15 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 15 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 15 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 15 ▶use Types::Standard qw/CodeRef HashRef Maybe Num Object Str Value/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 15 ▶use Types::Common::Numeric qw/PositiveOrZeroNum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 15 ▶use OpenTracing::Types qw/:types :is/;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38  
39 =head1 DESCRIPTION
40  
41 This is a Role for OpenTracing implenetations that are compliant with the
42 L<OpenTracing::Interface>.
43  
44 With the exception of calls to C<get_context()> (which are always allowed),
45 C<finish()> must be the last call made to any span instance, and to do otherwise
46 leads to undefined behavior (but not returning an exception).
47  
48 =cut
49  
50  
51  
52 has span_id => (
53 is => 'ro',
54 # isa => Uuid, # not restraints here, do so when consuming this
55 init_arg => undef,
56 17 ▶ default => sub { Data::GUID->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 15 );
58  
59  
60  
61 15 has operation_name => (
62 is => 'rwp',
63 isa => Str,
64 required => 1,
65 # writer => 'overwrite_operation_name',
66 reader => 'get_operation_name', # it's not in the Interface
67 );
68  
69  
70  
71 has start_time => (
72 is => 'ro',
73 isa => PositiveOrZeroNum,
74 17 ▶ default => sub { epoch_floatingpoint() }
Subroutine __ANON__: called CC=1 SCAR=0.0
75 15 );
76  
77  
78  
79 15 has finish_time => (
80 is => 'rwp',
81 isa => PositiveOrZeroNum,
82 predicate => 'has_finished',
83 init_arg => undef,
84 );
85  
86  
87  
88 has tags => (
89 is => 'rwp',
90 isa => HashRef[Value],
91 handles_via => 'Hash',
92 handles => {
93 get_tags => 'all',
94 },
95 14 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 15 );
97  
98  
99  
100 15 has context => (
101 is => 'ro',
102 isa => SpanContext,
103 reader => 'get_context',
104 # writer => '_set_context',
105 required => 1, # either from Span->get_context or SpanContext self
106 );
107  
108  
109  
110 sub overwrite_operation_name {
111 2 ▶ my $self = shift;
Subroutine overwrite_operation_name: called CC=2 SCAR=6.9
Pod undocumented
112
113 2 ▶ croak "Can't overwrite an operation-name on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 1 2
114 if $self->has_finished;
115
116 1 my $operation_name = shift; # or throw an exception
117
118 1 $self->_set_operation_name( $operation_name );
119
120 1 return $self
121 }
122  
123  
124  
125 sub finish {
126 31 ▶ my $self = shift;
Subroutine finish: called CC=4 SCAR=14.0
Pod undocumented
127
128 31 ▶ croak "Span has already been finished"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 30 31
129 if $self->has_finished;
130
131 30 ▶ my $epoch_timestamp = shift // epoch_floatingpoint();
Condition66%2/3
shift() // epoch_floatingpoint()
l!l&&r!l&&!r
2280
MC/DC0%0/2
shift() // epoch_floatingpoint()
shift() epoch_floatingpoint()
Truth table66%2/3
A // B
A= shift()B= epoch_floatingpoint()
ABresult
1X1
011
000
132
133 30 $self->_set_finish_time( $epoch_timestamp );
134
135 30 ▶ $self->on_finish->( $self ) # we do like to have $self as invocant
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_on_finish 0 30 30
136 if $self->has_on_finish;
137 30 return $self
138 }
139  
140  
141  
142 sub set_tag {
143 3 ▶ my $self = shift;
Subroutine set_tag: called CC=2 SCAR=6.9
Pod undocumented
144
145 3 ▶ croak "Can't set a tag on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 2 3
146 if $self->has_finished;
147
148 2 my $key = shift;
149 2 my $value = shift;
150
151 2 $self->set_tags( $key => $value );
152
153 2 return $self
154 }
155  
156  
157  
158 sub set_tags {
159 3 ▶ my $self = shift;
Subroutine set_tags: called CC=2 SCAR=7.0
Pod undocumented
160
161 3 ▶ croak "Can't set a tag on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 0 3 3
162 if $self->has_finished;
163
164 3 my %tags = @_;
165 3 $self->_set_tags(
166 { $self->get_tags, %tags }
167 );
168
169 3 return $self
170 }
171  
172  
173  
174 sub log_data {
175 2 ▶ my $self = shift;
Subroutine log_data: called CC=2 SCAR=6.9
Pod undocumented
176
177 2 ▶ croak "Can't log any more data on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 1 2
178 if $self->has_finished;
179
180 1 my %log_data = @_;
181
182 # ... # shall we just use Log::Any ?
183
184 1 return $self
185 }
186  
187  
188  
189 sub set_baggage_item {
190 2 ▶ my $self = shift;
Subroutine set_baggage_item: called CC=2 SCAR=6.9
Pod undocumented
191
192 2 ▶ croak "Can't set baggage-items on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 1 2
193 if $self->has_finished;
194
195 1 my $key = shift;
196 1 my $value = shift;
197
198 1 $self->set_baggage_items( $key => $value );
199
200 1 return $self
201 }
202  
203  
204  
205 sub set_baggage_items {
206 3 ▶ my $self = shift;
Subroutine set_baggage_items: called CC=2 SCAR=6.9
Pod undocumented
207
208 3 ▶ croak "Can't set baggage-items on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 2 3
209 if $self->has_finished;
210
211 2 my %items = @_;
212
213 2 my $new_context = $self->get_context()->with_baggage_items( %items );
214 2 $self->_set_context( $new_context );
215
216 2 return $self
217 }
218  
219  
220  
221 sub get_baggage_item {
222 2 ▶ my $self = shift;
Subroutine get_baggage_item: called CC=1 SCAR=0.0
Pod undocumented
223 2 my $key = shift;
224
225 2 return $self->get_context()->get_baggage_item( $key )
226 }
227  
228  
229  
230 sub get_baggage_items {
231 1 ▶ my $self = shift;
Subroutine get_baggage_items: called CC=1 SCAR=0.0
Pod undocumented
232
233 1 return $self->get_context()->get_baggage_items
234 }
235  
236  
237  
238 sub duration {
239 4 ▶ my $self = shift;
Subroutine duration: called CC=5 SCAR=16.3
Pod undocumented
240
241 my $start_time = $self->{ start_time }
242 4 ▶ or croak
Branch: 1 branch
BranchTrueFalseTotal
my $start_time = $self->{"start_time"} or croak(q(Span has not been started: [') . ($self->get_operation_name || "'undef'") . q('] ... how did you do that ?)) 1 3 4
Condition50%1/2
$self->get_operation_name || q['undef']
l!l
01
MC/DC0%0/1
$self->get_operation_name || q['undef']
$self->get_operation_name
Truth table50%1/2
A
A= $self->get_operation_name
Aresult
11
00
243 "Span has not been started: ['"
244 .
245 ( $self->get_operation_name || "'undef'" )
246 .
247 "'] ... how did you do that ?";
248 my $finish_time = $self->{ finish_time }
249 3 ▶ or croak
Branch: 1 branch
BranchTrueFalseTotal
my $finish_time = $self->{"finish_time"} or croak(q(Span has not been finished: [') . ($self->get_operation_name || "'undef'") . q('] ... yet!)) 1 2 3
Condition50%1/2
$self->get_operation_name || q['undef']
l!l
10
MC/DC0%0/1
$self->get_operation_name || q['undef']
$self->get_operation_name
Truth table50%1/2
A
A= $self->get_operation_name
Aresult
11
00
250 "Span has not been finished: ['"
251 .
252 ( $self->get_operation_name || "'undef'" )
253 .
254 "'] ... yet!";
255
256 2 return $finish_time - $start_time
257 }
258  
259  
260  
261 15 has child_of => (
262 is => 'ro',
263 isa =>Object, # does Span or does SpanContext
264 required => 1,
265 );
266  
267  
268  
269 sub parent_span_id {
270 0 ▶ my $self = shift;
Subroutine parent_span_id: not called CC=2 SCAR=17.9
Pod undocumented
271
272 0 my $parent = $self->{ child_of };
273 0 ▶ return unless is_Span( $parent );
Branch: 1 branch
BranchTrueFalseTotal
unless is_Span($parent) 0 0 0
274
275 0 return $parent->span_id
276 }
277 #
278 # This may not be the right way to implement it, for the `child_of` attribute
279 # may not be such a good idea, maybe it should use references, but not sure how
280 # those are used
281  
282  
283  
284 # _set_context
285 #
286 # you really shouldn't change the context yourself, only on instantiation
287 #
288 sub _set_context {
289 2 ▶ my $self = shift;
Subroutine _set_context: called CC=3 SCAR=11.3
290
291 2 ▶ croak "Can't set context on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 0 2 2
292 if $self->has_finished;
293
294 2 ▶ my $context = shift or die "Missing context";
Branch: 1 branch
BranchTrueFalseTotal
my $context = shift() or die "Missing context" 0 2 2
295
296 2 $self->{ context } = $context;
297
298 2 return $self
299 }
300  
301  
302  
303 15 has on_finish => (
304 is => 'ro',
305 isa => Maybe[CodeRef],
306 predicate => 1,
307 );
308  
309  
310  
311 sub DEMOLISH {
312 30 ▶ my $self = shift;
Subroutine DEMOLISH: called CC=3 SCAR=11.0
Pod undocumented
313 30 my $in_global_destruction = shift;
314
315 30 ▶ return if $self->has_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 6 24 30
316
317 # carp "Span not programmatically finished before being demolished";
318
319 24 ▶ $self->finish( )
Branch: 1 branch
BranchTrueFalseTotal
unless $in_global_destruction 24 0 24
320 unless $in_global_destruction;
321
322 return
323 24 }
324  
325  
326  
327 =head2 epoch_floatingpoint
328  
329 Well, returns the time since 'epoch' with fractional seconds, as floating-point.
330  
331 =cut
332  
333 sub epoch_floatingpoint {
334 45 ▶ return time()
Subroutine epoch_floatingpoint: called CC=1 SCAR=0.0
Pod documented
335 }
336 #
337 # well, this is a bit off a silly idea:
338 # some implentations may want nano-second accuracy, but floating point
339 # computations using 64bits (IEEE) are only having 16 digits in the mantissa.
340 # The number of nano-seconds since epoch is 19 digits that barely fits in a
341 # signed 64 bit integer.
342  
343  
344  
345 BEGIN {
346 # use Role::Tiny::With;
347 with 'OpenTracing::Interface::Span'
348 15 ▶ if $ENV{OPENTRACING_INTERFACE};
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"OPENTRACING_INTERFACE"} 2 13 15
349 }
350  
351  
352  
353 15 1;
« Role/Scope.pm ↑ Summary Role/ScopeManager.pm »