blib/lib/Net/Async/OpenTracing.pm

filter: Statementstmt 46.9% 53 / 113 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 40.0% 16 / 40 Podpod 87.5% 14 / 16 totaltotal 39.9% 83 / 208 SCAR 46.1
CC
18
Cov
37%
configure
42.0
send_all_pending
12.0
send_next_batch
12.0
CRAP 100.8
↑ Summary
1 package Net::Async::OpenTracing;
2 # ABSTRACT: wire protocol support for the opentracing.io API
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.000';
8 1 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
9  
10 1 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Net::Async::OpenTracing - OpenTracing APM via L<IO::Async>
20  
21 =head1 SYNOPSIS
22  
23 use Net::Async::OpenTracing;
24 use IO::Async::Loop;
25 use OpenTracing::Any qw($tracer);
26 my $loop = IO::Async::Loop->new;
27 $loop->add(
28 my $tracing = Net::Async::OpenTracing->new(
29 host => '127.0.0.1',
30 port => 6832,
31 )
32 );
33 $tracer->span(operation_name => 'example');
34 # Manual sync - generally only needed on exit
35 $tracing->sync->get;
36  
37 =head1 DESCRIPTION
38  
39 This all relies on the abstract L<OpenTracing> interface, so that'd be
40 the first port of call for official documentation.
41  
42 =head2 Setting up and testing
43  
44 If you want to experiment with this, start by setting up a Jæger instance in Docker like so:
45  
46 docker run -d --name jaeger \
47 -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
48 -p 5775:5775/udp \
49 -p 6831:6831/udp \
50 -p 6832:6832/udp \
51 -p 5778:5778 \
52 -p 16686:16686 \
53 -p 14268:14268 \
54 -p 9411:9411 \
55 jaegertracing/all-in-one:1.17
56  
57 If you have a Kubernetes stack installed then you likely already have this available.
58  
59 UDP port 6832 is typically the "binary Thrift" port, so that's likely where you would
60 want this module configured to send data (other ports and protocols are available).
61  
62 Set up an L<Net::Async::OpenTracing> instance with those connection details:
63  
64 use Net::Async::OpenTracing;
65 my $loop = IO::Async::Loop->new;
66 $loop->add(
67 my $tracing = Net::Async::OpenTracing->new(
68 host => '127.0.0.1',
69 port => 6832,
70 )
71 );
72 # Now generate some traffic
73 {
74 my $span = $tracer->span(
75 operation_name => 'example_span'
76 );
77 $span->log('test message ' . $_ . ' from the parent') for 1..3;
78 my $child = $span->span(operation_name => 'child_span');
79 $child->log('message ' . $_ . ' from the child span') for 1..3;
80 }
81 # Make sure all trace data is sent
82 $tracing->sync->get;
83  
84 You should then see a trace with 2 spans show up.
85  
86 =cut
87  
88 1 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use IO::Async::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 1 ▶use OpenTracing;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use OpenTracing::Batch;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 ▶use OpenTracing::Protocol::Jaeger;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use OpenTracing::Any qw($tracer);
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 =head2 configure
103  
104 Takes the following named parameters:
105  
106 =over 4
107  
108 =item * C<host> - where to send traces
109  
110 =item * C<port> - the UDP/TCP port to connect to
111  
112 =item * C<protocol> - how to communicate: thrift, http/thrift, etc.
113  
114 =item * C<items_per_batch> - number of spans to try sending each time
115  
116 =item * C<batches_per_loop> - number of batches to try sending for each loop iteration
117  
118 =item * C<tracer> - the L<OpenTracing::Tracer> instance to use, defaults to the one
119 provided by L<OpenTracing::Any>
120  
121 =back
122  
123 =cut
124  
125 sub configure {
126 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=6 SCAR=37.4
Pod documented
127 0 ▶ $self->{pending} //= [];
Condition0%0/2
$self->{"pending"} //= []
l!l
00
MC/DC0%0/1
$self->{"pending"} //= []
$self->{"pending"}
Truth table0%0/2
A
A= $self->{"pending"}
Aresult
11
00
128  
129 # Only support Jæger for now
130 0 ▶ die 'invalid protocol' if $args{protocol} and $args{protocol} ne 'jaeger';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"protocol"} and $args{"protocol"} ne "jaeger" 0 0 0
Condition0%0/3
$args{"protocol"} and $args{"protocol"} ne "jaeger"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"protocol"} and $args{"protocol"} ne "jaeger"
$args{"protocol"} $args{"protocol"} ne "jaeger"
Truth table0%0/3
A and B
A= $args{"protocol"}B= $args{"protocol"} ne "jaeger"
ABresult
0X0
100
111
131  
132 0 for my $k (qw(host port protocol items_per_batch batches_per_loop tracer)) {
133 0 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 0 0
134 }
135 0 return $self->next::method(%args);
136 }
137  
138 =head2 host
139  
140 The hostname or IP to send spans to.
141  
142 =cut
143  
144 0 ▶sub host { shift->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
145  
146 =head2 port
147  
148 The port to send spans to.
149  
150 =cut
151  
152 0 ▶sub port { shift->{port} }
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
153  
154 =head2 tracer
155  
156 The L<OpenTracing::Tracer> instance, defaults to the standard L<OpenTracing::Any>-provided one.
157  
158 =cut
159  
160 0 ▶sub tracer { shift->{tracer} || $tracer }
Subroutine tracer: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"tracer"} or $Net::Async::OpenTracing::tracer 0 0 0
161  
162 sub _add_to_loop {
163 0 ▶ my ($self) = @_;
Subroutine _add_to_loop: not called CC=1 SCAR=6.9
164  
165 # Nothing to send yet, so mark this as done immediately
166 0 $self->{send_in_progress} = Future->done;
167  
168 $self->add_child(
169 $self->{udp} = my $client = IO::Async::Socket->new(
170 on_recv => sub {
171 0 ▶ my ($sock, $payload, $addr) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 try {
173 0 $log->warnf("Receiving [%s] from %s - unexpected, this should be one-way opentracing traffic from us?", $payload, $addr);
174 0 } catch {
175 0 $log->errorf("Exception when receiving: %s", $@);
176 }
177 },
178 on_outgoing_empty => $self->$curry::weak(sub {
179 shift->{send_in_progress}->done
180 0 ▶ }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 )
182 );
183 $self->tracer->add_span_completion_callback(
184 $self->{span_completion} = $self->$curry::weak(sub {
185 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 $self->loop->later(sub {
187 0 ▶ $self->start_sending;
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 });
189 })
190 0 );
191 0 $self->tracer->enable;
192 $self->{connected} = $client->connect(
193 host => $self->host,
194 service => $self->port,
195 socktype => 'dgram',
196 )->on_done(sub {
197 0 ▶ $log->tracef('Connected to UDP endpoint');
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 $self->start_sending;
199 0 })->retain;
200 }
201  
202 =head1 METHODS - Internal
203  
204 =head2 send
205  
206 Performs the send and sets up the L<Future> for marking completion.
207  
208 =cut
209  
210 sub send {
211 0 ▶ my ($self, $bytes) = @_;
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
212 $self->{send_in_progress} = $self->loop->new_future
213 0 ▶ if $self->{send_in_progress}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"send_in_progress"}->is_ready 0 0 0
214 0 $self->udp->send($bytes);
215 0 return $self->send_in_progress;
216 }
217  
218 =head2 send_in_progress
219  
220 Returns a L<Future> indicating whether a send is in progress or not (will
221 be marked as L<Future/done> if the send is complete).
222  
223 =cut
224  
225 0 ▶sub send_in_progress { shift->{send_in_progress}->without_cancel }
Subroutine send_in_progress: not called CC=1 SCAR=6.9
Pod documented
226  
227 =head2 is_sending
228  
229 Returns true if we are currently sending data.
230  
231 =cut
232  
233 0 ▶sub is_sending { my $f = shift->{is_sending}; $f && !$f->is_ready }
Subroutine is_sending: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$f and not $f->is_ready 0 0 0
234  
235 =head2 start_sending
236  
237 Trigger the send process, which will cause all pending traces to be
238 sent to the remote endpoint.
239  
240 Does nothing if sending is already in progress.
241  
242 =cut
243  
244 sub start_sending {
245 0 ▶ my ($self) = @_;
Subroutine start_sending: not called CC=2 SCAR=17.9
Pod documented
246 0 ▶ return if $self->is_sending;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_sending 0 0 0
247 0 $self->{is_sending} = $self->send_all_pending;
248 }
249  
250 =head2 proto
251  
252 The L<OpenTracing::Protocol> instance.
253  
254 =cut
255  
256 0 ▶sub proto { shift->{proto} //= OpenTracing::Protocol::Jaeger->new }
Subroutine proto: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
+(shift())->{"proto"} //= "OpenTracing::Protocol::Jaeger"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"proto"} //= "OpenTracing::Protocol::Jaeger"->new
+(shift())->{"proto"} "OpenTracing::Protocol::Jaeger"->new
Truth table0%0/3
A //= B
A= +(shift())->{"proto"}B= "OpenTracing::Protocol::Jaeger"->new
ABresult
1X1
011
000
257  
258 =head2 sub
259  
260 Sends all pending batches.
261  
262 =cut
263  
264 0 ▶async sub send_all_pending {
Subroutine send_all_pending: not called CC=3 SCAR=24.8
Pod undocumented
265 0 my ($self) = @_;
266 0 my $count = 0;
267 0 ▶ while(await $self->send_next_batch) {
Branch: 1 branch
BranchTrueFalseTotal
if (XXX) 0 0 0
268 0 ▶ await $self->loop->delay_future(after => 0)
Branch: 1 branch
BranchTrueFalseTotal
unless ++$count % $self->batches_per_loop 0 0 0
269 unless ++$count % $self->batches_per_loop;
270 }
271 1 }
272  
273 0 ▶sub items_per_batch { shift->{items_per_batch} ||= 10 }
Subroutine items_per_batch: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"items_per_batch"} ||= 10
l!l
00
MC/DC0%0/1
+(shift())->{"items_per_batch"} ||= 10
+(shift())->{"items_per_batch"}
Truth table0%0/2
A
A= +(shift())->{"items_per_batch"}
Aresult
11
00
274 0 ▶sub batches_per_loop { shift->{batches_per_loop} ||= 64 }
Subroutine batches_per_loop: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"batches_per_loop"} ||= 64
l!l
00
MC/DC0%0/1
+(shift())->{"batches_per_loop"} ||= 64
+(shift())->{"batches_per_loop"}
Truth table0%0/2
A
A= +(shift())->{"batches_per_loop"}
Aresult
11
00
275  
276 =head2 sub
277  
278 Gathers and sends a single L<OpenTracing::Batch>.
279  
280 =cut
281  
282 0 ▶async sub send_next_batch {
Subroutine send_next_batch: not called CC=3 SCAR=24.8
Pod undocumented
283 0 my ($self) = @_;
284 0 my $batch = OpenTracing::Batch->new;
285 0 ▶ my @spans = $self->tracer->extract_finished_spans($self->items_per_batch)
Branch: 1 branch
BranchTrueFalseTotal
my(@spans) = $self->tracer->extract_finished_spans($self->items_per_batch) or return 0 0 0 0
286 or return 0;
287 0 $batch->add_span($_) for @spans;
288  
289 0 my $bytes = pack('n1n1N/a*N1', 0x8001, 4, 'emitBatch', 1)
290 . $self->proto->encode_batch($batch)
291 . pack('C1', 0);
292 0 await $self->send(
293 $bytes
294 );
295 0 return 0 + @spans;
296 1 }
297  
298 =head2 span_completion
299  
300 Our callback for reporting span completion.
301  
302 =cut
303  
304 0 ▶sub span_completion { shift->{span_completion} }
Subroutine span_completion: not called CC=1 SCAR=6.9
Pod documented
305  
306 sub _remove_from_loop {
307 0 ▶ my ($self) = @_;
Subroutine _remove_from_loop: not called CC=2 SCAR=17.9
308 0 ▶ my $span_completion = $self->span_completion or return;
Branch: 1 branch
BranchTrueFalseTotal
my $span_completion = $self->span_completion or return 0 0 0
309 0 $self->tracer->remove_span_completion_callback($span_completion);
310 }
311  
312 =head2 udp
313  
314 The remote UDP endpoint (if it exists).
315  
316 =cut
317  
318 0 ▶sub udp { shift->{udp} }
Subroutine udp: not called CC=1 SCAR=6.9
Pod documented
319  
320 =head2 sync
321  
322 Ensure that we've sent any remaining traces. Can be called just before shutdown
323 to clear off any pending items - this returns a L<Future>, so you'd want code
324 similar to
325  
326 $tracing->sync->get;
327  
328 to ensure that it completes before returning.
329  
330 =cut
331  
332 sub sync {
333 0 ▶ my ($self) = @_;
Subroutine sync: not called CC=1 SCAR=6.9
Pod documented
334 0 ▶ return $self->{connected}->then(async sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
335 0 await $self->send_all_pending;
336 0 });
337 }
338  
339 1 1;
340  
341 =head1 AUTHOR
342  
343 Tom Molesworth <TEAM@cpan.org>
344  
345 =head1 LICENSE
346  
347 Copyright Tom Molesworth 2018-2020. Licensed under the same terms as Perl itself.
348  
↑ Summary