OpenTracing/Protocol/Zipkin.pm

filter: Statementstmt 40.3% 25 / 62 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 70.0% 7 / 10 Podpod 66.6% 2 / 3 totaltotal 29.8% 34 / 114 CC 22 SCAR 57.5
CC
22
Cov
25%
bytes_from_span
462.0
span_from_bytes
6.0
new
2.0
CRAP 313.6
↑ Summary OpenTracing/Protocol/Jaeger.pm »
1 package OpenTracing::Protocol::Zipkin;
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 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 OpenTracing::Protocol::Zipkin - support for Zipkin v2 JSON representation of OpenTracing data
15  
16 =head1 DESCRIPTION
17  
18 See L<https://zipkin.io/zipkin-api/#/default/post_spans> for details on
19 the current format.
20  
21 =cut
22  
23 1 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 with qw(OpenTracing::Protocol);
29  
30 0 ▶sub new { bless { @_[1..$#_] }, $_[0] }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
31  
32 =head2 bytes_from_span
33  
34 Returns the given data structure as a bytestream containing
35 a JSON UTF-8 representation, as defined by Zipkin, Datadog and
36 other providers.
37  
38 The resulting JSON will have at most the following keys:
39  
40 =over 4
41  
42 =item * C<id> - the span ID
43  
44 =item * C<traceId> - the trace ID
45  
46 =item * C<parentId> - this trace's parent ID
47  
48 =item * C<localEndpoint> - where this span was running
49  
50 =item * C<remoteEndpoint> - the remote connection that this span was involved with
51  
52 =item * C<annotations> - any timestamp annotations relating to this span
53  
54 =item * C<tags> - any key/value pairs relating to this span
55  
56 =back
57  
58 =cut
59  
60 0 ▶sub bytes_from_span {
Subroutine bytes_from_span: not called CC=21 SCAR=61.4
Pod documented
61 0 my ($self, $data) = @_;
62  
63 # We'd like to be quite careful about types here. This incurs some performance
64 # overhead, but the JSON encoding still dominates.
65 my %copy = (
66 id => '' . $data->{id},
67 traceId => '' . $data->{trace_id},
68 0 );
69 0 ▶ $copy{debug} = JSON()->true if $data->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"debug"} 0 0 0
70 0 ▶ $copy{shared} = JSON()->true if $data->{shared};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"shared"} 0 0 0
71 0 ▶ $copy{name} = '' . $data->{name} if defined $data->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"name"} 0 0 0
72 0 ▶ $copy{parentId} = '' . $data->{parent_id} if defined $data->{parent_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"parent_id"} 0 0 0
73 0 ▶ $copy{kind} = '' . $data->{kind} if defined $data->{kind};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"kind"} 0 0 0
74 0 ▶ $copy{timestamp} = 0 + $data->{timestamp} if defined $data->{timestamp};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"timestamp"} 0 0 0
75 0 ▶ $copy{duration} = 0 + $data->{duration} if defined $data->{duration};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"duration"} 0 0 0
76  
77 0 ▶ if(my $endpoint = $data->{local_endpoint}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $endpoint = $data->{"local_endpoint"}) 0 0 0
78 0 $copy{localEndpoint} = my $target = { };
79 0 ▶ $target->{serviceName} = '' . $endpoint->{service_name} if defined $endpoint->{service_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"service_name"} 0 0 0
80 0 ▶ $target->{ipv4} = '' . $endpoint->{ipv4} if defined $endpoint->{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"ipv4"} 0 0 0
81 0 ▶ $target->{ipv6} = '' . $endpoint->{ipv6} if defined $endpoint->{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"ipv6"} 0 0 0
82 0 ▶ $target->{port} = 0 + $endpoint->{port} if defined $endpoint->{port};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"port"} 0 0 0
83 }
84  
85 0 ▶ if(my $endpoint = $data->{remote_endpoint}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $endpoint = $data->{"remote_endpoint"}) 0 0 0
86 0 $copy{remoteEndpoint} = my $target = { };
87 0 ▶ $target->{serviceName} = $endpoint->{service_name} if defined $endpoint->{service_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"service_name"} 0 0 0
88 0 ▶ $target->{ipv4} = '' . $endpoint->{ipv4} if defined $endpoint->{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"ipv4"} 0 0 0
89 0 ▶ $target->{ipv6} = '' . $endpoint->{ipv6} if defined $endpoint->{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"ipv6"} 0 0 0
90 0 ▶ $target->{port} = 0 + $endpoint->{port} if defined $endpoint->{port};
Branch: 1 branch
BranchTrueFalseTotal
if defined $endpoint->{"port"} 0 0 0
91 }
92  
93 0 for my $annotation (@{$data->{annotations}}) {
94 0 ▶ push @{$copy{annotations} //= []}, {
Condition0%0/2
$copy{"annotations"} //= []
l!l
00
MC/DC0%0/1
$copy{"annotations"} //= []
$copy{"annotations"}
Truth table0%0/2
A
A= $copy{"annotations"}
Aresult
11
00
95 timestamp => 0 + $annotation->{timestamp},
96 value => '' . $annotation->{value},
97 }
98 0 }
99 0 ▶ @{$copy{tags}}{keys %{$data->{tags}}} = map { "$_" } values %{$data->{tags}} if $data->{tags};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"tags"} 0 0 0
100  
101 0 return encode_json_utf8(
102 \%copy
103 );
104 }
105  
106 =head2 span_from_bytes
107  
108 Takes a bytestring containing UTF-8-encoded JSON data, and returns a
109 Perl hashref representing a span.
110  
111 =cut
112  
113 0 ▶sub span_from_bytes {
Subroutine span_from_bytes: not called CC=2 SCAR=17.9
Pod documented
114 0 my ($self, $data) = @_;
115 0 my $decoded = decode_json_utf8($data);
116 0 for (grep /[A-Z]/, keys %$decoded) {
117 0 $decoded->{ s{([A-Z])}{_\L$1}gr } = delete $decoded->{$_}
118 }
119 0 return $decoded;
120 }
121  
122 1 1;
123  
124 =head1 AUTHOR
125  
126 Tom Molesworth C<< <TEAM@cpan.org> >>
127  
128 =head1 LICENSE
129  
130 Copyright Tom Molesworth 2018-2021. Licensed under the same terms as Perl itself.
131  
132 1;
↑ Summary OpenTracing/Protocol/Jaeger.pm »