SDK/Trace/Span.pm

filter: Statementstmt 98.6% 141 / 143 Branchbran 85.0% 17 / 20 Conditioncond 75.0% 12 / 16 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 25 / 25 Podpod 100% 8 / 8 totaltotal 94.1% 209 / 222 CC 20 SCAR 11.9
CC
20
Cov
95%
record_exception
7.0
set_status
6.0
end
5.0
CRAP 3.3
« Processor/Batch.pm ↑ Summary SDK/Trace/Sampler/TraceIDRatioBased.pm »
1 5 ▶use Object::Pad ':experimental( init_expr mop )';
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package OpenTelemetry::SDK::Trace::Span;
4  
5 5 our $VERSION = '0.101';
6  
7 class OpenTelemetry::SDK::Trace::Span
8 :isa(OpenTelemetry::Trace::Span)
9 :does(OpenTelemetry::Attributes)
10 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶ use List::Util qw( any pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶ use Ref::Util qw( is_arrayref is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶ use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 use OpenTelemetry::Constants
16 5 ▶ -span_kind => { -as => sub { shift =~ s/^SPAN_KIND_//r } };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 5 ▶ use OpenTelemetry::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶ use OpenTelemetry::SDK::Trace::SpanLimits;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶ use OpenTelemetry::SDK::Trace::Span::Readable;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶ use OpenTelemetry::Trace::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶ use OpenTelemetry::Trace::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶ use OpenTelemetry::Trace::SpanContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶ use OpenTelemetry::Trace::Span::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶ use OpenTelemetry::Trace;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶ use isa qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 Exception::Base
29 Exception::Class::Base
30 OpenTelemetry::Trace::SpanContext
31 5 );
32  
33 5 my $logger = OpenTelemetry::Common::internal_logger;
34  
35 field $dropped_events = 0;
36 5 field $dropped_links = 0;
37 5 field $end;
38 field $kind :param = INTERNAL;
39 5 field $limits :param //= OpenTelemetry::SDK::Trace::SpanLimits->new;
40 5 field $name :param;
41 field $resource :param = undef;
42 5 field $scope :param;
43 field $start :param = undef;
44 5 field $status = OpenTelemetry::Trace::Span::Status->unset;
45 5 field @events;
46 5 field @links;
47 5 field @processors;
48 5 field $parent_span_context;
49  
50 # Internal method for adding a single link
51 #
52 # $self->$add_link({
53 # context => $valid_span_context,
54 # attributes => \%link_attributes,
55 # })
56 #
57 # Links with invalid span contexts are ignored
58 #
59 method $add_link ( $args ) {
60 return unless isa_OpenTelemetry_Trace_SpanContext($args->{context})
61 && $args->{context}->valid;
62  
63 if ( scalar @links >= $limits->link_count_limit ) {
64 $dropped_links++;
65 $logger->warn('Dropped link because it would exceed specified limit');
66 return;
67 }
68  
69 push @links, OpenTelemetry::Trace::Link->new(
70 context => $args->{context},
71 attributes => $args->{attributes},
72 attribute_count_limit => $limits->link_attribute_count_limit,
73 );
74 5 }
75  
76 ADJUSTPARAMS ( $params ) {
77 my $now = time;
78 undef $start if $start && $start > $now;
79 $start //= $now;
80  
81 $kind = INTERNAL if $kind < INTERNAL || $kind > CONSUMER;
82  
83 @processors = @{ delete $params->{processors} // [] };
84  
85 $self->$add_link($_) for @{ delete $params->{links} // [] };
86  
87 my $parent = delete $params->{parent};
88  
89 $parent_span_context
90 = OpenTelemetry::Trace->span_from_context($parent)->context;
91  
92 $_->on_start( $self, $parent ) for @processors;
93 }
94  
95 4 ▶ method set_name ( $new ) {
Subroutine set_name: called CC=3 SCAR=11.0
Pod documented
96 4 ▶ return $self unless $self->recording && $new;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recording and $new 3 1 4
Condition100%3/3
$self->recording and $new
!ll&&!rl&&r
121
MC/DC100%2/2
$self->recording and $new
$self->recording $new
Truth table100%3/3
A and B
A= $self->recordingB= $new
ABresult
0X0
100
111
97  
98 1 $name = $new;
99  
100 1 $self;
101 5 }
102  
103 2 ▶ method set_attribute ( %new ) {
Subroutine set_attribute: called CC=2 SCAR=6.9
Pod documented
104 2 ▶ unless ( $self->recording ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->recording) 1 1 2
105 1 $logger->warn('Attempted to set attributes on a span that is not recording');
106 1 return $self
107 }
108  
109 # FIXME: Ideally an overridable method from role, but that is not supported
110 1 Object::Pad::MOP::Class->for_class('OpenTelemetry::Attributes')
111 ->get_field('$attributes')
112 ->value($self)
113 ->set(%new);
114  
115 1 $self;
116 5 }
117  
118 6 ▶ method set_status ( $new, $description = undef ) {
Subroutine set_status: called CC=6 SCAR=17.9
Pod documented
119 6 ▶ return $self if !$self->recording || $status->is_ok;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->recording or $status->is_ok 2 4 6
Condition100%3/3
not $self->recording or $status->is_ok
l!l&&r!l&&!r
114
MC/DC100%2/2
not $self->recording or $status->is_ok
not $self->recording $status->is_ok
Truth table100%3/3
not(A) or B
A= not $self->recordingB= $status->is_ok
ABresult
1X1
011
000
120  
121 4 ▶ my $value = OpenTelemetry::Trace::Span::Status->new(
Condition100%2/2
$description // ""
l!l
13
MC/DC100%1/1
$description // ""
$description
Truth table100%2/2
A
A= $description
Aresult
11
00
122 code => $new,
123 description => $description // '',
124 );
125  
126 4 ▶ $status = $value unless $value->is_unset;
Branch: 1 branch
BranchTrueFalseTotal
unless $value->is_unset 3 1 4
127  
128 4 $self;
129 5 }
130  
131 9 ▶ method add_event (%args) {
Subroutine add_event: called CC=3 SCAR=11.0
Pod documented
132 9 ▶ return $self unless $self->recording;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recording 1 8 9
133  
134 8 ▶ if ( scalar @events >= $limits->event_count_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @events >= $limits->event_count_limit) 1 7 8
135 1 $dropped_events++;
136 1 $logger->warn('Dropped event because it would exceed specified limit');
137 1 return $self;
138 }
139  
140 push @events, OpenTelemetry::Trace::Event->new(
141 name => $args{name},
142 timestamp => $args{timestamp},
143 attributes => $args{attributes},
144 7 attribute_count_limit => $limits->event_attribute_count_limit,
145 attribute_length_limit => $limits->event_attribute_length_limit,
146 );
147  
148 7 $self;
149 5 }
150  
151 8 ▶ method end ( $time = undef ) {
Subroutine end: called CC=5 SCAR=16.1
Pod documented
152 # This should in theory be an atomic check. For now, to reduce
153 # the chances of it becoming a problem, we check the field
154 # directly instead of going through `recording`
155 8 ▶ return $self if defined $end;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end 1 7 8
156 7 ▶ $end = $time // time;
Condition66%2/3
$time // &OpenTelemetry::SDK::Trace::Span::time()
l!l&&r!l&&!r
250
MC/DC0%0/2
$time // &OpenTelemetry::SDK::Trace::Span::time()
$time &OpenTelemetry::SDK::Trace::Span::time()
Truth table66%2/3
A // B
A= $timeB= &OpenTelemetry::SDK::Trace::Span::time()
ABresult
1X1
011
000
157  
158 7 $_->on_end($self) for @processors;
159  
160 7 $self;
161 5 }
162  
163 3 ▶ method record_exception ( $exception, %attributes ) {
Subroutine record_exception: called CC=6 SCAR=19.5
Pod documented
164 3 ▶ return $self unless $self->recording;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recording 0 3 3
165  
166 3 my ( $message, $stacktrace );
167 3 ▶ if ( isa_Exception_Class_Base $exception ) {
Branch: 1 branch
BranchTrueFalseTotal
if (isa_Exception_Class_Base($exception)) { } 0 3 3
168 0 $message = $exception->message;
169 0 $stacktrace = $exception->trace->as_string;
170 }
171 else {
172 # This should cover the following common exceptions:
173 # * Catalyst::Exception::Basic
174 # * Class::Throwable
175 # * Dancer::Exception::Base
176 # * Exception::Base
177 # * Mojo::Exception
178 # * Throwable::Error
179 # * X::Tiny
180 # * plain die strings
181  
182 3 local $ENV{MOJO_EXCEPTION_VERBOSE} = 1;
183 3 local $Class::Throwable::DEFAULT_VERBOSITY = 2;
184  
185 3 ( $message, $stacktrace ) = split /\n/, "$exception", 2;
186  
187 3 ▶ $stacktrace //= $exception->get_caller_stacktrace
Branch: 1 branch
BranchTrueFalseTotal
if isa_Exception_Base($exception) 0 3 3
Condition0%0/3
$stacktrace //= $exception->get_caller_stacktrace
l!l&&r!l&&!r
000
MC/DC0%0/2
$stacktrace //= $exception->get_caller_stacktrace
$stacktrace $exception->get_caller_stacktrace
Truth table0%0/3
A //= B
A= $stacktraceB= $exception->get_caller_stacktrace
ABresult
1X1
011
000
188 if isa_Exception_Base $exception;
189 }
190  
191 3 ▶ $self->add_event(
Condition100%2/2
ref $exception || 'string'
l!l
12
MC/DC100%1/1
ref $exception || 'string'
ref $exception
Truth table100%2/2
A
A= ref $exception
Aresult
11
00
192 name => 'exception',
193 attributes => {
194 'exception.type' => ref $exception || 'string',
195 'exception.message' => $message,
196 'exception.stacktrace' => $stacktrace,
197 %attributes,
198 }
199 );
200 5 }
201  
202 30 ▶ method recording () { ! defined $end }
Subroutine recording: called CC=1 SCAR=0.0
Pod documented
203  
204 24 ▶ method snapshot () {
Subroutine snapshot: called CC=1 SCAR=0.0
Pod documented
205 24 OpenTelemetry::SDK::Trace::Span::Readable->new(
206 attributes => $self->attributes,
207 context => $self->context,
208 dropped_events => $dropped_events,
209 dropped_links => $dropped_links,
210 end_timestamp => $end,
211 events => [ @events ],
212 instrumentation_scope => $scope,
213 kind => $kind,
214 links => [ @links ],
215 name => $name,
216 parent_span_id => $parent_span_context->span_id,
217 resource => $resource,
218 start_timestamp => $start,
219 status => $status,
220 );
221 5 }
222 5 }
« Processor/Batch.pm ↑ Summary SDK/Trace/Sampler/TraceIDRatioBased.pm »