SDK/Trace/TracerProvider.pm

filter: Statementstmt 72.7% 88 / 121 Branchbran 21.4% 3 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 21 / 28 Podpod 100% 4 / 4 totaltotal 67.4% 116 / 172 CC 11 SCAR 14.6
CC
11
Cov
66%
tracer
12.0
shutdown
12.0
force_flush
12.0
CRAP 4.3
« SDK/Trace/Tracer.pm ↑ Summary Processor/Batch.pm »
1 4 ▶use Object::Pad ':experimental(init_expr)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2 # ABSTRACT: A TracerProvider for the OpenTelemetry SDK
3  
4 package OpenTelemetry::SDK::Trace::TracerProvider;
5  
6 4 our $VERSION = '0.101';
7  
8 4 ▶use OpenTelemetry;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 class OpenTelemetry::SDK::Trace::TracerProvider :isa(OpenTelemetry::Trace::TracerProvider) {
11 4 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶ use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶ use List::Util 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶ use Mutex;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶ use OpenTelemetry::Common qw( config timeout_timestamp maybe_timeout );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶ use OpenTelemetry::Constants -export;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶ use OpenTelemetry::Propagator::TraceContext::TraceFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶ use OpenTelemetry::SDK::InstrumentationScope;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶ use OpenTelemetry::SDK::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶ use OpenTelemetry::SDK::Trace::Sampler::AlwaysOff;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶ use OpenTelemetry::SDK::Trace::Sampler::AlwaysOn;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ use OpenTelemetry::SDK::Trace::Sampler::ParentBased;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶ use OpenTelemetry::SDK::Trace::Sampler::TraceIDRatioBased;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ use OpenTelemetry::SDK::Trace::Span;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶ use OpenTelemetry::SDK::Trace::SpanLimits;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶ use OpenTelemetry::SDK::Trace::Tracer;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶ use OpenTelemetry::Trace::SpanContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 field $sampler :param = undef;
30 4 field $id_generator :param = 'OpenTelemetry::Trace';
31 4 field $span_limits :param //= OpenTelemetry::SDK::Trace::SpanLimits->new;
32 4 field $resource :param //= OpenTelemetry::SDK::Resource->new;
33 4 field $stopped;
34 4 field %registry;
35 4 field @processors;
36  
37 field $lock //= Mutex->new;
38 4 field $registry_lock //= Mutex->new;
39  
40 4 my $logger = OpenTelemetry::Common::internal_logger;
41  
42 ADJUST {
43 try {
44 for ( config('TRACES_SAMPLER') // 'parentbased_always_on' ) {
45 $sampler //= OpenTelemetry::SDK::Trace::Sampler::AlwaysOn->new
46 if $_ eq 'always_on';
47  
48 $sampler //= OpenTelemetry::SDK::Trace::Sampler::AlwaysOff->new
49 if $_ eq 'always_off';
50  
51 $sampler //= OpenTelemetry::SDK::Trace::Sampler::TraceIDRatioBased->new(
52 ratio => config('TRACES_SAMPLER_ARG') // 1,
53 ) if $_ eq 'traceidratio';
54  
55 $sampler //= OpenTelemetry::SDK::Trace::Sampler::ParentBased->new(
56 root => OpenTelemetry::SDK::Trace::Sampler::AlwaysOn->new,
57 ) if $_ eq 'parentbased_always_on';
58  
59 $sampler //= OpenTelemetry::SDK::Trace::Sampler::ParentBased->new(
60 root => OpenTelemetry::SDK::Trace::Sampler::AlwaysOff->new,
61 ) if $_ eq 'parentbased_always_off';
62  
63 $sampler //= OpenTelemetry::SDK::Trace::Sampler::ParentBased->new(
64 root => OpenTelemetry::SDK::Trace::Sampler::TraceIDRatioBased->new(
65 ratio => config('TRACES_SAMPLER_ARG') // 1,
66 ),
67 ) if $_ eq 'parentbased_traceidratio';
68 }
69 }
70 catch ($e) {
71 my $default = OpenTelemetry::SDK::Trace::Sampler::ParentBased->new(
72 root => OpenTelemetry::SDK::Trace::Sampler::AlwaysOn->new,
73 );
74  
75 OpenTelemetry->handle_error(
76 exception => $e,
77 message => 'installing default sampler ' . $default->description,
78 );
79  
80 $sampler = $default;
81 }
82 }
83  
84 4 method $create_span (%args) {
85 my %span = %args{qw( parent name kind start scope links resource )};
86  
87 $span{attribute_count_limit} = $span_limits->attribute_count_limit;
88 $span{attribute_length_limit} = $span_limits->attribute_length_limit;
89  
90 my $parent_span_context = OpenTelemetry::Trace
91 ->span_from_context( $span{parent} )->context;
92  
93 my $trace_id = $parent_span_context->valid
94 ? $parent_span_context->trace_id
95 : $id_generator->generate_trace_id;
96  
97 my $result = $sampler->should_sample(
98 trace_id => $trace_id,
99 context => $args{parent},
100 name => $span{name},
101 kind => $span{kind},
102 attributes => $args{attributes},
103 links => $span{links},
104 );
105  
106 $span{attributes} = {
107 %{ $args{attributes} // {} },
108 %{ $result->attributes },
109 };
110  
111 my $span_id = $id_generator->generate_span_id;
112  
113 if ( $result->recording && !$stopped ) {
114 my $flags = $result->sampled
115 ? OpenTelemetry::Propagator::TraceContext::TraceFlags->new(1)
116 : OpenTelemetry::Propagator::TraceContext::TraceFlags->new(0);
117  
118 my $context = OpenTelemetry::Trace::SpanContext->new(
119 trace_id => $trace_id,
120 span_id => $span_id,
121 trace_flags => $flags,
122 trace_state => $result->trace_state,
123 );
124  
125 $span{context} = $context;
126 $span{processors} = [ @processors ];
127  
128 return OpenTelemetry::SDK::Trace::Span->new(%span);
129 }
130  
131 OpenTelemetry::Trace->non_recording_span(
132 OpenTelemetry::Trace::SpanContext->new(
133 trace_id => $trace_id,
134 span_id => $span_id,
135 trace_state => $result->trace_state,
136 )
137 );
138 4 }
139  
140 0 ▶ method tracer (%args) {
Subroutine tracer: not called CC=3 SCAR=24.8
Pod documented
141 0 my %defaults;
142  
143 0 $defaults{scope} = do {
144 # If no name is provided, we get it from the caller
145 # This has to override the version, since the version
146 # only makes sense for the name
147 0 ▶ $args{name} || do {
Branch: 1 branch
BranchTrueFalseTotal
$args{"name"} or do { ($args{"name"}) = caller; $args{"version"} = $args{"name"}->VERSION } 0 0 0
148 0 ( $args{name} ) = caller;
149 0 $args{version} = $args{name}->VERSION;
150 };
151  
152 OpenTelemetry::SDK::InstrumentationScope
153 0 ->new( %args{qw( name version attributes )} );
154 };
155  
156 $defaults{resource} = $args{schema_url}
157 ? $resource->merge(
158 OpenTelemetry::SDK::Resource->empty(
159 schema_url => $args{schema_url}
160 0 ▶ ),
Branch: 1 branch
BranchTrueFalseTotal
$args{'schema_url'} ? : 0 0 0
161 )
162 : $resource;
163  
164 0 ▶ $registry_lock->enter( sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
165 my $key
166 = $defaults{scope}->to_string
167 . '-'
168 0 . $defaults{resource}->schema_url;
169  
170 $registry{$key} //= OpenTelemetry::SDK::Trace::Tracer->new(
171 0 ▶ span_creator => sub { $self->$create_span( @_, %defaults ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 ▶ );
Condition0%0/3
$registry{$key} //= "OpenTelemetry::SDK::Trace::Tracer"->new("span_creator", sub { $self->$create_span(@_, %defaults); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$registry{$key} //= "OpenTelemetry::SDK::Trace::Tracer"->new("span_creator", sub { $self->$create_span(@_, %defaults); } )
$registry{$key} "OpenTelemetry::SDK::Trace::Tracer"->new("span_creator", sub { $self->$create_span(@_, %defaults); } )
Truth table0%0/3
A //= B
A= $registry{$key}B= "OpenTelemetry::SDK::Trace::Tracer"->new("span_creator", sub { $self->$create_span(@_, %defaults); } )
ABresult
1X1
011
000
173 0 });
174 4 }
175  
176 method $atomic_call_on_processors ( $method, $timeout ) {
177 my $start = timeout_timestamp;
178  
179 my $result = EXPORT_RESULT_SUCCESS;
180  
181 for my $processor ( @processors ) {
182 my $remaining = maybe_timeout $timeout, $start;
183  
184 if ( $timeout && ! $remaining ) {
185 $result = EXPORT_RESULT_TIMEOUT;
186 last;
187 }
188  
189 my $res = $processor->$method($remaining)->get;
190 $result = $res if $res > $result;
191 }
192  
193 return $result;
194 4 }
195  
196 0 ▶ async method shutdown ( $timeout = undef ) {
Subroutine shutdown: not called CC=3 SCAR=24.8
Pod documented
197 0 ▶ return EXPORT_RESULT_SUCCESS if $stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 0 0 0
198  
199 $lock->enter(
200 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 0 $stopped = 1;
202 0 $self->$atomic_call_on_processors( shutdown => $timeout );
203 }
204 0 );
205 4 }
206  
207 0 ▶ async method force_flush ( $timeout = undef ) {
Subroutine force_flush: not called CC=3 SCAR=24.8
Pod documented
208 0 ▶ return EXPORT_RESULT_SUCCESS if $stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 0 0 0
209  
210 $lock->enter(
211 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 $self->$atomic_call_on_processors( force_flush => $timeout );
213 }
214 0 );
215 4 }
216  
217  
218 2 ▶ method add_span_processor ($processor) {
Subroutine add_span_processor: called CC=1 SCAR=0.1
Pod documented
219 2 ▶ $lock->enter( sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
220 2 ▶ return $logger->warn('Attempted to add a span processor to a TracerProvider after shutdown')
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 0 2 2
221 if $stopped;
222  
223 2 my $candidate = ref $processor;
224  
225 2 ▶ return $logger->warn("Attempted to add a $candidate object as a span processor to a TracerProvider, but it does not do the OpenTelemetry::Trace::Span::Processor")
Branch: 1 branch
BranchTrueFalseTotal
unless $processor->DOES("OpenTelemetry::Trace::Span::Processor") 0 2 2
226 unless $processor->DOES('OpenTelemetry::Trace::Span::Processor');
227  
228 0 my %seen = map { ref, 1 } @processors;
229  
230 return $logger->warn("Attempted to add a $candidate span processor to a TracerProvider more than once")
231 2 ▶ if $seen{$candidate};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$candidate} 0 2 2
232  
233 2 push @processors, $processor;
234 2 });
235  
236 2 $self;
237 4 }
238 4 }
« SDK/Trace/Tracer.pm ↑ Summary Processor/Batch.pm »