Role/Span.pm

filter: Statementstmt 98.1% 103 / 105 Branchbran 82.3% 28 / 34 Conditioncond 58.3% 7 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 96.4% 27 / 28 Podpod 92.8% 13 / 14 totaltotal 89.4% 178 / 199 CC 22 SCAR 9.6
CC
22
Cov
91%
finish
5.3
duration
5.1
get_parent_span_id
4.1
CRAP 2.6
« Role/Tracer.pm ↑ Summary Role/ScopeManager.pm »
1 package OpenTracing::Role::Span;
2  
3 16 our $VERSION = 'v0.86.1';
4  
5  
6  
7 16 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use MooX::ProtectedAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use MooX::Should;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use OpenTracing::Types qw/:types :is/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Types::Standard qw/CodeRef HashRef Maybe Num Object Str Value/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Types::Common::Numeric qw/PositiveOrZeroNum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 16 has operation_name => (
19 is => 'rwp',
20 should => Str,
21 required => 1,
22 # writer => 'overwrite_operation_name',
23 reader => 'get_operation_name', # it's not in the Interface
24 );
25  
26 has start_time => (
27 is => 'ro',
28 should => PositiveOrZeroNum,
29 21 ▶ default => sub { epoch_floatingpoint() }
Subroutine __ANON__: called CC=1 SCAR=0.0
30 16 );
31  
32 16 has finish_time => (
33 is => 'rwp',
34 should => PositiveOrZeroNum,
35 predicate => 'has_finished',
36 init_arg => undef,
37 );
38  
39 has tags => (
40 is => 'rwp',
41 should => HashRef[Value],
42 handles_via => 'Hash',
43 handles => {
44 get_tags => 'all',
45 },
46 18 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 16 );
48  
49 16 has context => (
50 is => 'ro',
51 should => SpanContext,
52 reader => 'get_context',
53 # writer => '_set_context',
54 required => 1, # either from Span->get_context or SpanContext self
55 handles => {
56 get_span_id => 'span_id',
57 },
58 );
59  
60 2 ▶sub overwrite_operation_name {
Subroutine overwrite_operation_name: called CC=2 SCAR=6.9
Pod documented
61 2 my $self = shift;
62
63 2 ▶ croak "Can't overwrite an operation-name on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 1 2
64 if $self->has_finished;
65
66 1 my $operation_name = shift; # or throw an exception
67
68 1 $self->_set_operation_name( $operation_name );
69
70 1 return $self
71 }
72  
73 35 ▶sub finish {
Subroutine finish: called CC=5 SCAR=16.6
Pod documented
74 35 my $self = shift;
75
76 35 ▶ carp "Span has already been finished" and return $self
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 34 35
Condition60%3/5
$self->has_finished && (carp('Span has already been finished') && (return $self))
!ll&&!rl&&r
3410
carp("Span has already been finished") and return $self
!ll
01
MC/DC0%0/2
$self->has_finished && (carp('Span has already been finished') && (return $self))
$self->has_finished carp("Span has already been finished")
Truth table33%1/3
A && B
A= $self->has_finishedB= carp("Span has already been finished")
ABresult
0X0
100
111
77 if $self->has_finished;
78
79 34 ▶ my $epoch_timestamp = shift // epoch_floatingpoint();
Condition66%2/3
shift() // epoch_floatingpoint()
l!l&&r!l&&!r
2320
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
80
81 34 $self->_set_finish_time( $epoch_timestamp );
82
83 34 ▶ $self->on_finish->( $self )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_on_finish 0 34 34
84 if $self->has_on_finish;
85
86 34 return $self
87 }
88  
89 3 ▶sub add_tag {
Subroutine add_tag: called CC=2 SCAR=6.9
Pod documented
90 3 my $self = shift;
91
92 3 ▶ croak "Can't set a tag on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 2 3
93 if $self->has_finished;
94
95 2 my $key = shift;
96 2 my $value = shift;
97
98 2 $self->add_tags( $key => $value );
99
100 2 return $self
101 }
102  
103 3 ▶sub add_tags {
Subroutine add_tags: called CC=2 SCAR=7.0
Pod documented
104 3 my $self = shift;
105
106 3 ▶ croak "Can't set a tag on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 0 3 3
107 if $self->has_finished;
108
109 3 my %tags = @_;
110 3 $self->_set_tags(
111 { $self->get_tags, %tags }
112 );
113
114 3 return $self
115 }
116  
117 2 ▶sub log_data {
Subroutine log_data: called CC=2 SCAR=6.9
Pod documented
118 2 my $self = shift;
119
120 2 ▶ croak "Can't log any more data on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 1 2
121 if $self->has_finished;
122
123 1 my %log_data = @_;
124
125 # ... # shall we just use Log::Any ?
126
127 1 return $self
128 }
129  
130 2 ▶sub add_baggage_item {
Subroutine add_baggage_item: called CC=2 SCAR=6.9
Pod documented
131 2 my $self = shift;
132
133 2 ▶ croak "Can't set baggage-items on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 1 2
134 if $self->has_finished;
135
136 1 my $key = shift;
137 1 my $value = shift;
138
139 1 $self->add_baggage_items( $key => $value );
140
141 1 return $self
142 }
143  
144 3 ▶sub add_baggage_items {
Subroutine add_baggage_items: called CC=2 SCAR=6.9
Pod documented
145 3 my $self = shift;
146
147 3 ▶ croak "Can't set baggage-items on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 1 2 3
148 if $self->has_finished;
149
150 2 my %items = @_;
151
152 2 my $new_context = $self->get_context()->with_baggage_items( %items );
153 2 $self->_set_context( $new_context );
154
155 2 return $self
156 }
157  
158 2 ▶sub get_baggage_item {
Subroutine get_baggage_item: called CC=1 SCAR=0.0
Pod documented
159 2 my $self = shift;
160 2 my $key = shift;
161
162 2 return $self->get_context()->get_baggage_item( $key )
163 }
164  
165 1 ▶sub get_baggage_items {
Subroutine get_baggage_items: called CC=1 SCAR=0.0
Pod documented
166 1 my $self = shift;
167
168 1 return $self->get_context()->get_baggage_items
169 }
170  
171 4 ▶sub duration {
Subroutine duration: called CC=5 SCAR=16.3
Pod documented
172 4 my $self = shift;
173
174 my $start_time = $self->{ start_time }
175 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 ?)) 3 1 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
176 "Span has not been started: ['"
177 .
178 ( $self->get_operation_name || "'undef'" )
179 .
180 "'] ... how did you do that ?";
181 my $finish_time = $self->{ finish_time }
182 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!)) 2 1 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
183 "Span has not been finished: ['"
184 .
185 ( $self->get_operation_name || "'undef'" )
186 .
187 "'] ... yet!";
188
189 2 return $finish_time - $start_time
190 }
191  
192 16 protected_has child_of => (
193 is => 'ro',
194 should => Span | SpanContext,
195 required => 0,
196 );
197 #
198 # this is just non of your business, and will get depricated as soon as there is
199 # references
200  
201 3 ▶sub _get_child_of { $_[0]->child_of }
Subroutine _get_child_of: called CC=1 SCAR=0.0
202 #
203 # so this can be swapped for something more clever once using references
204  
205 3 ▶sub get_parent_span_id {
Subroutine get_parent_span_id: called CC=4 SCAR=14.0
Pod documented
206 3 my $self = shift;
207
208 3 my $child_of = $self->_get_child_of;
209
210 3 ▶ return unless defined $child_of;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_of 1 2 3
211
212 2 ▶ return $child_of->span_id
Branch: 1 branch
BranchTrueFalseTotal
if is_SpanContext($child_of) 1 1 2
213 if is_SpanContext($child_of);
214
215 1 ▶ return $child_of->get_context->span_id
Branch: 1 branch
BranchTrueFalseTotal
if is_Span($child_of) 1 0 1
216 if is_Span($child_of);
217
218 0 croak "No 'parent span_id' for 'child_of' attribute [$child_of]"
219 #
220 # execution should never end up here
221
222 }
223 #
224 # This may not be the right way to implement it, for the `child_of` attribute
225 # may not be such a good idea, maybe it should use references, but not sure how
226 # those are used
227  
228  
229  
230 0 ▶sub is_root_span { ! shift->get_parent_span_id() }
Subroutine is_root_span: not called CC=1 SCAR=6.9
Pod documented
231  
232 # _set_context
233 #
234 # you really shouldn't change the context yourself, only on instantiation
235 #
236 2 ▶sub _set_context {
Subroutine _set_context: called CC=3 SCAR=11.3
237 2 my $self = shift;
238
239 2 ▶ croak "Can't set context on an already finished span"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 0 2 2
240 if $self->has_finished;
241
242 2 ▶ my $context = shift or die "Missing context";
Branch: 1 branch
BranchTrueFalseTotal
my $context = shift() or die "Missing context" 2 0 2
243
244 2 $self->{ context } = $context;
245
246 2 return $self
247 }
248  
249 16 has on_finish => (
250 is => 'ro',
251 should => Maybe[CodeRef],
252 predicate => 1,
253 );
254  
255 34 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=11.0
Pod undocumented
256 34 my $self = shift;
257 34 my $in_global_destruction = shift;
258
259 34 ▶ return if $self->has_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_finished 6 28 34
260
261 # carp "Span not programmatically finished before being demolished";
262
263 28 ▶ $self->finish( )
Branch: 1 branch
BranchTrueFalseTotal
unless $in_global_destruction 28 0 28
264 unless $in_global_destruction;
265
266 return
267 28 }
268  
269 53 ▶sub epoch_floatingpoint {
Subroutine epoch_floatingpoint: called CC=1 SCAR=0.0
Pod documented
270 53 return time()
271 }
272 #
273 # well, this is a bit off a silly idea:
274 # some implentations may want nano-second accuracy, but floating point
275 # computations using 64bits (IEEE) are only having 16 digits in the mantissa.
276 # The number of nano-seconds since epoch is 19 digits that barely fits in a
277 # signed 64 bit integer.
278  
279  
280  
281 16 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
282 # use Role::Tiny::With;
283 16 with 'OpenTracing::Interface::Span'
284 }
285  
286  
287  
288 16 1;
« Role/Tracer.pm ↑ Summary Role/ScopeManager.pm »