Exporter/OTLP/Encoder/JSON.pm

filter: Statementstmt 93.7% 149 / 159 Branchbran 85.7% 12 / 14 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 91.3% 21 / 23 Podpod 17.6% 3 / 17 totaltotal 85.7% 193 / 225 CC 15 SCAR 9.4
CC
15
Cov
92%
encode_anyvalue
5.0
encode
5.0
encode_resource_spans
3.0
CRAP 2.6
« Exporter/OTLP/Logs.pm ↑ Summary Exporter/OTLP.pm »
1 3 ▶use Object::Pad ':experimental(init_expr)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2 # ABSTRACT: A JSON encoder for the OTLP exporter
3  
4 package OpenTelemetry::Exporter::OTLP::Encoder::JSON;
5  
6 3 our $VERSION = '0.022';
7  
8 class OpenTelemetry::Exporter::OTLP::Encoder::JSON {
9 3 ▶ use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶ use OpenTelemetry::Constants 'HEX_INVALID_SPAN_ID';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶ use Ref::Util qw( is_hashref is_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶ use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶ use isa 'OpenTelemetry::SDK::Logs::LogRecord';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶ method content_type () { 'application/json' }
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
17  
18 7 ▶ method serialise ($data) { encode_json $data }
Subroutine serialise: called CC=1 SCAR=0.0
Pod documented
19  
20 2 ▶ method encode_arraylist ($v) {
Subroutine encode_arraylist: called CC=1 SCAR=0.0
Pod undocumented
21 2 [ map $self->encode_anyvalue($_), @$v ]
22 3 }
23  
24 54 ▶ method encode_kvlist ($v) {
Subroutine encode_kvlist: called CC=1 SCAR=0.0
Pod undocumented
25 [
26 map {
27 54 {
28 key => $_,
29 8 value => $self->encode_anyvalue( $v->{$_} )
30 }
31 } keys %$v
32 ]
33 3 }
34  
35 26 ▶ method encode_anyvalue ( $v ) {
Subroutine encode_anyvalue: called CC=5 SCAR=16.1
Pod undocumented
36 26 ▶ return {} unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 6 20 26
37  
38 20 ▶ return { kvlistValue => { values => $self->encode_kvlist($v) } }
Branch: 1 branch
BranchTrueFalseTotal
if is_hashref($v) 1 19 20
39 if is_hashref $v;
40  
41 19 ▶ return { arrayValue => { values => $self->encode_arraylist($v) } }
Branch: 1 branch
BranchTrueFalseTotal
if is_arrayref($v) 0 19 19
42 if is_arrayref $v;
43  
44 19 ▶ if ( my $ref = ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $v) 2 17 19
45 2 warn "Unsupported ref while encoding: $ref";
46 2 return {};
47 }
48  
49 # TODO: not strings
50 17 return { stringValue => "$v" };
51 3 }
52  
53 14 ▶ method encode_resource ( $resource ) {
Subroutine encode_resource: called CC=1 SCAR=0.0
Pod undocumented
54 {
55 14 attributes => $self->encode_kvlist($resource->attributes),
56 droppedAttributesCount => $resource->dropped_attributes,
57 };
58 3 }
59  
60 0 ▶ method encode_span_event ( $event ) {
Subroutine encode_span_event: not called CC=1 SCAR=6.9
Pod undocumented
61 {
62 0 attributes => $self->encode_kvlist($event->attributes),
63 droppedAttributesCount => $event->dropped_attributes,
64 name => $event->name,
65 timeUnixNano => int $event->timestamp * 1_000_000_000,
66 };
67 3 }
68  
69 0 ▶ method encode_span_link ( $link ) {
Subroutine encode_span_link: not called CC=1 SCAR=6.9
Pod undocumented
70 {
71 0 attributes => $self->encode_kvlist($link->attributes),
72 droppedAttributesCount => $link->dropped_attributes,
73 spanId => $link->context->hex_span_id,
74 traceId => $link->context->hex_trace_id,
75 };
76 3 }
77  
78 10 ▶ method encode_span_status ( $status ) {
Subroutine encode_span_status: called CC=1 SCAR=0.0
Pod undocumented
79 {
80 10 code => $status->code,
81 message => '' . $status->description,
82 };
83 3 }
84  
85 10 ▶ method encode_span ( $span ) {
Subroutine encode_span: called CC=2 SCAR=7.0
Pod undocumented
86 10 my $data = {
87 attributes => $self->encode_kvlist($span->attributes),
88 droppedAttributesCount => $span->dropped_attributes,
89 droppedEventsCount => $span->dropped_events,
90 droppedLinksCount => $span->dropped_links,
91 endTimeUnixNano => int $span->end_timestamp * 1_000_000_000,
92 events => [ map $self->encode_span_event($_), $span->events ],
93 kind => $span->kind,
94 links => [ map $self->encode_span_link($_), $span->links ],
95 name => $span->name,
96 spanId => $span->hex_span_id,
97 startTimeUnixNano => int $span->start_timestamp * 1_000_000_000,
98 status => $self->encode_span_status($span->status),
99 traceId => $span->hex_trace_id,
100 traceState => $span->trace_state->to_string,
101 };
102  
103 10 my $parent = $span->hex_parent_span_id;
104 10 ▶ $data->{parentSpanId} = $parent
Branch: 1 branch
BranchTrueFalseTotal
unless $parent eq "0000000000000000" 0 10 10
105 unless $parent eq HEX_INVALID_SPAN_ID;
106  
107 10 $data;
108 3 }
109  
110 8 ▶ method encode_log ( $log ) {
Subroutine encode_log: called CC=2 SCAR=6.9
Pod undocumented
111 8 my $data = {
112 attributes => $self->encode_kvlist($log->attributes),
113 body => $self->encode_anyvalue($log->body),
114 droppedAttributesCount => $log->dropped_attributes,
115 flags => $log->trace_flags->flags,
116 observedTimeUnixNano => int $log->observed_timestamp * 1_000_000_000,
117 severityNumber => $log->severity_number,
118 severityText => $log->severity_text,
119 spanId => $log->hex_span_id,
120 traceId => $log->hex_trace_id,
121 };
122  
123 8 my $t = $log->timestamp;
124 8 ▶ $data->{timeUnixNano} = int $t * 1_000_000_000 if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 2 6 8
125  
126 8 $data;
127 3 }
128  
129 18 ▶ method encode_scope ( $scope ) {
Subroutine encode_scope: called CC=1 SCAR=0.0
Pod undocumented
130 {
131 18 attributes => $self->encode_kvlist($scope->attributes),
132 droppedAttributesCount => $scope->dropped_attributes,
133 name => $scope->name,
134 version => $scope->version,
135 };
136 3 }
137  
138 10 ▶ method encode_scope_spans ( $scope, $spans ) {
Subroutine encode_scope_spans: called CC=1 SCAR=0.0
Pod undocumented
139 {
140 10 scope => $self->encode_scope($scope),
141 spans => [ map $self->encode_span($_), @$spans ],
142 };
143 3 }
144  
145 8 ▶ method encode_scope_logs ( $scope, $logs ) {
Subroutine encode_scope_logs: called CC=1 SCAR=0.0
Pod undocumented
146 {
147 8 scope => $self->encode_scope($scope),
148 logRecords => [ map $self->encode_log($_), @$logs ],
149 };
150 3 }
151  
152 8 ▶ method encode_resource_spans ( $resource, $spans ) {
Subroutine encode_resource_spans: called CC=3 SCAR=11.0
Pod undocumented
153 8 my %scopes;
154  
155 8 for (@$spans) {
156 10 my $key = refaddr $_->instrumentation_scope;
157  
158 10 ▶ $scopes{ $key } //= [ $_->instrumentation_scope, [] ];
Condition50%1/2
$scopes{$key} //= [$_->instrumentation_scope, []]
l!l
010
MC/DC0%0/1
$scopes{$key} //= [$_->instrumentation_scope, []]
$scopes{$key}
Truth table50%1/2
A
A= $scopes{$key}
Aresult
11
00
159 10 push @{ $scopes{ $key }[1] }, $_;
160 }
161  
162 {
163 8 resource => $self->encode_resource($resource),
164 scopeSpans => [ map $self->encode_scope_spans(@$_), values %scopes ],
165 schemaUrl => $resource->schema_url,
166 };
167 3 }
168  
169 6 ▶ method encode_resource_logs ( $resource, $logs ) {
Subroutine encode_resource_logs: called CC=3 SCAR=11.0
Pod undocumented
170 6 my %scopes;
171  
172 6 for (@$logs) {
173 8 my $key = refaddr $_->instrumentation_scope;
174  
175 8 ▶ $scopes{ $key } //= [ $_->instrumentation_scope, [] ];
Condition50%1/2
$scopes{$key} //= [$_->instrumentation_scope, []]
l!l
08
MC/DC0%0/1
$scopes{$key} //= [$_->instrumentation_scope, []]
$scopes{$key}
Truth table50%1/2
A
A= $scopes{$key}
Aresult
11
00
176 8 push @{ $scopes{ $key }[1] }, $_;
177 }
178  
179 {
180 6 resource => $self->encode_resource($resource),
181 scopeLogs => [ map $self->encode_scope_logs(@$_), values %scopes ],
182 schemaUrl => $resource->schema_url,
183 };
184 3 }
185  
186 6 ▶ method encode ( $data ) {
Subroutine encode: called CC=5 SCAR=16.1
Pod documented
187 6 my ( %request, %resources );
188  
189 6 my $type;
190 6 for (@$data) {
191 18 ▶ $type //= isa_OpenTelemetry_SDK_Logs_LogRecord($_)
Branch: 1 branch
BranchTrueFalseTotal
isa_OpenTelemetry_SDK_Logs_LogRecord($_) ? : 2 4 6
Condition100%2/2
$type //= isa_OpenTelemetry_SDK_Logs_LogRecord($_) ? "logs" : "spans"
l!l
126
MC/DC100%1/1
$type //= isa_OpenTelemetry_SDK_Logs_LogRecord($_) ? "logs" : "spans"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
192 ? 'logs'
193 : 'spans';
194  
195 18 my $key = refaddr $_->resource;
196 18 ▶ $resources{ $key } //= [ $_->resource, [] ];
Condition100%2/2
$resources{$key} //= [$_->resource, []]
l!l
414
MC/DC100%1/1
$resources{$key} //= [$_->resource, []]
$resources{$key}
Truth table100%2/2
A
A= $resources{$key}
Aresult
11
00
197 18 push @{ $resources{ $key }[1] }, $_;
198 }
199  
200 6 my $encode = "encode_resource_$type";
201 6 $self->serialise({
202 'resource' . ucfirst $type => [
203 map $self->$encode(@$_), values %resources,
204 ]
205 });
206 3 }
207 3 }
« Exporter/OTLP/Logs.pm ↑ Summary Exporter/OTLP.pm »