blib/lib/Net/Async/OpenTracing.pm

filter: Statementstmt 36.9% 41 / 111 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 48.1% 13 / 27 Podpod 12.5% 1 / 8 totaltotal 28.3% 55 / 194 CC 25 SCAR 52.6
CC
25
Cov
28%
send_batch
462.0
configure
20.0
process
6.0
CRAP 192.7
↑ 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 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.001';
10  
11 1 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Net::Async::OpenTracing - basic proof-of-concept implementation for OpenTracing APM
18  
19 =head1 DESCRIPTION
20  
21 This all relies on the abstract L<OpenTracing> interface, so that'd be
22 the first port of call for official documentation.
23  
24 =head2 Setting up and testing
25  
26 Start up a Jæger instance in Docker like so:
27  
28 docker run -d --name jaeger \
29 -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
30 -p 5775:5775/udp \
31 -p 6831:6831/udp \
32 -p 6832:6832/udp \
33 -p 5778:5778 \
34 -p 16686:16686 \
35 -p 14268:14268 \
36 -p 9411:9411 \
37 jaegertracing/all-in-one:1.12
38  
39 Set up an L<Net::Async::OpenTracing> instance with those connection details:
40  
41 my $loop = IO::Async::Loop->new;
42 $loop->add(
43 my $tracing = Net::Async::OpenTracing->new(
44 host => '127.0.0.1',
45 port => 6832,
46 )
47 );
48 {
49 my $batch = $tracing->new_batch();
50 my $span = $batch->new_span(
51 'example_span'
52 );
53 $span->log('test message ' . $_ . ' from the parent') for 1..3;
54 my $child = $span->new_span('child_span');
55 $child->log('message ' . $_ . ' from the child span') for 1..3;
56 }
57 # Make sure all trace data is sent
58 $tracing->sync->get;
59  
60 You should then see a trace with 2 spans show up.
61  
62 =cut
63  
64 1 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Unicode::UTF8 qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Math::Random::Secure;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use IO::Async::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶use OpenTracing;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 # The documentation is less clear on this than I'd like:
77 # - ordinal value of the list is presumably 0, but might also be 1,
78 # so we extract to a constant here
79 1 ▶use constant ENUM_BASE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 sub configure {
82 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=4 SCAR=30.0
Pod documented
83 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
84 0 for my $k (qw(host port)) {
85 0 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 0 0
86 }
87 0 return $self->next::method(%args);
88 }
89  
90 0 ▶sub host { shift->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
91 0 ▶sub port { shift->{port} }
Subroutine port: not called CC=1 SCAR=6.9
Pod undocumented
92  
93 sub _add_to_loop {
94 0 ▶ my ($self) = @_;
Subroutine _add_to_loop: not called CC=1 SCAR=6.9
95 $self->add_child(
96 $self->{udp} = my $client = IO::Async::Socket->new(
97 on_recv => sub {
98 0 ▶ my ($sock, $payload, $addr) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 try {
100 0 $log->warnf("Receiving [%s] from %s - unexpected, this should be one-way opentracing traffic from us?", $payload, $addr);
101 0 } catch {
102 0 $log->errorf("Exception when sending: %s", $@);
103 }
104 },
105 )
106 0 );
107 $self->{connected} = $client->connect(
108 host => $self->host,
109 service => $self->port,
110 socktype => 'dgram',
111 )->then(sub {
112 0 ▶ $log->tracef('Connected to UDP endpoint');
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 my @pending = splice $self->{pending}->@*;
114 0 $log->tracef('Have %d messages to send', 0 + @pending);
115 Future->wait_all(
116 0 map { $client->send($_) } @pending
117 )
118 0 })->retain;
119 }
120  
121 sub process {
122 0 ▶ my ($self) = @_;
Subroutine process: not called CC=2 SCAR=17.9
Pod undocumented
123 0 ▶ $self->{process} //= do {
Condition0%0/3
$self->{"process"} //= do { "OpenTracing::Process"->new("name", "$0") }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"process"} //= do { "OpenTracing::Process"->new("name", "$0") }
$self->{"process"} do { "OpenTracing::Process"->new("name", "$0") }
Truth table0%0/3
A //= B
A= $self->{"process"}B= do { "OpenTracing::Process"->new("name", "$0") }
ABresult
1X1
011
000
124 0 OpenTracing::Process->new(
125 name => "$0"
126 )
127 }
128 }
129  
130 0 ▶sub udp { shift->{udp} }
Subroutine udp: not called CC=1 SCAR=6.9
Pod undocumented
131  
132 sub new_batch {
133 0 ▶ my ($self, %args) = @_;
Subroutine new_batch: not called CC=1 SCAR=6.9
Pod undocumented
134 0 $log->tracef('Creating new opentracing batch');
135 return OpenTracing::Batch->new(
136 process => $self->process,
137 on_destroy => sub {
138 0 ▶ $log->infof('Destroy for batch');
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 $self->send_batch(shift);
140 }
141 0 );
142 }
143  
144 sub send_batch {
145 0 ▶ my ($self, $batch) = @_;
Subroutine send_batch: not called CC=21 SCAR=61.4
Pod undocumented
146 0 $log->tracef('Will try to send batch %s', $batch);
147  
148 # We're collecting binary Thrift protocol data here, and will send via UDP.
149 0 my $data = '';
150 0 $data .= pack 'C1n1',
151 12, # struct Batch
152 1; # field 1
153  
154 0 my $process = $batch->process;
155 # Process
156 0 ▶ $data .= pack 'C1n1 C1n1N/a*',
Condition0%0/2
$process->name // ""
l!l
00
MC/DC0%0/1
$process->name // ""
$process->name
Truth table0%0/2
A
A= $process->name
Aresult
11
00
157 12, # struct
158 1, # field id 1 = Process
159 # string serviceName
160 11, # string
161 1, # field ID 1 = serviceName
162 encode_utf8($process->name // '');
163  
164 0 ▶ if(my $tags = $process->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tags = $process->tags) 0 0 0
165 # list Tag
166 0 ▶ $data .= pack 'C1n1 C1N1',
Branch: 1 branch
BranchTrueFalseTotal
if %$tags 0 0 0
167 15, # list
168 2, # field ID 2
169 12, # struct
170 0 + keys %$tags if %$tags;
171 0 for my $k (sort keys %$tags) {
172 $data .= pack 'C1n1N/a* C1n1N1 C1n1N/a* C1',
173 11, # type = string
174 1, # field ID = 1
175 encode_utf8($k // ''),
176 8, # type = int32 (enum)
177 2, # field ID = 2
178 ENUM_BASE, # entry 0 is string
179 11, # type = string
180 3, # field ID = 3
181 0 ▶ encode_utf8($tags->{$k} // ''),
Condition0%0/4
$k // ""
l!l
00
$tags->{$k} // ""
l!l
00
MC/DC0%0/1
$k // ""
$k
MC/DC0%0/1
$tags->{$k} // ""
$tags->{$k}
Truth table0%0/2
A
A= $k
Aresult
11
00
Truth table0%0/2
A
A= $tags->{$k}
Aresult
11
00
182 0; # EOF marker
183 }
184 }
185 0 $data .= pack 'C1', 0; # EOF marker for process
186  
187 # list<Span>
188 0 my @spans = $batch->span_list;
189 0 $data .= pack 'C1n1C1N1',
190 15,
191 2,
192 12, # 12 is struct
193 0 + @spans;
194 0 for my $span (@spans) {
195 0 ▶ $data .= pack 'CnQ> CnQ> CnQ> CnQ> CnN/a* CnN CnQ> CnQ>',
Condition0%0/4
$span->operation_name // ""
l!l
00
$span->flags // 0
l!l
00
MC/DC0%0/1
$span->operation_name // ""
$span->operation_name
MC/DC0%0/1
$span->flags // 0
$span->flags
Truth table0%0/2
A
A= $span->operation_name
Aresult
11
00
Truth table0%0/2
A
A= $span->flags
Aresult
11
00
196 # trace_id_low
197 10,
198 1,
199 $span->trace_id,
200 # trace_id_high
201 10,
202 2,
203 0, # Math::Random::Secure::irand(2**62),
204 # span_id
205 10,
206 3,
207 $span->id,
208 # parent_span_id
209 10,
210 4,
211 $span->parent_id,
212 # operation_name
213 11,
214 5,
215 encode_utf8($span->operation_name // ''),
216 # references
217 # flags
218 8,
219 7,
220 $span->flags // 0,
221 # start_time
222 10,
223 8,
224 $span->start_time,
225 # duration
226 10,
227 9,
228 $span->duration;
229 # tags
230 0 ▶ if(my $tags = $span->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tags = $span->tags) 0 0 0
231 # list Tag
232 0 ▶ $data .= pack 'C1n1 C1N1',
Branch: 1 branch
BranchTrueFalseTotal
if %$tags 0 0 0
233 15, # list
234 10, # field ID 2 (?? why 10 then?)
235 12, # struct
236 0 + keys %$tags if %$tags;
237 0 for my $k (sort keys %$tags) {
238 $data .= pack 'C1n1N/a* C1n1N1 C1n1N/a* C1',
239 11, # type = string
240 1, # field ID = 1
241 encode_utf8($k // ''),
242 8, # type = int32 (enum)
243 2, # field ID = 2
244 ENUM_BASE, # entry 0 is string
245 11, # type = string
246 3, # field ID = 3
247 0 ▶ encode_utf8($tags->{$k} // ''),
Condition0%0/4
$k // ""
l!l
00
$tags->{$k} // ""
l!l
00
MC/DC0%0/1
$k // ""
$k
MC/DC0%0/1
$tags->{$k} // ""
$tags->{$k}
Truth table0%0/2
A
A= $k
Aresult
11
00
Truth table0%0/2
A
A= $tags->{$k}
Aresult
11
00
248 0; # EOF marker
249 }
250 }
251 0 ▶ if(my @logs = $span->log_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@logs) = $span->log_list) 0 0 0
252 # list Log
253 0 $data .= pack 'C1n1 C1N1',
254 15, # list
255 11, # field ID 11 for logs
256 12, # struct
257 0 + @logs;
258 0 for my $log (@logs) {
259 0 my $tags = $log->tags;
260 0 ▶ $data .= pack 'C1n1Q> C1n1 C1N1',
Branch: 1 branch
BranchTrueFalseTotal
if %$tags 0 0 0
261 10, # type = int64
262 1, # field ID = 1
263 $log->timestamp,
264 15, # list
265 2, # field ID 2
266 12, # list of structs
267 0 + keys %$tags if %$tags;
268 0 for my $k (sort keys %$tags) {
269 $data .= pack 'C1n1N/a* C1n1N1 C1n1N/a* C1',
270 11, # type = string
271 1, # field ID = 1
272 encode_utf8($k),
273 8, # type = int32 (enum)
274 2, # field ID = 2
275 ENUM_BASE, # entry 0 is string
276 11, # type = string
277 3, # field ID = 3
278 0 ▶ encode_utf8($tags->{$k} // ''),
Condition0%0/2
$tags->{$k} // ""
l!l
00
MC/DC0%0/1
$tags->{$k} // ""
$tags->{$k}
Truth table0%0/2
A
A= $tags->{$k}
Aresult
11
00
279 0; # EOF marker
280 }
281 0 $data .= pack 'C1', 0; # EOF for log
282 }
283 }
284  
285 0 $data .= pack 'C1', 0; # EOF for span
286 }
287  
288 0 $data .= pack 'C1', 0; # EOF marker for Batch
289 0 $data .= pack 'C1', 0; # EOF marker for method call
290  
291 # For the collector, we might want something like this:
292 # my $msg = pack('n1n1N/a*N1', 0x8001, 1, 'submitBatches', 1) . $data;
293 # but for UDP, we only get to talk to the agent, so it's a oneway emitBatch instead
294 0 my $msg = pack('n1n1N/a*N1', 0x8001, 4, 'emitBatch', 1) . $data;
295 0 ▶ if($self->{connected}->is_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'connected'}->is_done) { } 0 0 0
296 0 $log->tracef('Sending packet now');
297 0 $self->udp->send($msg, autoflush => 1);
298 } else {
299 0 $log->tracef('Defer packet until connection is ready');
300 0 push $self->{pending}->@*, $msg;
301 }
302 }
303  
304 sub sync {
305 0 ▶ my ($self) = @_;
Subroutine sync: not called CC=1 SCAR=6.9
Pod undocumented
306 return $self->{connected}->then(sub {
307 0 ▶ my $f = $self->loop->new_future;
Subroutine __ANON__: not called CC=1 SCAR=6.9
308 $self->udp->configure(
309 0 ▶ on_outgoing_empty => sub { $f->done }
Subroutine __ANON__: not called CC=1 SCAR=6.9
310 0 );
311 0 $f
312 0 });
313 }
314  
315 1 1;
316  
317 =head1 AUTHOR
318  
319 Tom Molesworth <TEAM@cpan.org>
320  
321 =head1 LICENSE
322  
323 Copyright Tom Molesworth 2018-2019. Licensed under the same terms as Perl itself.
324  
↑ Summary