Log/Any/Adapter/OpenTelemetry.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 75.0% 6 / 8 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 1 totaltotal 90.4% 76 / 84 CC 8 SCAR 10.2
CC
8
Cov
94%
structured
6.1
__ANON__
3.0
__ANON__
2.0
CRAP 2.8
« OpenTelemetry/Baggage.pm ↑ Summary OpenTelemetry/Trace.pm »
1 package Log::Any::Adapter::OpenTelemetry;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.040';
8  
9 1 ▶use Log::Any::Adapter::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OpenTelemetry qw( otel_config otel_span_from_context otel_logger_provider );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Ref::Util 'is_hashref';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use OpenTelemetry::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 LOG_LEVEL_TRACE
16 LOG_LEVEL_DEBUG
17 LOG_LEVEL_INFO
18 LOG_LEVEL_WARN
19 LOG_LEVEL_ERROR
20 LOG_LEVEL_FATAL
21 1 );
22  
23 1 ▶use base 'Log::Any::Adapter::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 my %LOG2OTEL = (
26 trace => LOG_LEVEL_TRACE,
27 debug => LOG_LEVEL_DEBUG,
28 info => LOG_LEVEL_INFO,
29 warn => LOG_LEVEL_WARN,
30 error => LOG_LEVEL_ERROR,
31 fatal => LOG_LEVEL_FATAL,
32 );
33  
34 1 my %OTEL2LOG = (
35 trace => 8,
36 debug => 7,
37 info => 6,
38 warn => 4,
39 error => 3,
40 fatal => 2,
41 );
42  
43 sub init ( $self, @ ) {
44 # FIXME: It would be good to get a logger early and cache
45 # it for eventual calls. However, this suffers from the same
46 # issue with caching tracers that is documented in the POD
47 # for OpenTelemetry::Trace::Tracer: namely, that if we get
48 # the no-op logger before we've set up a real logger provider
49 # that can generate real loggers, we'll be stuck with a no-op.
50 # It might be that we need to revisit the proxy classes removed
51 # in d9e321bd1bf65d510b12ef34fe2b5a0c51da0bf2, although the
52 # rationale for why they were removed is still sound. We'd just
53 # have to come up with a way to make sure its delegate continues
54 # to point to the right place even if the tracer provider changes
55 # $self->{logger} = otel_logger_provider->logger;
56 }
57  
58 1 for my $method ( Log::Any::Adapter::Util::logging_methods() ) {
59 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 0 ▶ *$method = sub ( $self, @args) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 $self->structured( $method, $self->category, @args );
62 9 };
63 }
64  
65 1 for my $method ( Log::Any::Adapter::Util::detection_methods() ) {
66 9 my $numeric = Log::Any::Adapter::Util::numeric_level( $method =~ s/^is_//r );
67  
68 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 67 ▶ *$method = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
70 67 ▶ my $level = $OTEL2LOG{ lc( otel_config('LOG_LEVEL') // 'info' ) };
Condition100%2/2
otel_config("LOG_LEVEL") // "info"
l!l
634
MC/DC100%1/1
otel_config("LOG_LEVEL") // "info"
otel_config("LOG_LEVEL")
Truth table100%2/2
A
A= otel_config("LOG_LEVEL")
Aresult
11
00
71 67 ▶ $numeric <= ( $level // $OTEL2LOG{info} );
Condition66%2/3
$level // $OTEL2LOG{"info"}
l!l&&r!l&&!r
5890
MC/DC0%0/2
$level // $OTEL2LOG{"info"}
$level $OTEL2LOG{"info"}
Truth table66%2/3
A // B
A= $levelB= $OTEL2LOG{"info"}
ABresult
1X1
011
000
72 9 };
73 }
74  
75 47 ▶sub structured ( $self, $method, $category, @parts ) {
Subroutine structured: called CC=6 SCAR=18.0
Pod undocumented
76 47 my $level = $method;
77 47 for ($level) {
78 47 s/(?:emergency|alert|critical)/fatal/;
79 47 s/notice/info/;
80 47 s/warning/warn/;
81 }
82  
83 # FIXME: This is a little finicky. The aim is for the first
84 # argument to be the body (even if it is structured), and
85 # anything else gets put into the attributes. If the log
86 # comes with structured data that is not a hash, we put it
87 # under a `payload` key. Maybe this can be simplified to
88 # always put the data under a given key, but then we add
89 # data to the arguably common operation of attaching a hash.
90 47 my %args = ( body => shift @parts );
91  
92 47 ▶ $args{attributes} = @parts == 1
Branch: 4 branches
BranchTrueFalseTotal
if @parts 2 45 47
@parts == 1 ? : 1 1 2
is_hashref($parts[0]) ? : 0 1 1
@parts % 2 ? : 0 1 1
93 ? is_hashref $parts[0]
94 ? $parts[0] : { payload => $parts[0] }
95 : @parts % 2
96 ? { payload => \@parts } : { @parts }
97 if @parts;
98  
99 otel_logger_provider->logger->emit_record(
100 timestamp => time,
101 severity_text => $method,
102 47 severity_number => 0+$LOG2OTEL{$level},
103 %args,
104 );
105 }
106  
107 1 1;
« OpenTelemetry/Baggage.pm ↑ Summary OpenTelemetry/Trace.pm »