Exporter/OTLP/Encoder/Protobuf.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 90 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use Object::Pad ':experimental(init_expr)';
2 # ABSTRACT: A Protobuf encoder for the OTLP exporter
3  
4 package OpenTelemetry::Exporter::OTLP::Encoder::Protobuf;
5  
6 our $VERSION = '0.021';
7  
8 class OpenTelemetry::Exporter::OTLP::Encoder::Protobuf
9 :isa(OpenTelemetry::Exporter::OTLP::Encoder::JSON) {
10  
11 use OpenTelemetry::Constants 'INVALID_SPAN_ID';
12 use OpenTelemetry::Proto;
13 use Ref::Util qw( is_hashref is_arrayref );
14 use Scalar::Util 'refaddr';
15  
16 method content_type () { 'application/x-protobuf' }
17  
18 method serialise ($data) {
19 return OpenTelemetry::Proto::Collector::Logs::V1::ExportLogsServiceRequest
20 ->new_and_check({ resource_logs => $data->{resourceLogs} })->encode
21 if $data->{resourceLogs};
22  
23 return OpenTelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest
24 ->new_and_check({ resource_spans => $data->{resourceSpans} })->encode
25 if $data->{resourceSpans};
26  
27 die 'Unknown payload type';
28 }
29  
30 method encode_anyvalue ( $v ) {
31 return { kvlist_value => { values => $self->encode_kvlist($v) } }
32 if is_hashref $v;
33  
34 return { array_value => { values => $self->encode_arraylist($v) } }
35 if is_arrayref $v;
36  
37 if ( my $ref = ref $v ) {
38 warn "Unsupported ref while encoding: $ref";
39 return;
40 }
41  
42 # TODO: not strings
43 return { string_value => "$v" };
44 }
45  
46 method encode_resource ( $resource ) {
47 {
48 attributes => $self->encode_kvlist($resource->attributes),
49 dropped_attributes_count => $resource->dropped_attributes,
50 };
51 }
52  
53 method encode_span_event ( $event ) {
54 {
55 attributes => $self->encode_kvlist($event->attributes),
56 dropped_attributes_count => $event->dropped_attributes,
57 name => $event->name,
58 time_unix_nano => int $event->timestamp * 1_000_000_000,
59 };
60 }
61  
62 method encode_span_link ( $link ) {
63 {
64 attributes => $self->encode_kvlist($link->attributes),
65 dropped_attributes_count => $link->dropped_attributes,
66 span_id => $link->context->span_id,
67 trace_id => $link->context->trace_id,
68 };
69 }
70  
71 method encode_span ( $span ) {
72 my $data = {
73 attributes => $self->encode_kvlist($span->attributes),
74 dropped_attributes_count => $span->dropped_attributes,
75 dropped_events_count => $span->dropped_events,
76 dropped_links_count => $span->dropped_links,
77 end_time_unix_nano => int $span->end_timestamp * 1_000_000_000,
78 events => [ map $self->encode_span_event($_), $span->events ],
79 kind => $span->kind,
80 links => [ map $self->encode_span_link($_), $span->links ],
81 name => $span->name,
82 span_id => $span->span_id,
83 start_time_unix_nano => int $span->start_timestamp * 1_000_000_000,
84 status => $self->encode_span_status($span->status),
85 trace_id => $span->trace_id,
86 trace_state => $span->trace_state->to_string,
87 };
88  
89 my $parent = $span->parent_span_id;
90 $data->{parent_span_id} = $parent
91 unless $parent eq INVALID_SPAN_ID;
92  
93 $data;
94 }
95  
96 method encode_log ( $log ) {
97 my $data = {
98 attributes => $self->encode_kvlist($log->attributes),
99 body => $self->encode_anyvalue( $log->body ),
100 dropped_attributes_count => $log->dropped_attributes,
101 flags => $log->trace_flags->flags,
102 observed_time_unix_nano => int $log->observed_timestamp * 1_000_000_000,
103 severity_number => 0+$log->severity_number,
104 severity_text => $log->severity_text,
105 span_id => $log->span_id,
106 trace_id => $log->trace_id,
107 };
108  
109 my $t = $log->timestamp;
110 $data->{time_unix_nano} = int $t * 1_000_000_000 if defined $t;
111  
112 $data;
113 }
114  
115 method encode_scope ( $scope ) {
116 {
117 attributes => $self->encode_kvlist($scope->attributes),
118 dropped_attributes_count => $scope->dropped_attributes,
119 name => $scope->name,
120 version => $scope->version,
121 };
122 }
123  
124 method encode_scope_logs ( $scope, $logs ) {
125 {
126 scope => $self->encode_scope($scope),
127 log_records => [ map $self->encode_log($_), @$logs ],
128 };
129 }
130  
131 method encode_resource_spans ( $resource, $spans ) {
132 my %scopes;
133  
134 for (@$spans) {
135 my $key = refaddr $_->instrumentation_scope;
136  
137 $scopes{ $key } //= [ $_->instrumentation_scope, [] ];
138 push @{ $scopes{ $key }[1] }, $_;
139 }
140  
141 {
142 resource => $self->encode_resource($resource),
143 scope_spans => [
144 map $self->encode_scope_spans(@$_), values %scopes,
145 ],
146 schema_url => $resource->schema_url,
147 };
148 }
149  
150 method encode_resource_logs ( $resource, $logs ) {
151 my %scopes;
152  
153 for (@$logs) {
154 my $key = refaddr $_->instrumentation_scope;
155  
156 $scopes{ $key } //= [ $_->instrumentation_scope, [] ];
157 push @{ $scopes{ $key }[1] }, $_;
158 }
159  
160 {
161 resource => $self->encode_resource($resource),
162 scope_logs => [
163 map $self->encode_scope_logs(@$_), values %scopes,
164 ],
165 schema_url => $resource->schema_url,
166 };
167 }
168 }