Role/Tracer.pm

filter: Statementstmt 91.5% 54 / 59 Branchbran 68.7% 11 / 16 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 4 totaltotal 73.1% 79 / 108 CC 12 SCAR 13.9
CC
12
Cov
80%
start_span
9.8
get_active_span
3.0
start_active_span
3.0
CRAP 4.0
↑ Summary Role/Scope.pm »
1 package OpenTracing::Role::Tracer;
2  
3 =head1 NAME
4  
5 OpenTracing::Role::Tracer - Role for OpenTracin implementations.
6  
7 =head1 SYNOPSIS
8  
9 package OpenTracing::Implementation::MyBackendService::Tracer;
10
11 use Moo;
12
13 sub start_active_span { ... };
14
15 sub start_span { ... };
16
17 ...
18
19 with 'OpenTracing::Role::Tracer';
20
21 1;
22  
23 =cut
24  
25  
26  
27 7 our $VERSION = '0.08_003';
28  
29  
30  
31 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use OpenTracing::Types qw/ScopeManager Span SpanContext/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use Role::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 7 ▶use Types::Common::Numeric qw/PositiveOrZeroNum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use Types::Standard qw/HashRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40  
41  
42 =head1 DESCRIPTION
43  
44 This is a base-class for OpenTracing implenetations that are compliant with the
45 L<OpenTracing::Interface>.
46  
47 =cut
48  
49  
50  
51 7 has scope_manager => (
52 is => 'ro',
53 isa => ScopeManager,
54 reader => 'get_scope_manager',
55 required => 1,
56 );
57  
58  
59  
60 7 requires 'extract_context';
61  
62 7 requires 'inject_context';
63  
64  
65  
66 sub get_active_span {
67 2 ▶ my $self = shift;
Subroutine get_active_span: called CC=3 SCAR=11.0
Pod undocumented
68
69 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'") 0 2 2
70 or croak "Can't get a 'ScopeManager'";
71
72 2 ▶ my $scope = $scope_manager->get_active_scope
Branch: 1 branch
BranchTrueFalseTotal
my $scope = $scope_manager->get_active_scope or return 1 1 2
73 or return;
74
75 1 return $scope->get_span;
76 }
77  
78  
79  
80 # this is not an OpenTracing API requirement
81 #
82 sub get_active_span_context {
83 0 ▶ my $self = shift;
Subroutine get_active_span_context: not called CC=1 SCAR=6.9
Pod undocumented
84
85 my $span_context = try {
86 0 ▶ $self->get_active_span->get_context
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 } catch {
88 return undef
89 0 ▶ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
90
91 0 return $span_context
92 }
93  
94  
95  
96 sub start_active_span {
97 5 ▶ my $self = shift;
Subroutine start_active_span: called CC=3 SCAR=11.0
Pod undocumented
98 #
99 # TODO: croak when even number of arguments, after shifting invocant
100 # first must be a operation name
101 # then key / value pairs
102
103 5 ▶ my $operation_name = shift
Branch: 1 branch
BranchTrueFalseTotal
my $operation_name = shift() or croak("Missing required operation_name") 0 5 5
104 or croak "Missing required operation_name";
105 5 my $opts = { @_ };
106 #
107 # TODO: only check on 'finish_span_on_close'
108 # leave all other checks to 'start_span'
109
110 # remove the `finish_span_on_close` option, which is for this method only!
111 my $finish_span_on_close =
112 exists( $opts->{ finish_span_on_close } ) ?
113 !! delete $opts->{ finish_span_on_close }
114 : !undef
115 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
116
117 5 my $span = $self->start_span( $operation_name => %$opts );
118 #
119 # TODO: use 'try { ... } catch { croak }'
120
121 5 my $scope_manager = $self->get_scope_manager();
122
123 5 my $scope = $scope_manager->activate_span( $span,
124 finish_span_on_close => $finish_span_on_close
125 );
126
127 5 return $scope
128 }
129  
130  
131  
132 sub start_span {
133 1 ▶ my $self = shift;
Subroutine start_span: called CC=7 SCAR=22.8
Pod undocumented
134 #
135 # TODO: croak when even number of arguments, after shifting invocant
136 # first must be a operation name
137 # then key / value pairs
138
139 1 ▶ my $operation_name = shift
Branch: 1 branch
BranchTrueFalseTotal
my $operation_name = shift() or croak("Missing required operation_name") 0 1 1
140 or croak "Missing required operation_name";
141 1 my $opts = { @_ };
142
143 1 my $start_time = delete $opts->{ start_time };
144 1 my $ignore_active_span = delete $opts->{ ignore_active_span };
145 1 my $child_of = delete $opts->{ child_of };
146 1 my $tags = delete $opts->{ tags };
147 #
148 # TODO: croak whit remaining options
149
150 1 ▶ $child_of //= $self->get_active_span()
Branch: 1 branch
BranchTrueFalseTotal
unless $ignore_active_span 0 1 1
Condition0%0/3
$child_of //= $self->get_active_span
l!l&&r!l&&!r
000
MC/DC0%0/2
$child_of //= $self->get_active_span
$child_of $self->get_active_span
Truth table0%0/3
A //= B
A= $child_ofB= $self->get_active_span
ABresult
1X1
011
000
151 unless $ignore_active_span;
152
153 # $child_of->does(') ?
154
155 1 ▶ my $context =
Branch: 1 branch
BranchTrueFalseTotal
defined $child_of && $child_of->can('with_baggage_item') && $child_of->can('get_baggage_item') ? : 1 0 1
Condition33%2/6
defined $child_of and $child_of->can("with_baggage_item") and $child_of->can("get_baggage_item")
!ll&&!rl&&r
001
defined $child_of and $child_of->can("with_baggage_item")
!ll&&!rl&&r
001
MC/DC0%0/3
defined $child_of and $child_of->can("with_baggage_item") and $child_of->can("get_baggage_item")
defined $child_of $child_of->can("with_baggage_item") $child_of->can("get_baggage_item")
Truth table25%1/4
A and B and C
A= defined $child_ofB= $child_of->can("with_baggage_item")C= $child_of->can("get_baggage_item")
ABCresult
0XX0
10X0
1100
1111
156 defined $child_of
157 &&
158 # $child_of->does('OpenTracing::Interface::SpanContext')
159 $child_of->can('with_baggage_item')
160 &&
161 $child_of->can('get_baggage_item')
162 #
163 # 'does' does not work, then just check on most essential methods
164 # TODO: use OpenTracing::Types
165 #
166 ?
167 $child_of : $self->get_active_span_context();
168 #
169 # TODO: figure out why we want to get the active span it's context
170 # what if we have passed in a span, should we take it's context?
171 # is it more relevant to have the 'active context' at all times?
172
173 1 my $span = $self->build_span(
174 operation_name => $operation_name,
175 child_of => $child_of,
176 start_time => $start_time,
177 tags => $tags,
178 context => $context,
179 );
180 #
181 # we should get rid of passing 'child_of' or the not exisitng 'follows_from'
182 # these are merely helpers to define 'references'.
183 #
184 # TODO: use 'try { ... } catch { croak }'
185
186 1 return $span
187 }
188  
189  
190  
191 =head1 REQUIRES
192  
193 The followin must be implemented by consuming class
194  
195 =cut
196  
197  
198  
199 =head2 build_span
200  
201 =cut
202  
203 instance_method build_span (
204
205 Str :$operation_name,
206 SpanContext | Span :$child_of,
207 SpanContext :$span_context,
208 PositiveOrZeroNum :$start_time = undef,
209 HashRef[Str] :$tags = {},
210
211 7 ▶) :Return (Span) { };
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213  
214  
215 BEGIN {
216 # use Role::Tiny::With;
217 with 'OpenTracing::Interface::Tracer'
218 7 ▶ if $ENV{OPENTRACING_INTERFACE};
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"OPENTRACING_INTERFACE"} 2 5 7
219 }
220  
221  
222  
223 7 1;
↑ Summary Role/Scope.pm »