| 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"
MC/DC100%1/1
otel_config("LOG_LEVEL") // "info"
otel_config("LOG_LEVEL")
Truth table100%2/2
A
A= otel_config("LOG_LEVEL")
| |||||||||||||||||||||||
| 71 | 67 | ▶ | $numeric <= ( $level // $OTEL2LOG{info} ); | ||||||||||||||||||||
|
Condition66%2/3
$level // $OTEL2LOG{"info"}
MC/DC0%0/2
$level // $OTEL2LOG{"info"}
$level
$OTEL2LOG{"info"}
Truth table66%2/3
A // B
A= $levelB= $OTEL2LOG{"info"}
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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; | |||||||||||||||||||||