DataDog/Tracer.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 75.0% 3 / 4 Conditioncond 55.5% 10 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 3 totaltotal 82.7% 115 / 139 CC 10 SCAR 10.7
CC
10
Cov
90%
build_context
8.1
extract_context_from_http_headers
3.0
__ANON__
2.1
CRAP 2.9
↑ Summary DataDog/Client.pm »
1 package OpenTracing::Implementation::DataDog::Tracer;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 5 our $VERSION = 'v0.47.2';
8  
9 =head1 NAME
10  
11 OpenTracing::Implementation::DataDog::Tracer - Keep track of traces
12  
13 =head1 SYNOPSIS
14  
15 use aliased 'OpenTracing::Implementation::DataDog::Tracer';
16 use aliased 'OpenTracing::Implementation::DataDog::Client';
17 use aliased 'OpenTracing::Implementation::DataDog::ScopeManager';
18
19 my $TRACER = Tracer->new(
20 client => Client->new(),
21 );
22  
23 and later
24  
25 sub foo {
26
27 my $scope = $TRACER->start_active_span( 'Operation Name' => %options );
28
29 ...
30
31 $scope->close;
32
33 return $foo
34 }
35  
36 =cut
37  
38 8 ▶use syntax 'maybe';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 ▶use MooX::Should;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 5 with 'OpenTracing::Role::Tracer';
44  
45 8 ▶use aliased 'OpenTracing::Implementation::DataDog::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use aliased 'OpenTracing::Implementation::DataDog::HTTPPropagator';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 ▶use aliased 'OpenTracing::Implementation::DataDog::ScopeManager';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 8 ▶use aliased 'OpenTracing::Implementation::DataDog::Span';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 8 ▶use aliased 'OpenTracing::Implementation::DataDog::SpanContext';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 8 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 8 ▶use Ref::Util qw/is_plain_hashref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 8 ▶use Types::Standard qw/Object Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55  
56  
57 =head1 DESCRIPTION
58  
59 This is a L<OpenTracing SpanContext|OpenTracing::Interface::SpanContext>
60 compliant implementation with DataDog specific extentions
61  
62 =cut
63  
64  
65  
66 =head1 EXTENDED ATTRIBUTES
67  
68 =cut
69  
70  
71  
72 =head2 C<scope_manager>
73  
74 A L<OpenTracing::Types::ScopeManger> that now defaults to a
75 L<DataDog::ScopeManger|OpenTracing::Implementation::DataDog::ScopeManager>
76  
77 =cut
78  
79 has '+scope_manager' => (
80 23 ▶ default => sub { ScopeManager->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 5 );
82  
83  
84  
85 =head1 DATADOG SPECIFIC ATTRIBUTES
86  
87 =cut
88  
89  
90  
91 =head2 C<client>
92  
93 A client that has a C<send_span> method that will get called on a `on_finish`.
94  
95 See L<DataDog::Client|OpenTracing::Implementation::DataDog::Client> for more.
96  
97 It also accepts a plain hash refference with key-value pairs suitable to
98 construct a client object.
99  
100 =cut
101  
102 has client => (
103 is => 'lazy',
104 should => Object,
105 handles => [qw/send_span/],
106 coerce
107 1 ▶ => sub { is_plain_hashref $_[0] ? Client->new( %{$_[0]} ) : $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
is_plain_hashref($_[0]) ? : 1 0 1
108 0 ▶ default => sub { {} }, # XXX this does not return an Object !!!
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 5 );
110  
111  
112  
113 5 has default_resource_name => (
114 is => 'ro',
115 should => Str,
116 predicate => 1,
117 );
118  
119  
120  
121 5 has default_service_name => (
122 is => 'ro',
123 should => Str,
124 predicate => 1,
125 );
126  
127  
128  
129 5 has default_service_type => (
130 is => 'ro',
131 should => Str,
132 predicate => 1,
133 );
134  
135  
136  
137 5 has default_environment => (
138 is => 'ro',
139 should => Str,
140 predicate => 1,
141 );
142  
143  
144  
145 5 has default_hostname => (
146 is => 'ro',
147 should => Str,
148 predicate => 1,
149 );
150  
151  
152  
153 5 has default_version => (
154 is => 'ro',
155 should => Str,
156 predicate => 1,
157 );
158  
159 has '_http_propagator' => (
160 is => 'ro',
161 23 ▶ default => sub { HTTPPropagator->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
162 5 );
163  
164  
165  
166 14 ▶sub build_span {
Subroutine build_span: called CC=1 SCAR=0.0
Pod undocumented
167 14 my $self = shift;
168 14 my %opts = @_;
169
170 my $span = Span->new(
171
172 operation_name => $opts{ operation_name },
173
174 maybe
175 child_of => $opts{ child_of },
176
177 maybe
178 start_time => $opts{ start_time },
179
180 maybe
181 tags => $opts{ tags },
182
183 context => $opts{ context },
184
185 on_finish => sub {
186 my $span = shift;
187 $self->send_span( $span )
188 },
189
190 14 );
191
192 12 return $span
193 }
194  
195  
196  
197 17 ▶sub build_context {
Subroutine build_context: called CC=7 SCAR=21.0
Pod undocumented
198 17 my $self = shift;
199 17 my %opts = @_;
200
201 my $resource_name = delete $opts{ resource_name }
202 17 ▶ || $self->default_resource_name;
Condition100%3/3
delete $opts{'resource_name'} || $self->default_resource_name
l!l&&r!l&&!r
2166
MC/DC0%0/2
delete $opts{'resource_name'} || $self->default_resource_name
delete $opts{'resource_name'} $self->default_resource_name
Truth table33%1/3
A || B
A= delete $opts{'resource_name'}B= $self->default_resource_name
ABresult
1X1
011
000
203
204 my $service_name = delete $opts{ service_name }
205 17 ▶ || $self->default_service_name;
Condition66%2/3
delete $opts{'service_name'} || $self->default_service_name
l!l&&r!l&&!r
0151
MC/DC50%1/2
delete $opts{'service_name'} || $self->default_service_name
delete $opts{'service_name'} $self->default_service_name
Truth table66%2/3
A || B
A= delete $opts{'service_name'}B= $self->default_service_name
ABresult
1X1
011
000
206
207 my $service_type = delete $opts{ service_type }
208 17 ▶ || $self->default_service_type;
Condition66%2/3
delete $opts{'service_type'} || $self->default_service_type
l!l&&r!l&&!r
097
MC/DC50%1/2
delete $opts{'service_type'} || $self->default_service_type
delete $opts{'service_type'} $self->default_service_type
Truth table66%2/3
A || B
A= delete $opts{'service_type'}B= $self->default_service_type
ABresult
1X1
011
000
209
210 my $environment = delete $opts{ environment }
211 17 ▶ || $self->default_environment;
Condition33%1/3
delete $opts{'environment'} || $self->default_environment
l!l&&r!l&&!r
0016
MC/DC0%0/2
delete $opts{'environment'} || $self->default_environment
delete $opts{'environment'} $self->default_environment
Truth table33%1/3
A || B
A= delete $opts{'environment'}B= $self->default_environment
ABresult
1X1
011
000
212
213 my $hostname = delete $opts{ hostname }
214 17 ▶ || $self->default_hostname;
Condition33%1/3
delete $opts{'hostname'} || $self->default_hostname
l!l&&r!l&&!r
0016
MC/DC0%0/2
delete $opts{'hostname'} || $self->default_hostname
delete $opts{'hostname'} $self->default_hostname
Truth table33%1/3
A || B
A= delete $opts{'hostname'}B= $self->default_hostname
ABresult
1X1
011
000
215
216 my $version = delete $opts{ version }
217 17 ▶ || $self->default_version;
Condition33%1/3
delete $opts{'version'} || $self->default_version
l!l&&r!l&&!r
0016
MC/DC0%0/2
delete $opts{'version'} || $self->default_version
delete $opts{'version'} $self->default_version
Truth table33%1/3
A || B
A= delete $opts{'version'}B= $self->default_version
ABresult
1X1
011
000
218
219 17 my $span_context = SpanContext->new(
220
221 %opts,
222
223 resource_name => $resource_name,
224
225 maybe
226 service_name => $service_name,
227
228 maybe
229 service_type => $service_type,
230
231 maybe
232 environment => $environment,
233
234 maybe
235 hostname => $hostname,
236
237 maybe
238 version => $version,
239
240 );
241
242 17 return $span_context
243 }
244  
245  
246  
247 sub inject_context_into_array_reference { return $_[1] } # $carrier
248  
249  
250  
251 sub inject_context_into_hash_reference {
252 2 my $self = shift;
253 2 my $carrier = shift;
254 9 my $context = shift;
255
256 9 return Hash::Merge->new('RIGHT_PRECEDENT')->merge(
257 $carrier,
258 {
259 opentracing_context => {
260 trace_id => $context->trace_id,
261 span_id => $context->span_id,
262 resource => $context->get_resource_name,
263 service => $context->get_service_name,
264 maybe
265 type => $context->get_service_type,
266 maybe
267 environment => $context->get_environment,
268 maybe
269 hostname => $context->get_hostname,
270 }
271
272 }
273 )
274 }
275  
276  
277  
278 sub inject_context_into_http_headers {
279 9 my ($self, $carrier, $context) = @_;
280 9 $carrier = $carrier->clone;
281  
282 2 $self->_http_propagator->inject($carrier, $context);
283  
284 2 return $carrier;
285 }
286  
287 sub extract_context_from_array_reference { return undef }
288 sub extract_context_from_hash_reference { return undef }
289  
290 8 ▶sub extract_context_from_http_headers {
Subroutine extract_context_from_http_headers: called CC=3 SCAR=11.0
Pod undocumented
291 2 my ($self, $carrier) = @_;
292 2 my ($trace_id, $span_id) = $self->_http_propagator->extract($carrier);
293 2 ▶ return unless defined $trace_id and defined $span_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trace_id and defined $span_id 2 6 8
294  
295 2 return $self->build_context()
296 ->with_trace_id($trace_id)
297 ->with_span_id($span_id);
298 }
299  
300 =head1 SEE ALSO
301  
302 =over
303  
304 =item L<OpenTracing::Implementation::DataDog>
305  
306 Sending traces to DataDog using Agent.
307  
308 =item L<OpenTracing::Role::Tracer>
309  
310 Role for OpenTracing Implementations.
311  
312 =back
313  
314  
315  
316 =head1 AUTHOR
317  
318 Theo van Hoesel <tvanhoesel@perceptyx.com>
319  
320  
321  
322 =head1 COPYRIGHT AND LICENSE
323  
324 'OpenTracing::Implementation::DataDog'
325 is Copyright (C) 2019 .. 2021, Perceptyx Inc
326  
327 This library is free software; you can redistribute it and/or modify it under
328 the terms of the Artistic License 2.0.
329  
330 This package is distributed in the hope that it will be useful, but it is
331 provided "as is" and without any express or implied warranties.
332  
333 For details, see the full text of the license in the file LICENSE.
334  
335  
336 =cut
337  
338 5 1;
↑ Summary DataDog/Client.pm »