Exporter/OTLP/Encoder/Protobuf.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 93 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.022';
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 {} unless defined $v;
32  
33 return { kvlist_value => { values => $self->encode_kvlist($v) } }
34 if is_hashref $v;
35  
36 return { array_value => { values => $self->encode_arraylist($v) } }
37 if is_arrayref $v;
38  
39 if ( my $ref = ref $v ) {
40 warn "Unsupported ref while encoding: $ref";
41 return {};
42 }
43  
44 # TODO: not strings
45 return { string_value => "$v" };
46 }
47  
48 method encode_resource ( $resource ) {
49 {
50 attributes => $self->encode_kvlist($resource->attributes),
51 dropped_attributes_count => $resource->dropped_attributes,
52 };
53 }
54  
55 method encode_span_event ( $event ) {
56 {
57 attributes => $self->encode_kvlist($event->attributes),
58 dropped_attributes_count => $event->dropped_attributes,
59 name => $event->name,
60 time_unix_nano => int $event->timestamp * 1_000_000_000,
61 };
62 }
63  
64 method encode_span_link ( $link ) {
65 {
66 attributes => $self->encode_kvlist($link->attributes),
67 dropped_attributes_count => $link->dropped_attributes,
68 span_id => $link->context->span_id,
69 trace_id => $link->context->trace_id,
70 };
71 }
72  
73 method encode_span ( $span ) {
74 my $data = {
75 attributes => $self->encode_kvlist($span->attributes),
76 dropped_attributes_count => $span->dropped_attributes,
77 dropped_events_count => $span->dropped_events,
78 dropped_links_count => $span->dropped_links,
79 end_time_unix_nano => int $span->end_timestamp * 1_000_000_000,
80 events => [ map $self->encode_span_event($_), $span->events ],
81 kind => $span->kind,
82 links => [ map $self->encode_span_link($_), $span->links ],
83 name => $span->name,
84 span_id => $span->span_id,
85 start_time_unix_nano => int $span->start_timestamp * 1_000_000_000,
86 status => $self->encode_span_status($span->status),
87 trace_id => $span->trace_id,
88 trace_state => $span->trace_state->to_string,
89 };
90  
91 my $parent = $span->parent_span_id;
92 $data->{parent_span_id} = $parent
93 unless $parent eq INVALID_SPAN_ID;
94  
95 $data;
96 }
97  
98 method encode_log ( $log ) {
99 my $data = {
100 attributes => $self->encode_kvlist($log->attributes),
101 body => $self->encode_anyvalue( $log->body ),
102 dropped_attributes_count => $log->dropped_attributes,
103 flags => $log->trace_flags->flags,
104 observed_time_unix_nano => int $log->observed_timestamp * 1_000_000_000,
105 severity_number => 0+$log->severity_number,
106 severity_text => $log->severity_text,
107 span_id => $log->span_id,
108 trace_id => $log->trace_id,
109 };
110  
111 my $t = $log->timestamp;
112 $data->{time_unix_nano} = int $t * 1_000_000_000 if defined $t;
113  
114 $data;
115 }
116  
117 method encode_scope ( $scope ) {
118 {
119 attributes => $self->encode_kvlist($scope->attributes),
120 dropped_attributes_count => $scope->dropped_attributes,
121 name => $scope->name,
122 version => $scope->version,
123 };
124 }
125  
126 method encode_scope_logs ( $scope, $logs ) {
127 {
128 scope => $self->encode_scope($scope),
129 log_records => [ map $self->encode_log($_), @$logs ],
130 };
131 }
132  
133 method encode_resource_spans ( $resource, $spans ) {
134 my %scopes;
135  
136 for (@$spans) {
137 my $key = refaddr $_->instrumentation_scope;
138  
139 $scopes{ $key } //= [ $_->instrumentation_scope, [] ];
140 push @{ $scopes{ $key }[1] }, $_;
141 }
142  
143 {
144 resource => $self->encode_resource($resource),
145 scope_spans => [
146 map $self->encode_scope_spans(@$_), values %scopes,
147 ],
148 schema_url => $resource->schema_url,
149 };
150 }
151  
152 method encode_resource_logs ( $resource, $logs ) {
153 my %scopes;
154  
155 for (@$logs) {
156 my $key = refaddr $_->instrumentation_scope;
157  
158 $scopes{ $key } //= [ $_->instrumentation_scope, [] ];
159 push @{ $scopes{ $key }[1] }, $_;
160 }
161  
162 {
163 resource => $self->encode_resource($resource),
164 scope_logs => [
165 map $self->encode_scope_logs(@$_), values %scopes,
166 ],
167 schema_url => $resource->schema_url,
168 };
169 }
170 }