OpenTracing/Tracer.pm

filter: Statementstmt 53.5% 75 / 140 Branchbran 22.2% 8 / 36 Conditioncond 29.6% 8 / 27 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 55.8% 19 / 34 Podpod 35.0% 7 / 20 totaltotal 43.5% 118 / 271 CC 33 SCAR 19.1
CC
33
Cov
45%
span
14.5
process
12.2
remove_span_completion_callback
12.0
CRAP 6.7
« OpenTracing/Integration/HTTP/Tiny.pm ↑ Summary OpenTracing/Batch.pm »
1 package OpenTracing::Tracer;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '1.006'; # VERSION
7 9 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 9 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 OpenTracing::Tracer - application tracing
17  
18 =head1 DESCRIPTION
19  
20 This provides the interface between the OpenTracing API and the tracing service(s)
21 for an application.
22  
23 Typically a single process would have one tracer instance.
24  
25 =cut
26  
27 9 ▶use OpenTracing::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶use OpenTracing::Span;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use OpenTracing::SpanProxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use OpenTracing::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 9 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 9 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =head1 METHODS
39  
40 =cut
41  
42 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
43 8 my ($class, %args) = @_;
44 8 ▶ $args{span_completion_callbacks} ||= [];
Condition50%1/2
$args{"span_completion_callbacks"} ||= []
l!l
08
MC/DC0%0/1
$args{"span_completion_callbacks"} ||= []
$args{"span_completion_callbacks"}
Truth table50%1/2
A
A= $args{"span_completion_callbacks"}
Aresult
11
00
45 8 ▶ $args{current_span} ||= [];
Condition50%1/2
$args{"current_span"} ||= []
l!l
08
MC/DC0%0/1
$args{"current_span"} ||= []
$args{"current_span"}
Truth table50%1/2
A
A= $args{"current_span"}
Aresult
11
00
46 8 bless \%args, $class
47 }
48  
49 =head2 process
50  
51 Returns the current L<OpenTracing::Process>.
52  
53 =cut
54  
55 1 ▶sub process {
Subroutine process: called CC=6 SCAR=25.0
Pod documented
56 1 my ($self) = @_;
57  
58 # Handle forks
59 1 ▶ if($self->{process} and $self->{process}->pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"process"} and $self->{"process"}->pid != $$) 0 1 1
Condition33%1/3
$self->{"process"} and $self->{"process"}->pid != $$
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"process"} and $self->{"process"}->pid != $$
$self->{"process"} $self->{"process"}->pid != $$
Truth table33%1/3
A and B
A= $self->{"process"}B= $self->{"process"}->pid != $$
ABresult
0X0
100
111
60 0 delete $self->{process};
61 }
62  
63 1 ▶ $self->{process} //= do {
Condition33%1/3
$self->{"process"} //= do { require Net::Address::IP::Local; "OpenTracing::Process"->new("tags", {"pid", $$, "ip", "Net::Address::IP::Local"->public_ipv4, "tracer.version", 'perl-OpenTracing-' . (__PACKAGE__->VERSION // ("OpenTracing"->VERSION // "unknown") . '-dev')}) }
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"process"} //= do { require Net::Address::IP::Local; "OpenTracing::Process"->new("tags", {"pid", $$, "ip", "Net::Address::IP::Local"->public_ipv4, "tracer.version", 'perl-OpenTracing-' . (__PACKAGE__->VERSION // ("OpenTracing"->VERSION // "unknown") . '-dev')}) }
$self->{"process"} do { require Net::Address::IP::Local; "OpenTracing::Process"->new("tags", {"pid", $$, "ip", "Net::Address::IP::Local"->public_ipv4, "tracer.version", 'perl-OpenTracing-' . (__PACKAGE__->VERSION // ("OpenTracing"->VERSION // "unknown") . '-dev')}) }
Truth table33%1/3
A //= B
A= $self->{"process"}B= do { require Net::Address::IP::Local; "OpenTracing::Process"->new("tags", {"pid", $$, "ip", "Net::Address::IP::Local"->public_ipv4, "tracer.version", 'perl-OpenTracing-' . (__PACKAGE__->VERSION // ("OpenTracing"->VERSION // "unknown") . '-dev')}) }
ABresult
1X1
011
000
64 1 require Net::Address::IP::Local;
65 1 ▶ OpenTracing::Process->new(
Condition0%0/4
__PACKAGE__->VERSION // ("OpenTracing"->VERSION // "unknown") . '-dev'
l!l
00
"OpenTracing"->VERSION // "unknown"
l!l
00
MC/DC0%0/1
__PACKAGE__->VERSION // ("OpenTracing"->VERSION // "unknown") . '-dev'
__PACKAGE__->VERSION
Truth table0%0/2
A
A= __PACKAGE__->VERSION
Aresult
11
00
66 tags => {
67 pid => $$,
68 ip => Net::Address::IP::Local->public_ipv4,
69 # When running from the repository, we won't have a ->VERSION, so
70 # we'll default to the main package but indicate with -dev that
71 # we may have differences from the "official" version
72 'tracer.version' => 'perl-OpenTracing-' . (__PACKAGE__->VERSION // ((OpenTracing->VERSION // "unknown") . "-dev")),
73 }
74 );
75 }
76 }
77  
78 =head2 is_enabled
79  
80 Returns true if this tracer is currently enabled.
81  
82 =cut
83  
84 2 ▶sub is_enabled { shift->{is_enabled} //= 0 }
Subroutine is_enabled: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"is_enabled"} //= 0
l!l
11
MC/DC100%1/1
+(shift())->{"is_enabled"} //= 0
+(shift())->{"is_enabled"}
Truth table100%2/2
A
A= +(shift())->{"is_enabled"}
Aresult
11
00
85  
86 =head2 enable
87  
88 Enable the current tracer.
89  
90 =cut
91  
92 0 ▶sub enable { shift->{is_enabled} = 1 }
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
93  
94 =head2 disable
95  
96 Disable the current tracer.
97  
98 =cut
99  
100 0 ▶sub disable { shift->{is_enabled} = 0 }
Subroutine disable: not called CC=1 SCAR=6.9
Pod documented
101  
102 =head2 spans
103  
104 Returns an arrayref of L<OpenTracing::Span> instances.
105  
106 =cut
107  
108 0 ▶sub spans {
Subroutine spans: not called CC=1 SCAR=6.9
Pod documented
109 shift->{spans}
110 0 }
111  
112 =head2 span_list
113  
114 Returns a list of L<OpenTracing::Span> instances.
115  
116 =cut
117  
118 0 ▶sub span_list {
Subroutine span_list: not called CC=2 SCAR=17.9
Pod documented
119 0 ▶ (shift->{spans} //= [])->@*
Condition0%0/2
+(shift())->{"spans"} //= []
l!l
00
MC/DC0%0/1
+(shift())->{"spans"} //= []
+(shift())->{"spans"}
Truth table0%0/2
A
A= +(shift())->{"spans"}
Aresult
11
00
120 }
121  
122 =head2 add_span
123  
124 Adds a new L<OpenTracing::Span> instance to the pending list, if
125 we're currently enabled.
126  
127 =cut
128  
129 1 ▶sub add_span {
Subroutine add_span: called CC=2 SCAR=10.1
Pod documented
130 1 my ($self, $span) = @_;
131 1 ▶ return $span unless $self->is_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_enabled 1 0 1
132 0 push $self->{spans}->@*, $span;
133 0 Scalar::Util::weaken($span->{batch});
134 0 $span
135 }
136  
137 1 ▶sub span {
Subroutine span: called CC=8 SCAR=26.7
Pod undocumented
138 1 my ($self, %args) = @_;
139 1 ▶ $args{operation_name} //= (caller 1)[3];
Condition33%1/3
$args{"operation_name"} //= (caller 1)[3]
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"operation_name"} //= (caller 1)[3]
$args{"operation_name"} (caller 1)[3]
Truth table33%1/3
A //= B
A= $args{"operation_name"}B= (caller 1)[3]
ABresult
1X1
011
000
140  
141 # We want to figure out what parent to
142 # use, following as precedence order:
143 # - Parent args
144 # - First CHILD_OF reference
145 # - First FOLLOW_FROM reference
146 # - Current trace span
147 1 my $parent = $args{parent};
148 1 ▶ unless ($parent)
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 1 0 1
149 {
150 1 my @reference_queue = ();
151  
152 # Default to current span if any
153 1 ▶ if (my $current_span = $self->{current_span}->[-1])
Branch: 1 branch
BranchTrueFalseTotal
if (my $current_span = $self->{"current_span"}[-1]) 0 1 1
154 {
155 0 push @reference_queue, {
156 id => $current_span->id,
157 trace_id => $current_span->trace_id
158 };
159 }
160  
161 1 ▶ if(my $references = $args{references}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $references = $args{"references"}) 0 1 1
162 0 foreach my $reference (@$references) {
163 0 ▶ next unless $reference; # skip empty references if any
Branch: 1 branch
BranchTrueFalseTotal
unless $reference 0 0 0
164  
165 0 push @reference_queue, {
166 id => $reference->context->id,
167 trace_id => $reference->context->trace_id
168 };
169  
170 # Stop the loop if CHILD_OF is found
171 0 ▶ last if $reference->ref_type == OpenTracing::Reference::CHILD_OF;
Branch: 1 branch
BranchTrueFalseTotal
if $reference->ref_type == 0 0 0 0
172  
173 # Otherwise loop over FOLLOWS_FROM just in case we find a CHILD_OF later
174 }
175 }
176  
177 # Take the first found reference (either CHILD_OF or FOLLOWS_FROM) or PARENT)
178 1 $parent = shift @reference_queue;
179 }
180  
181 $self->add_span(
182 1 my $span = OpenTracing::Span->new(
183 tracer => $self,
184 parent => $parent,
185 %args
186 )
187 );
188 1 push @{ $self->{current_span} }, $span;
189 1 return OpenTracing::SpanProxy->new(span => $span);
190 }
191  
192 0 ▶sub current_span { shift->{current_span}->[-1] }
Subroutine current_span: not called CC=1 SCAR=6.9
Pod undocumented
193  
194 1 ▶sub finish_span {
Subroutine finish_span: called CC=4 SCAR=14.6
Pod undocumented
195 1 my ($self, $span) = @_;
196 1 $log->tracef('Finishing span %s', $span);
197  
198 1 @{ $self->{current_span} } = grep { refaddr($_) != refaddr($span)} @{ $self->{current_span} };
199  
200 1 ▶ push @{$self->{finished_spans} //= []}, $span;
Condition50%1/2
$self->{"finished_spans"} //= []
l!l
01
MC/DC0%0/1
$self->{"finished_spans"} //= []
$self->{"finished_spans"}
Truth table50%1/2
A
A= $self->{"finished_spans"}
Aresult
11
00
201 1 ▶ return $span unless $self->is_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_enabled 1 0 1
202  
203 0 $_->($span) for $self->span_completion_callbacks;
204 0 return $span;
205 }
206  
207 0 ▶sub add_span_completion_callback {
Subroutine add_span_completion_callback: not called CC=1 SCAR=6.9
Pod undocumented
208 0 my ($self, $code) = @_;
209 0 push $self->{span_completion_callbacks}->@*, $code;
210 0 return $self;
211 }
212  
213 0 ▶sub remove_span_completion_callback {
Subroutine remove_span_completion_callback: not called CC=3 SCAR=24.8
Pod undocumented
214 0 my ($self, $code) = @_;
215 0 my $addr = Scalar::Util::refaddr($code);
216 0 my $data = $self->{span_completion_callbacks};
217 # Essentially extract_by from List::UtilsBy
218 0 for(my $idx = 0; ; ++$idx) {
219 0 ▶ last if $idx > $#$data;
Branch: 1 branch
BranchTrueFalseTotal
if $idx > $data->$#* 0 0 0
220 0 ▶ next unless Scalar::Util::refaddr($data->[$idx]) == $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($data->[$idx]) == $addr 0 0 0
221 0 splice @$data, $idx, 1, ();
222 # Skip the $idx change
223 0 redo;
224 }
225 0 return $self;
226 }
227  
228 0 ▶sub span_completion_callbacks {
Subroutine span_completion_callbacks: not called CC=1 SCAR=6.9
Pod undocumented
229 shift->{span_completion_callbacks}->@*
230 0 }
231  
232 0 ▶sub inject {
Subroutine inject: not called CC=3 SCAR=24.8
Pod undocumented
233 0 my ($self, $span, %args) = @_;
234 0 ▶ $args{format} //= 'text_map';
Condition0%0/2
$args{"format"} //= "text_map"
l!l
00
MC/DC0%0/1
$args{"format"} //= "text_map"
$args{"format"}
Truth table0%0/2
A
A= $args{"format"}
Aresult
11
00
235 0 ▶ if($args{format} eq 'text_map') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'format'} eq 'text_map') { } 0 0 0
236 return {
237 0 map {; $_ => $span->$_ } qw(id trace_id parent_id operation_name start_time finish_time),
238 }
239 } else {
240 die 'unknown format ' . $args{format}
241 0 }
242 }
243  
244 0 ▶sub span_for_future {
Subroutine span_for_future: not called CC=1 SCAR=6.9
Pod undocumented
245 0 my ($self, $f, %args) = @_;
246 0 my $span = $self->span(
247 operation_name => $f->label,
248 %args,
249 );
250 0 ▶ $f->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 $span->tag(
252 'future.state' => $f->state
253 );
254 0 $span->finish;
255 0 undef $f;
256 0 undef $span
257 0 });
258 0 return $span;
259 }
260  
261 0 ▶sub extract {
Subroutine extract: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my ($self, $data, %args) = @_;
263 0 ▶ $args{format} //= 'text_map';
Condition0%0/2
$args{"format"} //= "text_map"
l!l
00
MC/DC0%0/1
$args{"format"} //= "text_map"
$args{"format"}
Truth table0%0/2
A
A= $args{"format"}
Aresult
11
00
264 0 ▶ if($args{format} eq 'text_map') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'format'} eq 'text_map') { } 0 0 0
265 0 @$data{tracer} = $self;
266 0 return OpenTracing::Span->new(%$data);
267 } else {
268 die 'unknown format ' . $args{format}
269 0 }
270 }
271  
272 0 ▶sub extract_finished_spans {
Subroutine extract_finished_spans: not called CC=3 SCAR=24.8
Pod undocumented
273 0 my ($self, $count) = @_;
274 0 ▶ if(!defined($count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $count) { } 0 0 0
elsif (not $count) { } 0 0 0
275 0 $count = 10;
276 } elsif(!$count) {
277 0 $count = @{$self->{finished_spans}};
278 }
279 0 return splice @{$self->{finished_spans}}, 0, min(0 + @{$self->{finished_spans}}, $count);
280 }
281  
282 0 ▶sub child_of {
Subroutine child_of: not called CC=2 SCAR=17.9
Pod undocumented
283 0 my ($self, $context) = @_;
284 0 ▶ die "Can't create a child_of reference without a valid context" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 0 0
285 0 return OpenTracing::Reference->new(
286 ref_type => OpenTracing::Reference::CHILD_OF,
287 context => $context);
288 }
289  
290 0 ▶sub follows_from {
Subroutine follows_from: not called CC=2 SCAR=17.9
Pod undocumented
291 0 my ($self, $context) = @_;
292 0 ▶ die "Can't create a follow_from reference without a valid context" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 0 0
293 0 return OpenTracing::Reference->new(
294 ref_type => OpenTracing::Reference::FOLLOWS_FROM,
295 context => $context);
296 }
297  
298 =head2 DESTROY
299  
300 Triggers callbacks when the batch is discarded. Normally used by the transport
301 mechanism to ensure that the batch is sent over to the tracing endpoint.
302  
303 =cut
304  
305 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=13.3
306 1 my ($self) = @_;
307 1 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 1 1
308 my $on_destroy = delete $self->{on_destroy}
309 1 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $on_destroy = delete $self->{"on_destroy"} or return 0 1 1
310 0 $self->$on_destroy;
311 0 return;
312 }
313  
314 9 1;
315  
316 __END__
« OpenTracing/Integration/HTTP/Tiny.pm ↑ Summary OpenTracing/Batch.pm »