OpenTracing/Protocol/Zipkin.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« OpenTracing.pm ↑ Summary OpenTracing/Common.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 our $VERSION = '1.004'; # VERSION
7 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=2.6
24 use JSON::MaybeXS qw(JSON);
25  
26 use Role::Tiny::With;
27  
28 with qw(OpenTracing::Protocol);
29  
30 sub new { bless { @_[1..$#_] }, $_[0] }
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 sub bytes_from_span {
61 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 );
69 $copy{debug} = JSON()->true if $data->{debug};
70 $copy{shared} = JSON()->true if $data->{shared};
71 $copy{name} = '' . $data->{name} if defined $data->{name};
72 $copy{parentId} = '' . $data->{parent_id} if defined $data->{parent_id};
73 $copy{kind} = '' . $data->{kind} if defined $data->{kind};
74 $copy{timestamp} = 0 + $data->{timestamp} if defined $data->{timestamp};
75 $copy{duration} = 0 + $data->{duration} if defined $data->{duration};
76  
77 if(my $endpoint = $data->{local_endpoint}) {
78 $copy{localEndpoint} = my $target = { };
79 $target->{serviceName} = '' . $endpoint->{service_name} if defined $endpoint->{service_name};
80 $target->{ipv4} = '' . $endpoint->{ipv4} if defined $endpoint->{ipv4};
81 $target->{ipv6} = '' . $endpoint->{ipv6} if defined $endpoint->{ipv6};
82 $target->{port} = 0 + $endpoint->{port} if defined $endpoint->{port};
83 }
84  
85 if(my $endpoint = $data->{remote_endpoint}) {
86 $copy{remoteEndpoint} = my $target = { };
87 $target->{serviceName} = $endpoint->{service_name} if defined $endpoint->{service_name};
88 $target->{ipv4} = '' . $endpoint->{ipv4} if defined $endpoint->{ipv4};
89 $target->{ipv6} = '' . $endpoint->{ipv6} if defined $endpoint->{ipv6};
90 $target->{port} = 0 + $endpoint->{port} if defined $endpoint->{port};
91 }
92  
93 for my $annotation (@{$data->{annotations}}) {
94 push @{$copy{annotations} //= []}, {
95 timestamp => 0 + $annotation->{timestamp},
96 value => '' . $annotation->{value},
97 }
98 }
99 @{$copy{tags}}{keys %{$data->{tags}}} = map { "$_" } values %{$data->{tags}} if $data->{tags};
100  
101 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 sub span_from_bytes {
114 my ($self, $data) = @_;
115 my $decoded = decode_json_utf8($data);
116 for (grep /[A-Z]/, keys %$decoded) {
117 $decoded->{ s{([A-Z])}{_\L$1}gr } = delete $decoded->{$_}
118 }
119 return $decoded;
120 }
121  
122 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;
« OpenTracing.pm ↑ Summary OpenTracing/Common.pm »