Role/Tracer.pm

filter: Statementstmt 95.0% 135 / 142 Branchbran 73.0% 19 / 26 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 90.2% 37 / 41 Podpod 83.3% 5 / 6 totaltotal 89.0% 203 / 228 CC 17 SCAR 9.8
CC
17
Cov
90%
start_span
8.2
get_active_context
6.0
_first_context_formatter_for_carrier
3.1
CRAP 2.7
« Role/Scope.pm ↑ Summary Role/Span.pm »
1 package OpenTracing::Role::Tracer;
2  
3 9 our $VERSION = 'v0.86.1';
4  
5 9 ▶use syntax qw/maybe/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use MooX::Should;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use OpenTracing::Types qw/ScopeManager Span SpanContext is_Span is_SpanContext/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Ref::Util qw/is_plain_hashref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Role::Declare::Should -lax;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Types::Common::Numeric qw/PositiveOrZeroNum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Types::Standard qw/ArrayRef CodeRef Dict HashRef InstanceOf Maybe Object Str Undef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Types::TypeTiny qw/TypeTiny/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 our @CARP_NOT;
22  
23 has scope_manager => (
24 is => 'ro',
25 should => ScopeManager,
26 reader => 'get_scope_manager',
27 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 require 'OpenTracing::Implementation::NoOp::ScopeManager';
29 0 return OpenTracing::Implementation::NoOp::ScopeManager->new
30 },
31 9 );
32  
33 2 ▶sub get_active_span {
Subroutine get_active_span: called CC=3 SCAR=11.0
Pod documented
34 2 my $self = shift;
35
36 2 ▶ my $scope_manager = $self->get_scope_manager
Branch: 1 branch
BranchTrueFalseTotal
my $scope_manager = $self->get_scope_manager or croak("Can't get a 'ScopeManager'") 2 0 2
37 or croak "Can't get a 'ScopeManager'";
38
39 2 ▶ my $scope = $scope_manager->get_active_scope
Branch: 1 branch
BranchTrueFalseTotal
my $scope = $scope_manager->get_active_scope or return 1 1 2
40 or return;
41
42 1 return $scope->get_span;
43 }
44  
45 5 ▶sub start_active_span {
Subroutine start_active_span: called CC=3 SCAR=11.0
Pod documented
46 5 my $self = shift;
47 5 ▶ my $operation_name = shift
Branch: 1 branch
BranchTrueFalseTotal
my $operation_name = shift() or croak("Missing required operation_name") 5 0 5
48 or croak "Missing required operation_name";
49 5 my $opts = { @_ };
50
51 my $finish_span_on_close =
52 exists( $opts->{ finish_span_on_close } ) ?
53 !! delete $opts->{ finish_span_on_close }
54 : !undef
55 5 ▶ ; # use 'truthness' of param if provided, or set to 'true' otherwise
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'finish_span_on_close'} ? : 3 2 5
56
57 5 my $span = $self->start_span( $operation_name => %$opts );
58
59 5 my $scope_manager = $self->get_scope_manager();
60 5 my $scope = $scope_manager->activate_span( $span,
61 finish_span_on_close => $finish_span_on_close
62 );
63
64 5 return $scope
65 }
66  
67 2 ▶sub start_span {
Subroutine start_span: called CC=8 SCAR=21.0
Pod documented
68 2 my $self = shift;
69
70 2 ▶ my $operation_name = shift
Branch: 1 branch
BranchTrueFalseTotal
my $operation_name = shift() or croak("Missing required operation_name") 2 0 2
71 or croak "Missing required operation_name";
72 2 my $opts = { @_ };
73
74 2 my $start_time = delete $opts->{ start_time };
75 2 my $ignore_active_span = delete $opts->{ ignore_active_span };
76 2 my $child_of = delete $opts->{ child_of };
77 2 my $tags = delete $opts->{ tags };
78
79 2 ▶ $child_of //= $self->get_active_span()
Branch: 1 branch
BranchTrueFalseTotal
unless $ignore_active_span 1 1 2
Condition33%1/3
$child_of //= $self->get_active_span
l!l&&r!l&&!r
001
MC/DC0%0/2
$child_of //= $self->get_active_span
$child_of $self->get_active_span
Truth table33%1/3
A //= B
A= $child_ofB= $self->get_active_span
ABresult
1X1
011
000
80 unless $ignore_active_span;
81
82 2 my $context;
83
84 2 ▶ $context = $child_of
Branch: 1 branch
BranchTrueFalseTotal
if is_SpanContext($child_of) 1 1 2
85 if is_SpanContext($child_of);
86
87 2 ▶ $context = $child_of->get_context
Branch: 1 branch
BranchTrueFalseTotal
if is_Span($child_of) 0 2 2
88 if is_Span($child_of);
89
90 2 ▶ $context = $context->new_clone->with_trace_id( $context->trace_id )
Branch: 1 branch
BranchTrueFalseTotal
if is_SpanContext($context) 1 1 2
91 if is_SpanContext($context);
92
93 2 ▶ $context = $self->build_context( )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 1 1 2
94 unless defined $context;
95
96 2 my $span = $self->build_span(
97 operation_name => $operation_name,
98 context => $context,
99
100 maybe
101 child_of => $child_of,
102
103 maybe
104 start_time => $start_time,
105
106 maybe
107 tags => $tags,
108 );
109 #
110 # we should get rid of passing 'child_of' or the not exisitng 'follows_from'
111 # these are merely helpers to define 'references'.
112
113 2 return $span
114 }
115  
116  
117  
118 3 ▶sub extract_context {
Subroutine extract_context: called CC=1 SCAR=0.0
Pod documented
119 3 my $self = shift;
120 3 my $carrier = shift;
121 3 my $context = shift;
122
123 3 my $context_formatter =
124 $self->_first_context_formatter_for_carrier( $carrier );
125
126 3 my $formatter = $context_formatter->{extractor};
127 3 return $self->$formatter( $carrier );
128 }
129  
130  
131  
132 9 ▶sub inject_context {
Subroutine inject_context: called CC=3 SCAR=11.0
Pod documented
133 9 my $self = shift;
134 9 my $carrier = shift;
135 9 my $context = shift;
136
137 9 my $context_formatter =
138 $self->_first_context_formatter_for_carrier( $carrier );
139
140 9 ▶ $context //= $self->get_active_context();
Condition100%3/3
$context //= $self->get_active_context
l!l&&r!l&&!r
333
MC/DC100%2/2
$context //= $self->get_active_context
$context $self->get_active_context
Truth table100%3/3
A //= B
A= $contextB= $self->get_active_context
ABresult
1X1
011
000
141 9 ▶ return $carrier unless defined $context;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 3 6 9
142 6 my $formatter = $context_formatter->{injector};
143 6 return $self->$formatter( $carrier, $context );
144 }
145  
146  
147  
148 # XXX this is not a OpenTracing API method
149 #
150 0 ▶sub get_active_context {
Subroutine get_active_context: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my $self = shift;
152
153 0 ▶ my $active_span = $self->get_active_span
Branch: 1 branch
BranchTrueFalseTotal
my $active_span = $self->get_active_span or return 0 0 0
154 or return;
155
156 0 return $active_span->get_context
157 }
158  
159  
160  
161 9 ▶use constant ContextFormatter => Dict[
Subroutine BEGIN: called CC=1 SCAR=0.0
162 type => TypeTiny,
163 injector => CodeRef,
164 extractor => CodeRef,
165 9 ];
166  
167  
168  
169 9 has context_formatters => (
170 is => 'rw',
171 should => ArrayRef[ContextFormatter],
172 handles_via => 'Array',
173 handles => {
174 register_context_formatter => 'unshift',
175 known_context_formatters => 'elements',
176 },
177 default => \&_default_context_formatters,
178 );
179  
180 20 ▶sub _default_context_formatters {
Subroutine _default_context_formatters: called CC=1 SCAR=0.1
181 [
182 {
183 type => Undef,
184 0 ▶ injector => sub {undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 0 ▶ extractor => sub {undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 },
187 {
188 type => ArrayRef,
189 2 ▶ injector => sub {shift->inject_context_into_array_reference(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
190 1 ▶ extractor => sub {shift->extract_context_from_array_reference(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
191 },
192 {
193 type => HashRef,
194 2 ▶ injector => sub {shift->inject_context_into_hash_reference(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
195 1 ▶ extractor => sub {shift->extract_context_from_hash_reference(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
196 },
197 {
198 type => InstanceOf['HTTP::Headers'],
199 2 ▶ injector => sub {shift->inject_context_into_http_headers(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
200 1 ▶ extractor => sub {shift->extract_context_from_http_headers(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
201 },
202 20 ]
203 }
204  
205 12 ▶sub _first_context_formatter_for_carrier {
Subroutine _first_context_formatter_for_carrier: called CC=3 SCAR=11.2
206 12 my $self = shift;
207 12 my $carrier = shift;
208
209 36 ▶ my $context_formatter = first { $_->{type}->check($carrier) }
Subroutine __ANON__: called CC=1 SCAR=0.0
210 12 $self->known_context_formatters;
211
212 12 ▶ my $type = ref($carrier) || 'Scalar';
Condition50%1/2
ref $carrier || 'Scalar'
l!l
120
MC/DC0%0/1
ref $carrier || 'Scalar'
ref $carrier
Truth table50%1/2
A
A= ref $carrier
Aresult
11
00
213 12 ▶ croak "Unsupported carrier format [$type]"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context_formatter 0 12 12
214 unless defined $context_formatter;
215
216 12 return $context_formatter
217 }
218  
219  
220  
221 instance_method extract_context_from_array_reference(
222 ArrayRef $carrier,
223 9 ▶) :ReturnMaybe(SpanContext) {};
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 instance_method extract_context_from_hash_reference(
226 HashRef $carrier,
227 9 ▶) :ReturnMaybe(SpanContext) {};
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 instance_method extract_context_from_http_headers(
230 Object $carrier,
231 ) :ReturnMaybe(SpanContext) {
232 ( InstanceOf['HTTP::Headers'] )->assert_valid( $carrier )
233 9 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
234  
235 instance_method inject_context_into_array_reference(
236 ArrayRef $carrier,
237 Maybe[ SpanContext ] $span_context = undef,
238 9 ▶) :Return(ArrayRef) {};
Subroutine BEGIN: called CC=1 SCAR=0.0
239  
240 instance_method inject_context_into_hash_reference(
241 HashRef $carrier,
242 Maybe[ SpanContext ] $span_context = undef,
243 9 ▶) :Return(HashRef) {};
Subroutine BEGIN: called CC=1 SCAR=0.0
244  
245 instance_method inject_context_into_http_headers(
246 Object $carrier,
247 Maybe[ SpanContext ] $span_context = undef,
248 ) :Return(InstanceOf['HTTP::Headers']) {
249 ( InstanceOf['HTTP::Headers'] )->assert_valid( $carrier )
250 9 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252  
253 instance_method build_span (
254 Str :$operation_name,
255 SpanContext :$context,
256 Maybe[ SpanContext | Span ] :$child_of,
257 Maybe[ PositiveOrZeroNum ] :$start_time,
258 Maybe[ HashRef[Str] ] :$tags,
259 9 ▶) :Return (Span) { };
Subroutine BEGIN: called CC=1 SCAR=0.0
260  
261  
262 instance_method build_context (
263 %span_context_args,
264 ) :Return (SpanContext) {
265 ( HashRef[Str] )->assert_valid( { %span_context_args } );
266 9 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268  
269 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
270 # use Role::Tiny::With;
271 9 with 'OpenTracing::Interface::Tracer'
272 }
273  
274  
275  
276 9 1;
« Role/Scope.pm ↑ Summary Role/Span.pm »