OpenTelemetry/Common.pm

filter: Statementstmt 93.0% 67 / 72 Branchbran 72.7% 16 / 22 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.9% 20 / 22 Podpod 83.3% 5 / 6 totaltotal 87.5% 112 / 128 CC 14 SCAR 10.4
CC
14
Cov
88%
maybe_timeout
6.0
config
5.0
__ANON__
4.7
CRAP 2.8
« OpenTelemetry/Instrumentation/LWP/UserAgent.pm ↑ Summary OpenTelemetry/Baggage.pm »
1 package
2 OpenTelemetry::Internal::Logger;
3  
4 20 ▶use Carp::Clan '^(?:OpenTelemetry\b|Log::Any::Proxy$)';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use parent 'Log::Any::Adapter::Stderr';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) {
8 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 180 my $method_level = Log::Any::Adapter::Util::numeric_level($method);
10 180 ▶ *{$method} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
11 0 my ( $self, $text ) = @_;
12 0 ▶ return if $method_level > $self->{log_level};
Branch: 1 branch
BranchTrueFalseTotal
if $method_level > $self->{"log_level"} 0 0 0
13  
14 # NOTE: We are using Carp::Clan so we carp from non-OTel
15 # classes, but Carp::Clan for some reason likes to prepend
16 # sub names, which we don't want
17 0 ▶ local $SIG{__WARN__} = sub { warn shift =~ s/^.*?: //r };
Subroutine __ANON__: not called CC=1 SCAR=6.9
18  
19 0 carp "$text";
20 180 };
21 }
22  
23 package
24 OpenTelemetry::Common;
25  
26 # ABSTRACT: Utility package with shared functions for OpenTelemetry
27  
28 20 our $VERSION = '0.040';
29  
30 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 20 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 20 ▶use Crypt::SysRandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 20 ▶use List::Util qw( any first );
Subroutine BEGIN: called CC=1 SCAR=0.0
36 20 ▶use OpenTelemetry::Constants qw( INVALID_TRACE_ID INVALID_SPAN_ID );
Subroutine BEGIN: called CC=1 SCAR=0.0
37 20 ▶use Ref::Util qw( is_arrayref is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 20 ▶use Time::HiRes qw( clock_gettime CLOCK_MONOTONIC );
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 20 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 20 our @EXPORT_OK = qw(
43 config
44 generate_span_id
45 generate_trace_id
46 maybe_timeout
47 timeout_timestamp
48 internal_logger
49 );
50  
51 20 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 my $logger = Log::Any->get_logger(
53 category => 'OpenTelemetry',
54 $ENV{LOG_ANY_DEFAULT_ADAPTER} ? () : (
55 default_adapter => [
56 '+OpenTelemetry::Internal::Logger',
57 20 ▶ log_level => $ENV{OTEL_PERL_INTERNAL_LOG_LEVEL} // 'warn',
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'LOG_ANY_DEFAULT_ADAPTER'} ? : 0 20 20
Condition50%1/2
$ENV{"OTEL_PERL_INTERNAL_LOG_LEVEL"} // "warn"
l!l
020
MC/DC0%0/1
$ENV{"OTEL_PERL_INTERNAL_LOG_LEVEL"} // "warn"
$ENV{"OTEL_PERL_INTERNAL_LOG_LEVEL"}
Truth table50%1/2
A
A= $ENV{"OTEL_PERL_INTERNAL_LOG_LEVEL"}
Aresult
11
00
58 ],
59 ),
60 );
61  
62 # Undocumented because this is really only for internal use
63 54 ▶sub internal_logger { $logger }
Subroutine internal_logger: called CC=1 SCAR=0.0
Pod undocumented
64  
65 3 ▶sub timeout_timestamp :prototype() {
Subroutine timeout_timestamp: called CC=1 SCAR=0.0
Pod documented
66 3 clock_gettime CLOCK_MONOTONIC;
67 }
68  
69 4 ▶sub maybe_timeout ( $timeout = undef, $start = undef ) {
Subroutine maybe_timeout: called CC=6 SCAR=17.9
Pod documented
70 4 ▶ return unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 2 2 4
71  
72 2 ▶ $timeout -= ( timeout_timestamp - ( $start // 0 ) );
Condition100%2/2
$start // 0
l!l
11
MC/DC100%1/1
$start // 0
$start
Truth table100%2/2
A
A= $start
Aresult
11
00
73  
74 2 ▶ $timeout > 0 ? $timeout : 0;
Branch: 1 branch
BranchTrueFalseTotal
$timeout > 0 ? : 1 1 2
75 }
76  
77 # As per https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md
78 78 ▶sub config ( @keys ) {
Subroutine config: called CC=5 SCAR=16.1
Pod documented
79 78 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 78 78
80  
81 163 ▶ my ($value) = first { defined && length } @ENV{
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 74 89 163
82 78 map { 'OTEL_PERL_' . $_, 'OTEL_' . $_ } @keys
83 };
84  
85 78 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 6 72 78
86  
87 72 ▶ $value =~ /^true$/i ? 1 : $value =~ /^false$/i ? 0 : $value;
Branch: 2 branches
BranchTrueFalseTotal
$value =~ /^true$/i ? : 1 71 72
$value =~ /^false$/i ? : 1 70 71
88 }
89  
90 # Trace functions
91 4 ▶sub generate_trace_id {
Subroutine generate_trace_id: called CC=2 SCAR=7.1
Pod documented
92 4 while (1) {
93 4 my $id = Crypt::SysRandom::random_bytes 16;
94 4 ▶ return $id unless $id eq INVALID_TRACE_ID;
Branch: 1 branch
BranchTrueFalseTotal
unless $id eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 4 0 4
95 }
96 }
97  
98 4 ▶sub generate_span_id {
Subroutine generate_span_id: called CC=2 SCAR=7.1
Pod documented
99 4 while (1) {
100 4 my $id = Crypt::SysRandom::random_bytes 8;
101 4 ▶ return $id unless $id eq INVALID_SPAN_ID;
Branch: 1 branch
BranchTrueFalseTotal
unless $id eq "\0\0\0\0\0\0\0\0" 4 0 4
102 }
103 }
104  
105 20 delete $OpenTelemetry::Common::{$_} for qw(
106 CLOCK_MONOTONIC
107 INVALID_SPAN_ID
108 INVALID_TRACE_ID
109 any
110 clock_gettime
111 first
112 is_arrayref
113 is_hashref
114 );
115  
116 20 1;
« OpenTelemetry/Instrumentation/LWP/UserAgent.pm ↑ Summary OpenTelemetry/Baggage.pm »