Role/Span.pm

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