blib/lib/Langertha/Runtime/Metrics/OTLP.pm

filter: Statementstmt 97.1% 103 / 106 Branchbran 83.3% 30 / 36 Conditioncond 61.4% 35 / 57 MC/DCmcdc 33.3% 11 / 33 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 78.5% 194 / 247 CC 48 SCAR 23.3
CC
48
Cov
84%
_build_metrics
18.1
build_payload
9.7
_summary_metric
9.5
CRAP 10.3
« blib/lib/Langertha/Reasoning/Profile.pm ↑ Summary blib/lib/Langertha/Engine/AKI.pm »
1 package Langertha::Runtime::Metrics::OTLP;
2 # ABSTRACT: Convert parsed Prometheus records to an OTLP/HTTP (JSON) metrics payload
3 4 our $VERSION = '0.505';
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use JSON::MaybeXS qw( JSON );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 has _json => (
11 is => 'ro',
12 lazy => 1,
13 7 ▶ default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 );
15  
16 12 ▶sub build_payload {
Subroutine build_payload: called CC=9 SCAR=22.7
Pod documented
17 12 my ( $self, $records, %opts ) = @_;
18 12 ▶ croak "build_payload() requires an ArrayRef of records"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $records eq "ARRAY" 1 11 12
19 unless ref($records) eq 'ARRAY';
20  
21 11 ▶ my $timestamp = $opts{timestamp} // time();
Condition66%2/3
$opts{"timestamp"} // &Langertha::Runtime::Metrics::OTLP::time()
l!l&&r!l&&!r
380
MC/DC0%0/2
$opts{"timestamp"} // &Langertha::Runtime::Metrics::OTLP::time()
$opts{"timestamp"} &Langertha::Runtime::Metrics::OTLP::time()
Truth table66%2/3
A // B
A= $opts{"timestamp"}B= &Langertha::Runtime::Metrics::OTLP::time()
ABresult
1X1
011
000
22 11 my $time_nano = sprintf( "%.0f", $timestamp * 1_000_000_000 );
23  
24 11 ▶ my %resource_attrs = %{ $opts{resource_attributes} || {} };
Condition50%1/2
$opts{'resource_attributes'} || {}
l!l
011
MC/DC0%0/1
$opts{'resource_attributes'} || {}
$opts{'resource_attributes'}
Truth table50%1/2
A
A= $opts{'resource_attributes'}
Aresult
11
00
25 $resource_attrs{'service.name'} //= $opts{service_name}
26 11 ▶ if defined $opts{service_name} && length $opts{service_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"service_name"} and length $opts{"service_name"} 4 7 11
Condition50%3/6
defined $opts{"service_name"} and length $opts{"service_name"}
!ll&&!rl&&r
704
$resource_attrs{"service.name"} //= $opts{"service_name"}
l!l&&r!l&&!r
040
MC/DC50%1/2
defined $opts{"service_name"} and length $opts{"service_name"}
defined $opts{"service_name"} length $opts{"service_name"}
MC/DC0%0/2
$resource_attrs{"service.name"} //= $opts{"service_name"}
$resource_attrs{"service.name"} $opts{"service_name"}
Truth table66%2/3
A and B
A= defined $opts{"service_name"}B= length $opts{"service_name"}
ABresult
0X0
100
111
Truth table33%1/3
A //= B
A= $resource_attrs{"service.name"}B= $opts{"service_name"}
ABresult
1X1
011
000
27  
28 11 ▶ my $scope_name = $opts{scope_name} // 'langertha';
Condition50%1/2
$opts{"scope_name"} // "langertha"
l!l
011
MC/DC0%0/1
$opts{"scope_name"} // "langertha"
$opts{"scope_name"}
Truth table50%1/2
A
A= $opts{"scope_name"}
Aresult
11
00
29  
30 11 my @resource_attributes;
31 11 for my $key ( sort keys %resource_attrs ) {
32 4 push @resource_attributes, $self->_attr($key, $resource_attrs{$key});
33 }
34  
35 return {
36 11 resourceMetrics => [
37 {
38 resource => {
39 attributes => \@resource_attributes,
40 },
41 scopeMetrics => [
42 {
43 scope => { name => $scope_name },
44 metrics => [ $self->_build_metrics($records, $time_nano) ],
45 },
46 ],
47 },
48 ],
49 };
50 }
51  
52  
53 7 ▶sub to_json {
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
54 7 my ( $self, $records, %opts ) = @_;
55 7 return $self->_json->encode( $self->build_payload($records, %opts) );
56 }
57  
58  
59 # Group records into OTLP metrics. Histogram and summary series are
60 # re-grouped by base name; everything else maps 1:1 to a gauge or sum.
61 11 ▶sub _build_metrics {
Subroutine _build_metrics: called CC=18 SCAR=28.9
62 11 my ( $self, $records, $time_nano ) = @_;
63  
64 11 my %histograms; # base => { buckets => [...], sum => $r, count => $r }
65 my %summaries; # base => { sum => $r, count => $r, quantiles => [...] }
66 11 my @standalone;
67  
68 11 for my $r ( @$records ) {
69 19 my $name = $r->{name};
70 19 ▶ my $type = $r->{type} // 'untyped';
Condition50%1/2
$$r{"type"} // "untyped"
l!l
190
MC/DC0%0/1
$$r{"type"} // "untyped"
$$r{"type"}
Truth table50%1/2
A
A= $$r{"type"}
Aresult
11
00
71  
72 19 ▶ if ( $type eq 'histogram' && $name =~ /^(.*)_bucket$/ ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'histogram' and $name =~ /^(.*)_bucket$/) { } 3 16 19
elsif ($type eq 'histogram' and $name =~ /^(.*)_sum$/) { } 1 15 16
elsif ($type eq 'histogram' and $name =~ /^(.*)_count$/) { } 1 14 15
elsif ($type eq 'summary' and $name =~ /^(.*)_sum$/) { } 1 13 14
elsif ($type eq 'summary' and $name =~ /^(.*)_count$/) { } 1 12 13
elsif ($type eq 'summary' and exists $$r{'labels'}{'quantile'}) { } 2 10 12
Condition100%3/3
$type eq "histogram" and $name =~ /^(.*)_bucket$/
!ll&&!rl&&r
1423
MC/DC100%2/2
$type eq "histogram" and $name =~ /^(.*)_bucket$/
$type eq "histogram" $name =~ /^(.*)_bucket$/
Truth table100%3/3
A and B
A= $type eq "histogram"B= $name =~ /^(.*)_bucket$/
ABresult
0X0
100
111
73 3 push @{ $histograms{$1}{buckets} }, $r;
74 }
75 ▶ elsif ( $type eq 'histogram' && $name =~ /^(.*)_sum$/ ) {
Condition100%3/3
$type eq "histogram" and $name =~ /^(.*)_sum$/
!ll&&!rl&&r
1411
MC/DC100%2/2
$type eq "histogram" and $name =~ /^(.*)_sum$/
$type eq "histogram" $name =~ /^(.*)_sum$/
Truth table100%3/3
A and B
A= $type eq "histogram"B= $name =~ /^(.*)_sum$/
ABresult
0X0
100
111
76 1 $histograms{$1}{sum} = $r;
77 }
78 ▶ elsif ( $type eq 'histogram' && $name =~ /^(.*)_count$/ ) {
Condition66%2/3
$type eq "histogram" and $name =~ /^(.*)_count$/
!ll&&!rl&&r
1401
MC/DC50%1/2
$type eq "histogram" and $name =~ /^(.*)_count$/
$type eq "histogram" $name =~ /^(.*)_count$/
Truth table66%2/3
A and B
A= $type eq "histogram"B= $name =~ /^(.*)_count$/
ABresult
0X0
100
111
79 1 $histograms{$1}{count} = $r;
80 }
81 ▶ elsif ( $type eq 'summary' && $name =~ /^(.*)_sum$/ ) {
Condition100%3/3
$type eq "summary" and $name =~ /^(.*)_sum$/
!ll&&!rl&&r
1031
MC/DC100%2/2
$type eq "summary" and $name =~ /^(.*)_sum$/
$type eq "summary" $name =~ /^(.*)_sum$/
Truth table100%3/3
A and B
A= $type eq "summary"B= $name =~ /^(.*)_sum$/
ABresult
0X0
100
111
82 1 $summaries{$1}{sum} = $r;
83 }
84 ▶ elsif ( $type eq 'summary' && $name =~ /^(.*)_count$/ ) {
Condition100%3/3
$type eq "summary" and $name =~ /^(.*)_count$/
!ll&&!rl&&r
1021
MC/DC100%2/2
$type eq "summary" and $name =~ /^(.*)_count$/
$type eq "summary" $name =~ /^(.*)_count$/
Truth table100%3/3
A and B
A= $type eq "summary"B= $name =~ /^(.*)_count$/
ABresult
0X0
100
111
85 1 $summaries{$1}{count} = $r;
86 }
87 ▶ elsif ( $type eq 'summary' && exists $r->{labels}{quantile} ) {
Condition66%2/3
$type eq "summary" and exists $$r{"labels"}{"quantile"}
!ll&&!rl&&r
1002
MC/DC50%1/2
$type eq "summary" and exists $$r{"labels"}{"quantile"}
$type eq "summary" exists $$r{"labels"}{"quantile"}
Truth table66%2/3
A and B
A= $type eq "summary"B= exists $$r{"labels"}{"quantile"}
ABresult
0X0
100
111
88 2 push @{ $summaries{$name}{quantiles} }, $r;
89 }
90 else {
91 10 push @standalone, $r;
92 }
93 }
94  
95 11 my @metrics;
96 11 push @metrics, $self->_standalone_metric($_, $time_nano) for @standalone;
97 push @metrics, $self->_histogram_metric($_, $histograms{$_}, $time_nano)
98 11 for sort keys %histograms;
99 push @metrics, $self->_summary_metric($_, $summaries{$_}, $time_nano)
100 11 for sort keys %summaries;
101 11 return @metrics;
102 }
103  
104 10 ▶sub _standalone_metric {
Subroutine _standalone_metric: called CC=4 SCAR=13.9
105 10 my ( $self, $r, $time_nano ) = @_;
106 my $dp = {
107 timeUnixNano => $time_nano,
108 10 asDouble => $r->{value} + 0,
109 };
110 10 my $attrs = $self->_labels_to_attrs($r->{labels});
111 10 ▶ $dp->{attributes} = $attrs if @$attrs;
Branch: 1 branch
BranchTrueFalseTotal
if @$attrs 6 4 10
112  
113 10 ▶ my $type = $r->{type} // 'untyped';
Condition50%1/2
$$r{"type"} // "untyped"
l!l
100
MC/DC0%0/1
$$r{"type"} // "untyped"
$$r{"type"}
Truth table50%1/2
A
A= $$r{"type"}
Aresult
11
00
114 10 ▶ if ( $type eq 'counter' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "counter") 2 8 10
115 return {
116 name => $r->{name},
117 2 sum => {
118 dataPoints => [ $dp ],
119 aggregationTemporality => 'AGGREGATION_TEMPORALITY_CUMULATIVE',
120 isMonotonic => JSON->true,
121 },
122 };
123 }
124 # gauge, untyped, and anything unrecognized → gauge
125 8 return { name => $r->{name}, gauge => { dataPoints => [ $dp ] } };
126 }
127  
128 1 ▶sub _histogram_metric {
Subroutine _histogram_metric: called CC=8 SCAR=21.0
129 1 my ( $self, $base, $group, $time_nano ) = @_;
130  
131 1 ▶ my @buckets = @{ $group->{buckets} || [] };
Condition50%1/2
$$group{'buckets'} || []
l!l
10
MC/DC0%0/1
$$group{'buckets'} || []
$$group{'buckets'}
Truth table50%1/2
A
A= $$group{'buckets'}
Aresult
11
00
132 # Sort by the le bound numerically; +Inf sorts last.
133 @buckets = sort {
134 1 my ( $av, $bv ) = ( $a->{labels}{le}, $b->{labels}{le} );
135 3 ▶ return 1 if $av eq '+Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $av eq "+Inf" 1 2 3
136 2 ▶ return -1 if $bv eq '+Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $bv eq "+Inf" 1 1 2
137 1 ( $av + 0 ) <=> ( $bv + 0 );
138 } @buckets;
139  
140 # explicitBounds is a repeated double — numeric, not string.
141 1 my @bounds = map { $_->{labels}{le} + 0 } grep { $_->{labels}{le} ne '+Inf' } @buckets;
142 1 my @counts = map { sprintf( "%.0f", $_->{value} ) } @buckets;
143  
144 my $dp = {
145 timeUnixNano => $time_nano,
146 count => sprintf( "%.0f", $group->{count}{value} // 0 ),
147 1 ▶ sum => $group->{sum}{value} // 0,
Condition50%2/4
$$group{"count"}{"value"} // 0
l!l
10
$$group{"sum"}{"value"} // 0
l!l
10
MC/DC0%0/1
$$group{"count"}{"value"} // 0
$$group{"count"}{"value"}
MC/DC0%0/1
$$group{"sum"}{"value"} // 0
$$group{"sum"}{"value"}
Truth table50%1/2
A
A= $$group{"count"}{"value"}
Aresult
11
00
Truth table50%1/2
A
A= $$group{"sum"}{"value"}
Aresult
11
00
148 bucketCounts => \@counts,
149 explicitBounds => \@bounds,
150 };
151 # Data-point attributes: the label set minus the le bound (usually empty).
152 1 ▶ my %labels = %{ $group->{buckets}[0]{labels} || {} };
Condition50%1/2
$$group{'buckets'}[0]{'labels'} || {}
l!l
10
MC/DC0%0/1
$$group{'buckets'}[0]{'labels'} || {}
$$group{'buckets'}[0]{'labels'}
Truth table50%1/2
A
A= $$group{'buckets'}[0]{'labels'}
Aresult
11
00
153 1 delete $labels{le};
154 1 my $attrs = $self->_labels_to_attrs(\%labels);
155 1 ▶ $dp->{attributes} = $attrs if @$attrs;
Branch: 1 branch
BranchTrueFalseTotal
if @$attrs 0 1 1
156  
157 return {
158 1 name => $base,
159 histogram => {
160 dataPoints => [ $dp ],
161 aggregationTemporality => 'AGGREGATION_TEMPORALITY_CUMULATIVE',
162 },
163 };
164 }
165  
166 1 ▶sub _summary_metric {
Subroutine _summary_metric: called CC=8 SCAR=22.5
167 1 my ( $self, $base, $group, $time_nano ) = @_;
168  
169 my $dp = {
170 timeUnixNano => $time_nano,
171 count => sprintf( "%.0f", $group->{count}{value} // 0 ),
172 1 ▶ sum => $group->{sum}{value} // 0,
Condition50%2/4
$$group{"count"}{"value"} // 0
l!l
10
$$group{"sum"}{"value"} // 0
l!l
10
MC/DC0%0/1
$$group{"count"}{"value"} // 0
$$group{"count"}{"value"}
MC/DC0%0/1
$$group{"sum"}{"value"} // 0
$$group{"sum"}{"value"}
Truth table50%1/2
A
A= $$group{"count"}{"value"}
Aresult
11
00
Truth table50%1/2
A
A= $$group{"sum"}{"value"}
Aresult
11
00
173 };
174 1 my @quantiles = sort { $a->{quantile} <=> $b->{quantile} }
175 2 map { { quantile => $_->{labels}{quantile} + 0, value => $_->{value} } }
176 1 ▶ @{ $group->{quantiles} || [] };
Condition50%1/2
$$group{'quantiles'} || []
l!l
10
MC/DC0%0/1
$$group{'quantiles'} || []
$$group{'quantiles'}
Truth table50%1/2
A
A= $$group{'quantiles'}
Aresult
11
00
177 1 ▶ $dp->{quantileValues} = \@quantiles if @quantiles;
Branch: 1 branch
BranchTrueFalseTotal
if @quantiles 1 0 1
178  
179 1 ▶ my %labels = %{ $group->{sum}{labels} || $group->{count}{labels} || {} };
Condition40%2/5
$$group{'sum'}{'labels'} || $$group{'count'}{'labels'} || {}
l!l
10
$$group{"sum"}{"labels"} or $$group{"count"}{"labels"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$$group{'sum'}{'labels'} || $$group{'count'}{'labels'} || {}
$$group{"sum"}{"labels"} $$group{"count"}{"labels"}
Truth table33%1/3
A or B
A= $$group{"sum"}{"labels"}B= $$group{"count"}{"labels"}
ABresult
1X1
011
000
180 1 delete $labels{quantile};
181 1 my $attrs = $self->_labels_to_attrs(\%labels);
182 1 ▶ $dp->{attributes} = $attrs if @$attrs;
Branch: 1 branch
BranchTrueFalseTotal
if @$attrs 0 1 1
183  
184 return {
185 1 name => $base,
186 summary => { dataPoints => [ $dp ] },
187 };
188 }
189  
190 # Prometheus label set → OTLP attribute array. Prometheus label values are
191 # always strings, so every value is a stringValue.
192 12 ▶sub _labels_to_attrs {
Subroutine _labels_to_attrs: called CC=2 SCAR=6.9
193 12 my ( $self, $labels ) = @_;
194 12 my @attrs;
195 12 for my $key ( sort keys %$labels ) {
196 6 push @attrs, { key => $key, value => { stringValue => $labels->{$key} } };
197 }
198 12 return \@attrs;
199 }
200  
201 # Resource attribute value → OTLP AnyValue. Strings, numbers, and booleans
202 # are supported; anything else is stringified.
203 4 ▶sub _attr {
Subroutine _attr: called CC=5 SCAR=20.3
204 4 my ( $self, $key, $value ) = @_;
205 4 ▶ if ( ref $value eq 'JSON::PP::Boolean' || ref $value eq 'JSON::XS::Boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "JSON::PP::Boolean" or ref $value eq "JSON::XS::Boolean") 0 4 4
Condition33%1/3
ref $value eq "JSON::PP::Boolean" or ref $value eq "JSON::XS::Boolean"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $value eq "JSON::PP::Boolean" or ref $value eq "JSON::XS::Boolean"
ref $value eq "JSON::PP::Boolean" ref $value eq "JSON::XS::Boolean"
Truth table33%1/3
A or B
A= ref $value eq "JSON::PP::Boolean"B= ref $value eq "JSON::XS::Boolean"
ABresult
1X1
011
000
206 0 return { key => $key, value => { boolValue => $value } };
207 }
208 4 ▶ if ( !ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 4 0 4
209 4 ▶ if ( $value =~ /^-?\d+(?:\.\d+)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?\d+(?:\.\d+)?$/) 0 4 4
210 0 return { key => $key, value => { doubleValue => $value + 0 } };
211 }
212 4 return { key => $key, value => { stringValue => "$value" } };
213 }
214 0 return { key => $key, value => { stringValue => "$value" } };
215 }
216  
217 4 __PACKAGE__->meta->make_immutable;
218  
219  
220 4 1;
221  
222 __END__
« blib/lib/Langertha/Reasoning/Profile.pm ↑ Summary blib/lib/Langertha/Engine/AKI.pm »