DataDog/Client.pm

filter: Statementstmt 98.4% 125 / 127 Branchbran 88.4% 23 / 26 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 97.0% 33 / 34 Podpod 66.6% 2 / 3 totaltotal 95.5% 192 / 201 CC 17 SCAR 8.2
CC
17
Cov
96%
send_span
6.0
_http_post_struct_as_json
4.0
to_struct
3.0
CRAP 2.3
« DataDog/Tracer.pm ↑ Summary DataDog/HTTPPropagator.pm »
1 package OpenTracing::Implementation::DataDog::Client;
2  
3 =head1 NAME
4  
5 OpenTracing::Implementation::DataDog::Client - A Client that sends off the spans
6  
7 =head1 SYNOPSIS
8  
9 use alias OpenTracing::Implementation::DataDog::Client;
10
11 my $datadog_client = ->new(
12 http_user_agent => LWP::UserAgent->new();
13 host => 'localhost',
14 port => '8126',
15 path => 'v0.3/traces',
16 ); # these are defaults
17  
18 and later:
19  
20 $datadog_client->send_span( $span );
21  
22 =cut
23  
24  
25  
26 =head1 DESCRIPTION
27  
28 The main responsabillity of this C<Client> is to provide the C<send_span>
29 method, that will send the data to the local running DataDog agent.
30  
31 It does this by calling L<to_struct> that massages the generic OpenTracing data,
32 like C<baggage_items> from L<SpanContext> and C<tags> from C<Span>, together
33 with the DataDog specific data like C<resource_name>.
34  
35 This structure will be send of as a JSON string to the local installed DataDog
36 agent.
37  
38 =cut
39  
40  
41  
42 14 our $VERSION = 'v0.47.2';
43  
44 14 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 14 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 14 ▶use Sub::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 # XXX Order matters: Sub::HandlesVia::Manual::WithMoo - Potential load order
49 14 ▶use MooX::Attribute::ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 14 ▶use MooX::ProtectedAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 14 ▶use MooX::Should;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 14 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 14 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 14 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 14 ▶use PerlX::Maybe qw/maybe provided/;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 14 ▶use Regexp::Common qw/URI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 14 ▶use Types::Standard qw/ArrayRef Bool Enum HasMethods Maybe Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 14 ▶use Types::Common::Numeric qw/IntRange/;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 14 ▶use OpenTracing::Implementation::DataDog::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
63 nano_seconds
64 14 );
65  
66 14 ▶use constant MAX_SPANS => 20_000; # this is just an arbitrary, hardcoded number
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68  
69  
70 =head1 OPTIONAL ATTRIBUTES
71  
72 The attributes below can be set during instantiation, but none are required and
73 have sensible defaults, that may actually play nice with known DataDog
74 environment variables
75  
76 =cut
77  
78  
79  
80 =head2 C<http_user_agent>
81  
82 A HTTP User Agent that connects to the locally running DataDog agent. This will
83 default to a L<LWP::UserAgent>, but any User Agent will suffice, as long as it
84 has a required delegate method C<request>, that takes a L<HTTP::Request> object
85 and returns a L<HTTP::Response> compliant response object.
86  
87 =cut
88  
89 14 has http_user_agent => (
90 is => 'lazy',
91 should => HasMethods[qw/request/],
92 handles => { _send_http_request => 'request' },
93 );
94  
95 0 ▶sub _build_http_user_agent {
Subroutine _build_http_user_agent: not called CC=1 SCAR=6.9
96 0 return LWP::UserAgent->new( )
97 }
98  
99  
100  
101 =head2 C<scheme>
102  
103 The scheme being used, should be either C<http> or C<https>,
104 defaults to C<http>
105  
106 =cut
107  
108 14 has scheme => (
109 is => 'ro',
110 should => Enum[qw/http https/],
111 default => 'http',
112 );
113  
114  
115  
116 =head2 C<host>
117  
118 The host-name where the DataDog agent is running, which defaults to
119 C<localhost> or the value of C<DD_AGENT_HOST> environment variable if set.
120  
121 =cut
122  
123 14 has host => (
124 is => 'ro',
125 env_key => 'DD_AGENT_HOST',
126 default => 'localhost',
127 );
128  
129  
130  
131 =head2 C<port>
132  
133 The port-number the DataDog agent is listening at, which defaults to C<8126> or
134 the value of the C<DD_TRACE_AGENT_PORT> environment variable if set.
135  
136 =cut
137  
138 14 has port => (
139 is => 'ro',
140 env_key => 'DD_TRACE_AGENT_PORT',
141 default => '8126',
142 );
143  
144  
145  
146 =head2 C<path>
147  
148 The path the DataDog agent is expecting requests to come in, which defaults to
149 C<v0.3/traces>.
150  
151 =cut
152  
153 14 has path => (
154 is => 'ro',
155 default => 'v0.3/traces',
156 );
157 #
158 # maybe a 'version number' would be a better option ?
159  
160  
161  
162 =head2 C<agent_url>
163  
164 The complete URL the DataDog agent is listening at, and defaults to the value of
165 the C<DD_TRACE_AGENT_URL> environment variable if set. If this is set, it takes
166 precedence over any of the other settings.
167  
168 =cut
169  
170 has agent_url => (
171 is => 'ro',
172 env_key => 'DD_TRACE_AGENT_URL',
173 4 ▶ should => Maybe[Str->where( sub { _is_uri($_) } )],
Subroutine __ANON__: called CC=1 SCAR=0.0
174 );
175  
176 =pod
177  
178 NOTE: DataDog Agents can also listen to a UNiX socket, and one is suggested that
179 there is a C<unix:> URL. Fist of all, that is false, the C<unix:> scheme is just
180 non existent. It should be C<file:> instead. Secondly, this L<Client> just does
181 not support it, only C<http:> or C<https:>
182  
183 =cut
184  
185  
186  
187 14 has uri => (
188 is => 'lazy',
189 init_arg => undef,
190 );
191  
192 9 ▶sub _build_uri {
Subroutine _build_uri: called CC=2 SCAR=6.9
193 9 my $self = shift;
194
195 return
196 9 ▶ $self->agent_url
Condition100%2/2
$self->agent_url // qq[$self->{"scheme"}://$self->{"host"}:$self->{"port"}/$self->{"path"}]
l!l
45
MC/DC100%1/1
$self->agent_url // qq[$self->{"scheme"}://$self->{"host"}:$self->{"port"}/$self->{"path"}]
$self->agent_url
Truth table100%2/2
A
A= $self->agent_url
Aresult
11
00
197 //
198 "$self->{ scheme }://$self->{ host }:$self->{ port }/$self->{ path }"
199 }
200 #
201 # URI::Template is a nicer solution for this and more dynamic
202  
203  
204  
205 14 protected_has _default_http_headers => (
206 is => 'lazy',
207 isa => ArrayRef,
208 init_arg => undef,
209 handles_via => 'Array',
210 handles => {
211 _default_http_headers_list => 'all',
212 },
213 );
214  
215 4 ▶sub _build__default_http_headers {
Subroutine _build__default_http_headers: called CC=1 SCAR=0.0
216 return [
217 4 'Content-Type' => 'application/json; charset=UTF-8',
218 'Datadog-Meta-Lang' => 'perl',
219 'Datadog-Meta-Lang-Interpreter' => $EXECUTABLE_NAME,
220 'Datadog-Meta-Lang-Version' => $PERL_VERSION->stringify,
221 'Datadog-Meta-Tracer-Version' => $VERSION,
222 ]
223 }
224  
225  
226  
227 14 has _json_encoder => (
228 is => 'lazy',
229 init_arg => undef,
230 handles => { _json_encode => 'encode' },
231 );
232  
233 4 ▶sub _build__json_encoder {
Subroutine _build__json_encoder: called CC=1 SCAR=0.0
234 4 JSON()->new->utf8->canonical->pretty->allow_bignum
235 }
236 #
237 # I just love readable and consistant JSONs
238  
239  
240  
241 =head2 C<span_buffer_threshold>
242  
243 This sets the size limit of the span buffer. When this number is reached, this
244 C<Client> will send off the buffered spans using the internal C<user_agent>.
245  
246 This number can be set on instantiation, or will take it from the
247 C<DD_TRACE_PARTIAL_FLUSH_MIN_SPANS> environment variable. If nothing is set, it
248 defaults to 100.
249  
250 The number can not be set to anything higher than 20_000.
251  
252 If this number is C<0> (zero), spans will be sent with each call to
253 C<send_span>.
254  
255 =cut
256  
257 14 has span_buffer_threshold => (
258 is => 'rw',
259 isa => IntRange[ 0, MAX_SPANS ],
260 env_key => 'DD_TRACE_PARTIAL_FLUSH_MIN_SPANS',
261 default => 100,
262 );
263  
264  
265  
266 protected_has _span_buffer => (
267 is => 'rw',
268 isa => ArrayRef,
269 init_args => undef,
270 10 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
271 14 handles_via => 'Array',
272 handles => {
273 _buffer_span => 'push',
274 _span_buffer_size => 'count',
275 _buffered_spans => 'all',
276 _empty_span_buffer => 'clear',
277 },
278 );
279  
280  
281  
282 14 protected_has _client_halted => (
283 is => 'rw',
284 isa => Bool,
285 reader => '_has_client_halted',
286 default => 0,
287 handles_via => 'Bool',
288 handles => {
289 _halt_client => 'set'
290 },
291 );
292  
293  
294  
295 =head1 DELEGATED INSTANCE METHODS
296  
297 The following method(s) are required by the L<DataDog::Tracer|
298 OpenTracing::Implementation::DataDog::Tracer>:
299  
300 =cut
301  
302  
303  
304 =head2 C<send_span>
305  
306 This method gets called by the L<DataDog::Tracer|
307 OpenTracing::Implementation::DataDog::Tracer> to send a L<Span> with its
308 specific L<DataDog::SpanContext|OpenTracing::Implementation::DataDog::SpanContext>.
309  
310 This will typically get called during C<on_finish>.
311  
312 =head3 Required Positional Arguments
313  
314 =over
315  
316 =item C<$span>
317  
318 An L<OpenTracing Span|OpenTracing::Interface::Span> compliant object, that will
319 be serialised (using L<to_struct> and converted to JSON).
320  
321 =back
322  
323 =head3 Returns
324  
325 =over
326  
327 =item C<undef>
328  
329 in case something went wrong during the HTTP-request or the client has been
330 halted in any previous call.
331  
332 =item a positive int
333  
334 indicating the number of collected spans, in case this client has only buffered
335 the span.
336  
337 =item a negative int
338  
339 indicating the number of flushed spans, in case the client has succesfully
340 flushed the spans collected in the buffer.
341  
342 =back
343  
344 =cut
345  
346 12 ▶sub send_span {
Subroutine send_span: called CC=6 SCAR=18.0
Pod documented
347 12 my $self = shift;
348 12 my $span = shift;
349
350 return
351 12 ▶ if $self->_has_client_halted();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_client_halted 1 11 12
352 # do not add more spans to the buffer
353
354 11 my $new_span_buffer_size = $self->_buffer_span($span);
355
356 11 ▶ return $new_span_buffer_size
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_span_buffer_size // 0) > 0 0 11 11
Condition50%1/2
$new_span_buffer_size // 0
l!l
110
MC/DC0%0/1
$new_span_buffer_size // 0
$new_span_buffer_size
Truth table50%1/2
A
A= $new_span_buffer_size
Aresult
11
00
357 unless ( $new_span_buffer_size // 0 ) > 0;
358 # this should be the number of spans in the buffer, should not be undef or 0
359
360 11 ▶ return $new_span_buffer_size
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_flush_span_buffer 7 4 11
361 unless $self->_should_flush_span_buffer();
362
363 4 my $flushed = $self->_flush_span_buffer();
364
365 return
366 4 ▶ unless defined $flushed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flushed 1 3 4
367
368 3 return -$flushed
369
370 }
371  
372  
373  
374 =head1 INSTANCE METHODS
375  
376 =cut
377  
378  
379  
380 =head2 C<to_struct>
381  
382 Gather required data from a single span and its context, tags and baggage items.
383  
384 =head3 Required Positional Arguments
385  
386 =over
387  
388 =item C<$span>
389  
390 =back
391  
392 =head3 Returns
393  
394 a hashreference with the following keys:
395  
396 =over
397  
398 =item C<trace_id>
399  
400 =item C<span_id>
401  
402 =item C<resource>
403  
404 =item C<service>
405  
406 =item C<type> (optional)
407  
408 =item C<env> (optional)
409  
410 =item C<hostname> (optional)
411  
412 =item C<name>
413  
414 =item C<start>
415  
416 =item C<duration>
417  
418 =item C<parent_id> (optional)
419  
420 =item C<error>
421  
422 =item C<meta> (optional)
423  
424 =item C<metrics>
425  
426 =back
427  
428 =head3 Notes
429  
430 This data structure is specific for sending it through the DataDog agent and
431 therefore can not be a intance method of the DataDog::Span object.
432  
433 =cut
434  
435 14 ▶sub to_struct {
Subroutine to_struct: called CC=3 SCAR=11.0
Pod documented
436 14 my $self = shift;
437 14 my $span = shift;
438
439 14 my $context = $span->get_context();
440  
441 14 my %meta_data = (
442 _fixup_span_tags( $span->get_tags ),
443 $context->get_baggage_items,
444 );
445
446 # fix issue with meta-data, values must be string!, and just skip undefined
447 %meta_data =
448 map {
449 14 ▶ defined $meta_data{$_} ? ( $_ => "$meta_data{$_}" ) : ()
Branch: 2 branches
BranchTrueFalseTotal
if %meta_data 8 6 14
defined $meta_data{$_} ? : 22 0 22
450 } keys %meta_data
451 if %meta_data;
452
453 14 my $data = {
454 trace_id => $context->trace_id,
455 span_id => $context->span_id,
456 resource => $context->get_resource_name,
457 service => $context->get_service_name,
458
459 maybe
460 type => $context->get_service_type,
461
462 maybe
463 env => $context->get_environment,
464
465 maybe
466 hostname => $context->get_hostname,
467
468 maybe
469 version => $context->get_version,
470
471 name => $span->get_operation_name,
472 start => nano_seconds( $span->start_time() ),
473 duration => nano_seconds( $span->duration() ),
474
475 maybe
476 parent_id => $span->get_parent_span_id(),
477
478 provided _is_with_errors( $span ),
479 error => 1,
480
481 provided %meta_data,
482 meta => { %meta_data },
483
484 # metrics => ... ,
485 };
486
487 # TODO: use Hash::Ordered, so we can control what will be the first item in
488 # the long string of JSON text. But this needs investigation on how
489 # this behaves with JSON
490
491 14 return $data
492 }
493  
494  
495  
496 =head1 ENVIRONMENT VARIABLES
497  
498 For configuring DataDog Tracing there is support for the folllowing environment
499 variables:
500  
501  
502  
503 =head2 C<DD_AGENT_HOST>
504  
505 Hostname for where to send traces to. If using a containerized environment,
506 configure this to be the host IP.
507  
508 B<default:> C<localhost>
509  
510  
511  
512 =head2 C<DD_TRACE_AGENT_PORT>
513  
514 The port number the Agent is listening on for configured host. If the Agent
515 configuration sets receiver_port or C<DD_APM_RECEIVER_PORT> to something other
516 than the default B<8126>, then C<DD_TRACE_AGENT_PORT> or C<DD_TRACE_AGENT_URL>
517 must match it.
518  
519 B<default:> C<8126>
520  
521  
522 =head2 C<DD_TRACE_AGENT_URL>
523  
524 The URL to send traces to. If the Agent configuration sets receiver_port or
525 C<DD_APM_RECEIVER_PORT> to something other than the default B<8126>, then
526 C<DD_TRACE_AGENT_PORT> or C<DD_TRACE_AGENT_URL> must match it. The URL value can
527 start with C<http://> to connect B<using HTTP> or with C<unix://> to use a
528 B<Unix Domain Socket>.
529  
530 When set this takes precedence over C<DD_AGENT_HOST> and C<DD_TRACE_AGENT_PORT>.
531  
532 B<CAVEATE: > the C<unix:> scheme is non-exisitent, and is not supported with the
533 L<DataDog::Client|OpenTracing::Implementation::DataDog::Client>.
534  
535  
536  
537 =head2 C<DD_TRACE_PARTIAL_FLUSH_MIN_SPANS>
538  
539 Set a number of partial spans to flush on. Useful to reduce memory overhead when
540 dealing with heavy traffic or long running traces.
541  
542 B<default:> 100
543  
544  
545  
546 =head1 SEE ALSO
547  
548 =over
549  
550 =item L<OpenTracing::Implementation::DataDog>
551  
552 Sending traces to DataDog using Agent.
553  
554 =item L<DataDog Docs API Tracing|https://docs.datadoghq.com/api/v1/tracing/>
555  
556 The DataDog B<Agent API> Documentation.
557  
558 =item L<LWP::UserAgent>
559  
560 Web user agent class
561  
562 =item L<JSON::Maybe::XS>
563  
564 Use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP>
565  
566 =item L<HTTP::Request>
567  
568 HTTP style request message
569  
570 =item L<HTTP::Response>
571  
572 HTTP style response message
573  
574 =back
575  
576  
577  
578 =head1 AUTHOR
579  
580 Theo van Hoesel <tvanhoesel@perceptyx.com>
581  
582  
583  
584 =head1 COPYRIGHT AND LICENSE
585  
586 'OpenTracing::Implementation::DataDog'
587 is Copyright (C) 2019 .. 2021, Perceptyx Inc
588  
589 This library is free software; you can redistribute it and/or modify it under
590 the terms of the Artistic License 2.0.
591  
592 This package is distributed in the hope that it will be useful, but it is
593 provided "as is" and without any express or implied warranties.
594  
595 For details, see the full text of the license in the file LICENSE.
596  
597  
598 =cut
599  
600  
601 # _fixup_span_tags
602 #
603 # rename and or remove key value pairs from standard OpenTracing to what
604 # DataDog expects to be send
605 #
606 14 ▶sub _fixup_span_tags {
Subroutine _fixup_span_tags: called CC=3 SCAR=11.0
607 14 my %tags = @_;
608
609 14 my $error = delete $tags { error };
610
611 14 ▶ $tags { 'error.type' } = delete $tags{ 'error.kind' }
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 13 14
612 if $error;
613
614 14 ▶ $tags { 'error.message' } = delete $tags{ 'message' }
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 13 14
615 if $error;
616
617 14 return %tags;
618 }
619  
620  
621  
622 # _flush_span_buffer
623 #
624 # Flushes the spans in the span buffer and send them off to the DataDog agent
625 # over HTTP.
626 #
627 # Returns the number off flushed spans or `undef` in case of an error.
628 #
629 6 ▶sub _flush_span_buffer {
Subroutine _flush_span_buffer: called CC=2 SCAR=6.9
630 6 my $self = shift;
631
632 6 my @structs = map {$self->to_struct($_) } $self->_buffered_spans();
633
634 6 ▶ my $resp = $self->_http_post_struct_as_json( [ \@structs ] )
Branch: 1 branch
BranchTrueFalseTotal
my $resp = $self->_http_post_struct_as_json([\@structs]) or return 4 2 6
635 or return;
636
637 4 $self->_empty_span_buffer();
638
639 4 return scalar @structs;
640 }
641  
642  
643  
644 # checks if there is an exisiting 'error' tag
645 #
646 14 ▶sub _is_with_errors {
Subroutine _is_with_errors: called CC=1 SCAR=0.0
647 14 my $span = shift;
648 return exists { $span->get_tags() }->{ error }
649 14 }
650  
651  
652  
653 # _is_uri
654 #
655 # Returns true if the given string matches an http(s) url
656 #
657 4 ▶sub _is_uri {
Subroutine _is_uri: called CC=1 SCAR=0.0
658 4 return $RE{URI}{HTTP}{-scheme => 'https?'}->matches(shift)
659 # scheme must be specified, defaults to 'http:'
660 }
661  
662  
663  
664 # _http_headers_with_trace_count
665 #
666 # Returns a list of HTTP Headers needed for DataDog
667 #
668 # This feature was originally added, so the Trace-Count could dynamically set
669 # per request. That was a design flaw, and now the count is hardcoded to '1',
670 # until we figured out how to send multiple spans.
671 #
672 6 ▶sub _http_headers_with_trace_count {
Subroutine _http_headers_with_trace_count: called CC=1 SCAR=0.0
673 6 my $self = shift;
674 6 my $count = shift;
675
676 return (
677 6 $self->_default_http_headers_list,
678
679 maybe
680 'X-Datadog-Trace-Count' => $count,
681 )
682 }
683  
684  
685  
686 # _http_post_struct_as_json
687 #
688 # Takes a given data structure and sends an HTTP POST request to the tracing
689 # agent.
690 #
691 # It is the caller's responsibility to generate the correct data structure!
692 #
693 # Maybe returns an HTTP::Response object, which may indicate a failure.
694 #
695 7 ▶sub _http_post_struct_as_json {
Subroutine _http_post_struct_as_json: called CC=4 SCAR=13.9
696 7 my $self = shift;
697 7 my $struct = shift;
698
699 return
700 7 ▶ if $self->_has_client_halted();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_client_halted 1 6 7
701 # this shouldn't be needed, but will happen on DEMOLISH & spans in buffer
702  
703 6 my $encoded_data = $self->_json_encode($struct);
704 0 do { warn "$encoded_data\n" }
705 6 ▶ if $ENV{OPENTRACING_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OPENTRACING_DEBUG"}) 0 6 6
706
707 6 my @headers = $self->_http_headers_with_trace_count( scalar @{$struct->[0]} );
708 6 my $rqst = HTTP::Request->new( 'POST', $self->uri, \@headers, $encoded_data );
709
710 6 my $resp = $self->_send_http_request( $rqst );
711 6 ▶ if ( $resp->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_error) 1 5 6
712 #
713 # not interested in what the error actually has been, no matter what it
714 # was, this client will be halted, be it an error in the data send (XXX)
715 # or a problem with the recipient tracing agent.
716 #
717 1 $self->_halt_client();
718 1 warn sprintf "DataDog::Client being halted due to an error [%s]\n",
719 $resp->status_line;
720 1 return;
721 }
722
723 5 return $resp;
724 }
725  
726  
727  
728 # _last_buffered_span
729 #
730 # Returns the last span added to the buffer.
731 #
732 # nothing special, but just easier to read the code where it is used
733 #
734 11 ▶sub _last_buffered_span {
Subroutine _last_buffered_span: called CC=1 SCAR=0.0
735 11 my $self = shift;
736
737 11 return $self->_span_buffer->[-1]
738 }
739  
740  
741  
742 # _should_flush_span_buffer
743 #
744 # Returns a 'Boolean'
745 #
746 # For obvious reasons, it should be flushed if the limit has been reached.
747 # But another reason is when the root-span has been just added. It is the first
748 # span being created, but it is therefor the last one being closed and send.
749 #
750 11 ▶sub _should_flush_span_buffer {
Subroutine _should_flush_span_buffer: called CC=2 SCAR=6.9
751 11 my $self = shift;
752
753 return (
754 11 ▶ $self->_last_buffered_span()->is_root_span
Condition100%3/3
$self->_last_buffered_span->is_root_span || $self->_span_buffer_threshold_reached
l!l&&r!l&&!r
137
MC/DC100%2/2
$self->_last_buffered_span->is_root_span || $self->_span_buffer_threshold_reached
$self->_last_buffered_span->is_root_span $self->_span_buffer_threshold_reached
Truth table100%3/3
A || B
A= $self->_last_buffered_span->is_root_spanB= $self->_span_buffer_threshold_reached
ABresult
1X1
011
000
755 or
756 $self->_span_buffer_threshold_reached()
757 );
758 }
759  
760  
761  
762 # _span_buffer_threshold_reached
763 #
764 # Returns a 'Boolean', being 'true' once the limit has been reached
765 #
766 10 ▶sub _span_buffer_threshold_reached {
Subroutine _span_buffer_threshold_reached: called CC=1 SCAR=0.0
767 10 my $self = shift;
768
769 10 return $self->_span_buffer_size >= $self->span_buffer_threshold
770 }
771  
772  
773 # DEMOLISH
774 #
775 # This should not happen, but just in case something went completely wrong, this
776 # will try to flush the buffered spans as a last resort.
777 #
778 9 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
779 9 my ($self) = @_;
780
781 9 ▶ $self->_flush_span_buffer() if $self->_span_buffer_size(); # send leftovers
Branch: 1 branch
BranchTrueFalseTotal
if $self->_span_buffer_size 2 7 9
782
783 9 return;
784 }
785  
786 14 1;
« DataDog/Tracer.pm ↑ Summary DataDog/HTTPPropagator.pm »