File Coverage

blib/lib/OpenTelemetry/Common.pm
Criterion Covered Total %
statement 63 68 92.6
branch 15 20 75.0
condition 2 2 100.0
subroutine 20 23 86.9
pod 5 6 83.3
total 105 119 88.2


line stmt bran cond sub pod time code
1             package
2             OpenTelemetry::Internal::Logger;
3              
4 21     21   11793 use Carp::Clan '^(?:OpenTelemetry\b|Log::Any::Proxy$)';
  21         93340  
  21         142  
5 21     21   9501 use parent 'Log::Any::Adapter::Stderr';
  21         4621  
  21         138  
6              
7             foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) {
8 21     21   289735 no strict 'refs';
  21         66  
  21         5563  
9             my $method_level = Log::Any::Adapter::Util::numeric_level($method);
10             *{$method} = sub {
11 0     0   0 my ( $self, $text ) = @_;
        0      
12 0 0       0 return if $method_level > $self->{log_level};
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     0   0 local $SIG{__WARN__} = sub { warn shift =~ s/^.*?: //r };
  0         0  
18              
19 0         0 carp "$text";
20             };
21             }
22              
23             package
24             OpenTelemetry::Common;
25              
26             # ABSTRACT: Utility package with shared functions for OpenTelemetry
27              
28             our $VERSION = '0.033';
29              
30 21     21   152 use strict;
  21         49  
  21         758  
31 21     21   133 use warnings;
  21         78  
  21         1348  
32 21     21   456 use experimental 'signatures';
  21         2977  
  21         151  
33              
34 21     21   16682 use Bytes::Random::Secure ();
  21         312755  
  21         1272  
35 21     21   197 use List::Util qw( any first );
  21         41  
  21         1848  
36 21     21   7855 use OpenTelemetry::Constants qw( INVALID_TRACE_ID INVALID_SPAN_ID );
  21         65  
  21         214  
37 21     21   30354 use Ref::Util qw( is_arrayref is_hashref );
  21         59532  
  21         2645  
38 21     21   176 use Time::HiRes qw( clock_gettime CLOCK_MONOTONIC );
  21         60  
  21         245  
39              
40 21     21   2706 use parent 'Exporter';
  21         47  
  21         203  
41              
42             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 21     21   2366 use Log::Any;
  21         47  
  21         239  
52             my $logger = Log::Any->get_logger(
53             category => 'OpenTelemetry',
54             $ENV{LOG_ANY_DEFAULT_ADAPTER} ? () : (
55             default_adapter => [
56             '+OpenTelemetry::Internal::Logger',
57             log_level => $ENV{OTEL_PERL_INTERNAL_LOG_LEVEL} // 'warn',
58             ],
59             ),
60             );
61              
62             # Undocumented because this is really only for internal use
63 56     56 0 185 sub internal_logger { $logger }
64              
65             sub timeout_timestamp :prototype() {
66 3     3 1 336365 clock_gettime CLOCK_MONOTONIC;
67             }
68              
69 4     4 1 1941 sub maybe_timeout ( $timeout = undef, $start = undef ) {
  4         11  
  4         7  
  4         6  
70 4 100       21 return unless defined $timeout;
71              
72 2   100     11 $timeout -= ( timeout_timestamp - ( $start // 0 ) );
73              
74 2 100       29 $timeout > 0 ? $timeout : 0;
75             }
76              
77             # As per https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md
78 78     78 1 5940 sub config ( @keys ) {
  78         147  
  78         97  
79 78 50       201 return unless @keys;
80              
81 163 100   163   528 my ($value) = first { defined && length } @ENV{
82 78         354 map { 'OTEL_PERL_' . $_, 'OTEL_' . $_ } @keys
  85         556  
83             };
84              
85 78 100       309 return $value unless defined $value;
86              
87 72 100       517 $value =~ /^true$/i ? 1 : $value =~ /^false$/i ? 0 : $value;
    100          
88             }
89              
90             # Trace functions
91             sub generate_trace_id {
92 4     4 1 3391 while (1) {
93 4         21 my $id = Bytes::Random::Secure::random_bytes 16;
94 4 50       1693 return $id unless $id eq INVALID_TRACE_ID;
95             }
96             }
97              
98             sub generate_span_id {
99 4     4 1 3684 while (1) {
100 4         13 my $id = Bytes::Random::Secure::random_bytes 8;
101 4 50       195 return $id unless $id eq INVALID_SPAN_ID;
102             }
103             }
104              
105             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             1;