Processor/Batch.pm

filter: Statementstmt 92.4% 122 / 132 Branchbran 70.0% 21 / 30 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 95.6% 22 / 23 Podpod 83.3% 5 / 6 totaltotal 87.2% 171 / 196 CC 19 SCAR 12.6
CC
19
Cov
87%
force_flush
8.3
shutdown
5.0
__ANON__
4.4
CRAP 3.5
« SDK/Trace/TracerProvider.pm ↑ Summary SDK/Trace/Span.pm »
1 4 ▶use Object::Pad ':experimental(init_expr)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2 # ABSTRACT: A batched OpenTelemetry processor
3  
4 package OpenTelemetry::Processor::Batch;
5  
6 4 our $VERSION = '0.101';
7  
8 # TODO: If we move the different base implementations of processors
9 # to the top-level, that still leaves the specific implementations
10 # for Traces / Metrics / Logs to exist somewhere else.
11 # This works, although we need to be careful about clarifying what
12 # the expectations are for classes in each namespace, and the what
13 # are the interfaces they expose.
14 # In all honesty, this separation ended up looking a lot better than
15 # expected.
16 4 ▶class OpenTelemetry::Processor::Batch :does(OpenTelemetry::Processor) {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶ use Feature::Compat::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶ use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶ use IO::Async::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶ use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ use Mutex;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶ use OpenTelemetry::Common qw( config timeout_timestamp maybe_timeout );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ use OpenTelemetry::Constants -export;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶ use OpenTelemetry::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶ use OpenTelemetry;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 my $logger = OpenTelemetry::Common::internal_logger;
29  
30 4 ▶ use Metrics::Any '$metrics', strict => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 name_prefix => [qw( otel processor batch )];
32  
33 4 $metrics->make_gauge( 'buffer_use',
34 name => [qw( buffer use )],
35 description => 'Ratio between maximum queue size and the size of the queue at export time',
36 );
37  
38 field $batch_size :param //= config('BSP_MAX_EXPORT_BATCH_SIZE') // 512;
39 4 field $exporter_timeout :param //= config('BSP_EXPORT_TIMEOUT') // 30_000;
40 4 field $max_queue_size :param //= config('BSP_MAX_QUEUE_SIZE') // 2_048;
41 4 field $schedule_delay :param //= config('BSP_SCHEDULE_DELAY') // 5_000;
42 4 field $exporter :param;
43  
44 field $lock = Mutex->new;
45  
46 6 ▶ field $done :reader;
Subroutine done: called CC=1 SCAR=0.0
Pod undocumented
47 4 field $function;
48 4 field @queue;
49  
50 ADJUST {
51 die OpenTelemetry::X->create(
52 Invalid => "Exporter must implement the OpenTelemetry::Exporter interface: " . ( ref $exporter || $exporter )
53 ) unless $exporter && $exporter->DOES('OpenTelemetry::Exporter');
54  
55 if ( $batch_size > $max_queue_size ) {
56 $logger->warn(
57 'Max export batch size cannot be greater than maximum queue size when instantiating batch processor',
58 {
59 batch_size => $batch_size,
60 queue_size => $max_queue_size,
61 },
62 );
63 $batch_size = $max_queue_size;
64 }
65  
66 # This is a non-standard variable, so we make it Perl-specific
67 my $max_workers = $ENV{OTEL_PERL_BSP_MAX_WORKERS};
68  
69 $function = IO::Async::Function->new(
70 $max_workers ? ( max_workers => $max_workers ) : (),
71  
72 code => sub ( $exporter, $batch, $timeout ) {
73 $exporter->export( $batch, $timeout );
74 },
75 );
76  
77 IO::Async::Loop->new->add($function);
78 }
79  
80 7 ▶ method process ( @items ) {
Subroutine process: called CC=2 SCAR=7.1
Pod documented
81 7 try {
82 my $batch = $lock->enter(
83 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
84 7 my $overflow = @queue + @items - $max_queue_size;
85 7 ▶ if ( $overflow > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow > 0) 0 7 7
86 # If the buffer is full, we drop old spans first
87 # The queue is always FIFO, even for dropped spans
88 # This behaviour is not in the spec, but is
89 # consistent with the Ruby implementation.
90 # For context, the Go implementation instead
91 # blocks until there is room in the buffer.
92 0 splice @queue, 0, $overflow;
93 0 $self->report_dropped( 'buffer-full', $overflow );
94 }
95  
96 7 push @queue, @items;
97  
98 7 ▶ return [] if @queue < $batch_size;
Branch: 1 branch
BranchTrueFalseTotal
if @queue < $batch_size 5 2 7
99  
100 2 ▶ $metrics->set_gauge_to(
Branch: 1 branch
BranchTrueFalseTotal
if @queue 2 0 2
101 buffer_use => @queue / $max_queue_size
102 ) if @queue;
103  
104 2 [ splice @queue, 0, $batch_size ];
105 }
106 7 );
107  
108 7 ▶ return unless @$batch;
Branch: 1 branch
BranchTrueFalseTotal
unless @$batch 5 2 7
109  
110 # Make sure we call this in void context so we don't have
111 # to wait for the future ourselves
112 $function->call(
113 args => [ $exporter, $batch, $exporter_timeout ],
114 2 ▶ on_result => sub ( $type, $result ) {
Subroutine __ANON__: called CC=2 SCAR=7.1
115 2 my $count = scalar @$batch;
116  
117 2 ▶ return $self->report_result( EXPORT_RESULT_FAILURE, $count )
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "return" 0 2 2
118 unless $type eq 'return';
119  
120 2 $self->report_result( $result, $count );
121 },
122 2 );
123  
124 2 return;
125 }
126 catch($e) {
127 0 warn $e;
128 }
129 4 }
130  
131 0 ▶ method report_dropped ( $reason, $count ) { $self }
Subroutine report_dropped: not called CC=1 SCAR=4.9
Pod documented
132  
133 1 ▶ method report_result ( $result, $count ) { $result }
Subroutine report_result: called CC=1 SCAR=0.0
Pod documented
134  
135 6 ▶ async method shutdown ( $timeout = undef ) {
Subroutine shutdown: called CC=5 SCAR=16.1
Pod documented
136 6 ▶ return EXPORT_RESULT_SUCCESS if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 5 6
137  
138 5 defer { $done = 1 }
139  
140 5 my $start = timeout_timestamp;
141  
142 # TODO: The Ruby implementation ignores whether the force_flush
143 # times out. Is this correct?
144 5 await $self->force_flush( maybe_timeout $timeout, $start );
145  
146 5 ▶ $self->report_dropped( 'terminating', scalar @queue ) if @queue;
Branch: 1 branch
BranchTrueFalseTotal
if @queue 0 5 5
147 5 @queue = ();
148  
149 5 ▶ await $function->stop if $function->workers;
Branch: 1 branch
BranchTrueFalseTotal
if $function->workers 5 0 5
150  
151 5 await $exporter->shutdown( maybe_timeout $timeout, $start );
152 4 }
153  
154 7 ▶ async method force_flush ( $timeout = undef ) {
Subroutine force_flush: called CC=8 SCAR=21.2
Pod documented
155 7 ▶ return EXPORT_RESULT_SUCCESS if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 6 7
156  
157 6 my $start = timeout_timestamp;
158  
159 6 ▶ my @stack = $lock->enter( sub { splice @queue, 0, @queue } );
Subroutine __ANON__: called CC=1 SCAR=0.0
160  
161 6 defer {
162 # If we still have any spans left it has to be because we
163 # timed out and couldn't export them. In that case, we drop
164 # them and report
165 6 ▶ $self->report_dropped( 'force-flush', scalar @stack ) if @stack;
Branch: 1 branch
BranchTrueFalseTotal
if @stack 0 6 6
166 }
167  
168 6 ▶ while ( @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 1 6 7
169 1 my $remaining = maybe_timeout $timeout, $start;
170 1 ▶ return EXPORT_RESULT_TIMEOUT if $timeout and !$remaining;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $remaining 0 1 1
Condition33%1/3
$timeout and not $remaining
!ll&&!rl&&r
100
MC/DC0%0/2
$timeout and not $remaining
$timeout not $remaining
Truth table33%1/3
A and not(B)
A= $timeoutB= not $remaining
ABresult
0X0
100
111
171  
172 1 my $batch = [ splice @stack, 0, $batch_size ];
173  
174 1 my $count = scalar @$batch;
175 1 try {
176 1 my $result = await $function->call(
177 args => [ $exporter, $batch, $remaining ],
178 );
179  
180 1 $self->report_result( $result, $count );
181  
182 1 ▶ return $result unless $result == EXPORT_RESULT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $result == 0 0 1 1
183 }
184 catch ($e) {
185 0 return $self->report_result( EXPORT_RESULT_FAILURE, $count);
186 }
187 }
188  
189 6 await $exporter->force_flush( maybe_timeout $timeout, $start );
190 4 }
191  
192 16 ▶ method DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
193 16 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 16 16
194 16 ▶ $function->stop->get if $function;
Branch: 1 branch
BranchTrueFalseTotal
if $function 14 2 16
195 4 }
196 4 }
« SDK/Trace/TracerProvider.pm ↑ Summary SDK/Trace/Span.pm »