Exporter/OTLP/Encoder/JSON.pm

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