OpenTracing/Protocol/Jaeger.pm

filter: Statementstmt 31.1% 24 / 77 Branchbran 6.2% 1 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 46.6% 7 / 15 Podpod 87.5% 7 / 8 totaltotal 29.7% 39 / 131 CC 18 SCAR 34.8
CC
18
Cov
24%
encode_span
90.0
encode_tags
30.0
encode_process
12.0
CRAP 32.4
« OpenTracing/Protocol/Zipkin.pm ↑ Summary OpenTracing/Integration/System.pm »
1 package OpenTracing::Protocol::Jaeger;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.006'; # VERSION
7 1 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 1 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 OpenTracing::Protocol::Jaeger - support for Jæger binary Thrift representation of OpenTracing data
17  
18 =head1 DESCRIPTION
19  
20 See L<https://github.com/jaegertracing/jaeger-idl/blob/master/thrift/jaeger.thrift> for
21 the current format.
22  
23 =cut
24  
25 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=15.4
29 # We *prefer* Unicode::UTF8, but should not absolutely
30 # require it here.
31 *encode_utf8 = eval {
32 require Unicode::UTF8
33 1 ▶ } ? \&Unicode::UTF8::encode_utf8
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Encode } } ? : 0 0 0
34 1 ▶ : eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Unicode::UTF8 } } ? : 1 0 1
35 0 require Encode;
36 } ? \&Encode::encode_utf8
37 : die 'cannot find UTF8 encoder, try installing Unicode::UTF8';
38 }
39  
40 1 with qw(OpenTracing::Protocol);
41  
42 0 ▶sub new { bless { @_[1..$#_] }, $_[0] }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
43  
44 # We're collecting binary Thrift protocol data here, likely to be sent via UDP.
45  
46 =head2 encode_batch
47  
48 Given an L<OpenTracing::Batch>, iterates through the process and spans,
49 returning a byte string containing binary Thrift data.
50  
51 =cut
52  
53 0 ▶sub encode_batch {
Subroutine encode_batch: not called CC=1 SCAR=6.9
Pod documented
54 0 my ($self, $batch) = @_;
55 0 $log->tracef('Will try to encode batch %s', $batch);
56  
57 0 $log->tracef('Batch spans: %s', $batch->spans);
58 0 my $data = '';
59 0 $data .= pack 'C1n1',
60 12, # struct Batch
61 1; # field 1
62 0 $data .= $self->encode_process($batch->process);
63 0 $data .= $self->encode_span_list($batch->spans);
64 0 $data .= pack 'C1', 0; # EOF marker for Batch
65 0 return $data;
66 }
67  
68 =head2 encode_tags
69  
70 Given a C<$field_id> and a C<$tags> hashref, encodes the key/value
71 tag data as a struct with the given C<$field_id> and returns a
72 byte string representing that data.
73  
74 =cut
75  
76 0 ▶sub encode_tags {
Subroutine encode_tags: not called CC=5 SCAR=34.0
Pod documented
77 0 my ($self, $field_id, $tags) = @_;
78 0 my $data = '';
79 # list Tag
80 0 ▶ $data .= pack 'C1n1 C1N1',
Branch: 1 branch
BranchTrueFalseTotal
if %$tags 0 0 0
81 15, # list
82 $field_id, # field ID is usually 2, or 10 for spans
83 12, # struct
84 0 + keys %$tags if %$tags;
85 0 for my $k (sort keys %$tags) {
86 $data .= pack 'C1n1N/a* C1n1N1 C1n1N/a* C1',
87 11, # type = string
88 1, # field ID = 1
89 encode_utf8($k // ''),
90 8, # type = int32 (enum)
91 2, # field ID = 2
92 0, # entry 0 is string
93 11, # type = string
94 3, # field ID = 3
95 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
96 0; # EOF marker
97 }
98 0 return $data;
99 }
100  
101 =head2 encode_process
102  
103 Given an L<OpenTracing::Process> instance, encodes using the binary Thrift
104 protocol and returns as byte string data.
105  
106 =cut
107  
108 0 ▶sub encode_process {
Subroutine encode_process: not called CC=3 SCAR=24.8
Pod documented
109 0 my ($self, $process) = @_;
110 0 my $data = '';
111 # Process
112 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
113 12, # struct
114 1, # field id 1 = Process
115 # string serviceName
116 11, # string
117 1, # field ID 1 = serviceName
118 encode_utf8($process->name // '');
119  
120 0 ▶ if(my $tags = $process->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tags = $process->tags) 0 0 0
121 0 $data .= $self->encode_tags(2, $tags);
122 }
123 0 $data .= pack 'C1', 0; # EOF marker for process
124 0 return $data;
125 }
126  
127 =head2 encode_span_list
128  
129 Encodes a span list given in the C<$spans> arrayref, calling
130 L</encode_span> for each one, returning byte string data.
131  
132 =cut
133  
134 0 ▶sub encode_span_list {
Subroutine encode_span_list: not called CC=2 SCAR=17.9
Pod documented
135 0 my ($self, $spans) = @_;
136 0 my $data = '';
137 0 $data .= pack 'C1n1C1N1',
138 15, # list
139 2, # field ID 2, spans
140 12, # 12 is struct
141 0 + @$spans;
142 0 $data .= $_ for map { $self->encode_span($_) } @$spans;
143 0 return $data;
144 }
145  
146 =head2 encode_span
147  
148 Encodes the given L<OpenTracing::Span> instance, returning byte string data.
149  
150 =cut
151  
152 0 ▶sub encode_span {
Subroutine encode_span: not called CC=9 SCAR=45.0
Pod documented
153 0 my ($self, $span) = @_;
154 0 my $data = '';
155 0 ▶ $data .= pack 'CnH16 CnH16 CnH16 CnH16 CnN/a*',
Condition0%0/2
$span->operation_name // ""
l!l
00
MC/DC0%0/1
$span->operation_name // ""
$span->operation_name
Truth table0%0/2
A
A= $span->operation_name
Aresult
11
00
156 # trace_id_low
157 10,
158 1,
159 substr($span->trace_id, 16, 16),
160 # trace_id_high
161 10,
162 2,
163 substr($span->trace_id, 0, 16),
164 # span_id
165 10,
166 3,
167 substr($span->id, 0, 16),
168 # parent_span_id
169 10,
170 4,
171 substr($span->parent_id, 0, 16),
172 # operation_name
173 11,
174 5,
175 encode_utf8($span->operation_name // '');
176  
177 # references
178 0 ▶ if(my $references = $span->references) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $references = $span->references) 0 0 0
179 # list Log
180 0 $data .= pack 'C1n1 C1N1',
181 15, # list
182 6, # field ID 6 for References
183 12, # struct
184 0 + @$references;
185 0 for my $reference (@$references) {
186 0 $data .= pack 'C1n1N1 CnH16 CnH16 CnH16 C1',
187 8, # type = int32 (enum)
188 1,
189 $reference->ref_type,
190 # trace_id_low
191 10,
192 2,
193 substr($reference->context->trace_id, 16, 16),
194 # trace_id_high
195 10,
196 3,
197 substr($reference->context->trace_id, 0, 16),
198 # span_id
199 10,
200 4,
201 substr($reference->context->id, 0, 16),
202 0;
203 }
204 }
205  
206 0 ▶ $data .= pack 'CnN CnQ> CnQ>',
Condition0%0/2
$span->flags // 0
l!l
00
MC/DC0%0/1
$span->flags // 0
$span->flags
Truth table0%0/2
A
A= $span->flags
Aresult
11
00
207 # flags
208 8,
209 7,
210 $span->flags // 0,
211 # start_time
212 10,
213 8,
214 $span->start_time,
215 # duration
216 10,
217 9,
218 $span->duration;
219 0 ▶ if(my $tags = $span->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tags = $span->tags) 0 0 0
220 0 $data .= $self->encode_tags(10, $tags);
221 }
222  
223 0 ▶ if(my $logs = $span->logs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logs = $span->logs) 0 0 0
224 # list Log
225 0 $data .= pack 'C1n1 C1N1',
226 15, # list
227 11, # field ID 11 for logs
228 12, # struct
229 0 + @$logs;
230 0 for my $log (@$logs) {
231 0 my $tags = $log->tags;
232 0 $data .= pack 'C1n1Q>',
233 10, # type = int64
234 1, # field ID = 1
235 $log->timestamp;
236 0 ▶ $data .= $self->encode_tags(2, $tags) if $tags;
Branch: 1 branch
BranchTrueFalseTotal
if $tags 0 0 0
237 0 $data .= pack 'C1', 0; # EOF for log
238 }
239 }
240  
241 0 $data .= pack 'C1', 0; # EOF for span
242 0 return $data;
243 }
244  
245 =head2 bytes_from_span
246  
247 Generate byte string encoding for the given L<OpenTracing::Span> instance.
248  
249 =cut
250  
251 0 ▶sub bytes_from_span {
Subroutine bytes_from_span: not called CC=1 SCAR=6.9
Pod documented
252 0 my ($self, $span) = @_;
253 0 return $self->encode_span($span);
254 }
255  
256 =head2 span_from_bytes
257  
258 Converts a byte string representation into an L<OpenTracing::Span> instance.
259  
260 =cut
261  
262 0 ▶sub span_from_bytes {
Subroutine span_from_bytes: not called CC=1 SCAR=6.9
Pod documented
263 0 my ($self, $data) = @_;
264 ...
265 0 }
266  
267 1 1;
268  
269 __END__
« OpenTracing/Protocol/Zipkin.pm ↑ Summary OpenTracing/Integration/System.pm »