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/Tracer.pm ↑ Summary SDK/Trace/Span.pm »
1 3 ▶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 3 our $VERSION = '0.102';
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 3 ▶class OpenTelemetry::Processor::Batch :does(OpenTelemetry::Processor) {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶ use Feature::Compat::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use IO::Async::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use Mutex;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ use OpenTelemetry::Common qw( config timeout_timestamp maybe_timeout );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶ use OpenTelemetry::Constants -export;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶ use OpenTelemetry::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶ use OpenTelemetry;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 my $logger = OpenTelemetry::Common::internal_logger;
29  
30 3 ▶ use Metrics::Any '$metrics', strict => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 name_prefix => [qw( otel processor batch )];
32  
33 3 $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 3 field $exporter_timeout :param //= config('BSP_EXPORT_TIMEOUT') // 30_000;
40 3 field $max_queue_size :param //= config('BSP_MAX_QUEUE_SIZE') // 2_048;
41 3 field $schedule_delay :param //= config('BSP_SCHEDULE_DELAY') // 5_000;
42 3 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 3 field $function;
48 3 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 # deprecated, non-standard variable explicitly not allowed by the OTel spec
56 if ( ( $ENV{OTEL_PERL_BSP_MAX_WORKERS} // 1 ) > 1 ) {
57 $logger->warn('Running the batch processor with multiple workers (OTEL_PERL_BSP_MAX_WORKERS) is no longer supported because the OpenTelemetry spec forbids it');
58 }
59  
60 if ( $batch_size > $max_queue_size ) {
61 $logger->warn(
62 'Max export batch size cannot be greater than maximum queue size when instantiating batch processor',
63 {
64 batch_size => $batch_size,
65 queue_size => $max_queue_size,
66 },
67 );
68 $batch_size = $max_queue_size;
69 }
70  
71 $function = IO::Async::Function->new(
72 # The specification requires batch processors to synchronise
73 # calls to the exporter, so a single worker does all the
74 # exporting, one batch at a time.
75 max_workers => 1,
76  
77 code => sub ( $exporter, $batch, $timeout ) {
78 $exporter->export( $batch, $timeout );
79 },
80 );
81  
82 IO::Async::Loop->new->add($function);
83 }
84  
85 7 ▶ method process ( @items ) {
Subroutine process: called CC=2 SCAR=7.1
Pod documented
86 7 try {
87 my $batch = $lock->enter(
88 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
89 7 my $overflow = @queue + @items - $max_queue_size;
90 7 ▶ if ( $overflow > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow > 0) 0 7 7
91 # If the buffer is full, we drop old spans first
92 # The queue is always FIFO, even for dropped spans
93 # This behaviour is not in the spec, but is
94 # consistent with the Ruby implementation.
95 # For context, the Go implementation instead
96 # blocks until there is room in the buffer.
97 0 splice @queue, 0, $overflow;
98 0 $self->report_dropped( 'buffer-full', $overflow );
99 }
100  
101 7 push @queue, @items;
102  
103 7 ▶ return [] if @queue < $batch_size;
Branch: 1 branch
BranchTrueFalseTotal
if @queue < $batch_size 5 2 7
104  
105 2 ▶ $metrics->set_gauge_to(
Branch: 1 branch
BranchTrueFalseTotal
if @queue 2 0 2
106 buffer_use => @queue / $max_queue_size
107 ) if @queue;
108  
109 2 [ splice @queue, 0, $batch_size ];
110 }
111 7 );
112  
113 7 ▶ return unless @$batch;
Branch: 1 branch
BranchTrueFalseTotal
unless @$batch 5 2 7
114  
115 # Make sure we call this in void context so we don't have
116 # to wait for the future ourselves
117 $function->call(
118 args => [ $exporter, $batch, $exporter_timeout ],
119 2 ▶ on_result => sub ( $type, $result ) {
Subroutine __ANON__: called CC=2 SCAR=7.1
120 2 my $count = scalar @$batch;
121  
122 2 ▶ return $self->report_result( EXPORT_RESULT_FAILURE, $count )
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "return" 0 2 2
123 unless $type eq 'return';
124  
125 2 $self->report_result( $result, $count );
126 },
127 2 );
128  
129 2 return;
130 }
131 catch($e) {
132 0 warn $e;
133 }
134 3 }
135  
136 0 ▶ method report_dropped ( $reason, $count ) { $self }
Subroutine report_dropped: not called CC=1 SCAR=4.9
Pod documented
137  
138 1 ▶ method report_result ( $result, $count ) { $result }
Subroutine report_result: called CC=1 SCAR=0.0
Pod documented
139  
140 6 ▶ async method shutdown ( $timeout = undef ) {
Subroutine shutdown: called CC=5 SCAR=16.1
Pod documented
141 6 ▶ return EXPORT_RESULT_SUCCESS if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 5 6
142  
143 5 defer { $done = 1 }
144  
145 5 my $start = timeout_timestamp;
146  
147 # TODO: The Ruby implementation ignores whether the force_flush
148 # times out. Is this correct?
149 5 await $self->force_flush( maybe_timeout $timeout, $start );
150  
151 5 ▶ $self->report_dropped( 'terminating', scalar @queue ) if @queue;
Branch: 1 branch
BranchTrueFalseTotal
if @queue 0 5 5
152 5 @queue = ();
153  
154 5 ▶ await $function->stop if $function->workers;
Branch: 1 branch
BranchTrueFalseTotal
if $function->workers 5 0 5
155  
156 5 await $exporter->shutdown( maybe_timeout $timeout, $start );
157 3 }
158  
159 7 ▶ async method force_flush ( $timeout = undef ) {
Subroutine force_flush: called CC=8 SCAR=21.2
Pod documented
160 7 ▶ return EXPORT_RESULT_SUCCESS if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 6 7
161  
162 6 my $start = timeout_timestamp;
163  
164 6 ▶ my @stack = $lock->enter( sub { splice @queue, 0, @queue } );
Subroutine __ANON__: called CC=1 SCAR=0.0
165  
166 6 defer {
167 # If we still have any spans left it has to be because we
168 # timed out and couldn't export them. In that case, we drop
169 # them and report
170 6 ▶ $self->report_dropped( 'force-flush', scalar @stack ) if @stack;
Branch: 1 branch
BranchTrueFalseTotal
if @stack 0 6 6
171 }
172  
173 6 ▶ while ( @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 1 6 7
174 1 my $remaining = maybe_timeout $timeout, $start;
175 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
176  
177 1 my $batch = [ splice @stack, 0, $batch_size ];
178  
179 1 my $count = scalar @$batch;
180 1 try {
181 1 my $result = await $function->call(
182 args => [ $exporter, $batch, $remaining ],
183 );
184  
185 1 $self->report_result( $result, $count );
186  
187 1 ▶ return $result unless $result == EXPORT_RESULT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $result == 0 0 1 1
188 }
189 catch ($e) {
190 0 return $self->report_result( EXPORT_RESULT_FAILURE, $count);
191 }
192 }
193  
194 6 await $exporter->force_flush( maybe_timeout $timeout, $start );
195 3 }
196  
197 10 ▶ method DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
198 10 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 10 10
199 10 ▶ $function->stop->get if $function;
Branch: 1 branch
BranchTrueFalseTotal
if $function 8 2 10
200 3 }
201 3 }
« SDK/Trace/Tracer.pm ↑ Summary SDK/Trace/Span.pm »