blib/lib/Lemonldap/NG/Common/Logger/Loki.pm

filter: Statementstmt 92.6% 63 / 68 Branchbran 68.7% 22 / 32 Conditioncond 39.1% 9 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 9 totaltotal 66.0% 105 / 159 CC 29 SCAR 28.9
CC
29
Cov
76%
new
29.9
log
10.4
debug
1.1
CRAP 17.9
« blib/lib/Lemonldap/NG/Common/Notifications/File.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Apache/Session/SOAP.pm »
1 package Lemonldap::NG::Common::Logger::Loki;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '2.22.0';
10  
11 2 ▶sub new {
Subroutine new: called CC=22 SCAR=34.0
Pod undocumented
12 2 my ( $class, $conf, %args ) = @_;
13 2 my $self = bless {}, $class;
14 2 ▶ my $level = $conf->{logLevel} || 'info';
Condition50%1/2
$conf->{'logLevel'} || 'info'
l!l
20
MC/DC0%0/1
$conf->{'logLevel'} || 'info'
$conf->{'logLevel'}
Truth table50%1/2
A
A= $conf->{'logLevel'}
Aresult
11
00
15 2 $self->{deferDir} = $conf->{lokiDeferDir};
16 2 ▶ $self->{url} = $conf->{lokiUrl} || 'http://localhost:3100/loki/api/v1/push';
Condition50%1/2
$conf->{'lokiUrl'} || 'http://localhost:3100/loki/api/v1/push'
l!l
02
MC/DC0%0/1
$conf->{'lokiUrl'} || 'http://localhost:3100/loki/api/v1/push'
$conf->{'lokiUrl'}
Truth table50%1/2
A
A= $conf->{'lokiUrl'}
Aresult
11
00
17 2 ▶ $self->{label} = $conf->{lokiLabel} || 'llng';
Condition50%1/2
$conf->{'lokiLabel'} || 'llng'
l!l
02
MC/DC0%0/1
$conf->{'lokiLabel'} || 'llng'
$conf->{'lokiLabel'}
Truth table50%1/2
A
A= $conf->{'lokiLabel'}
Aresult
11
00
18 2 $self->{j} = JSON->new->canonical;
19 $self->{ua} = Lemonldap::NG::Common::UserAgent->new($conf)
20 2 ▶ unless $self->{deferDir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"deferDir"} 1 1 2
21 2 ▶ $self->{instance} = $conf->{lokiInstance} || hostname;
Condition33%1/3
$conf->{'lokiInstance'} || hostname()
l!l&&r!l&&!r
020
MC/DC0%0/2
$conf->{'lokiInstance'} || hostname()
$conf->{'lokiInstance'} hostname()
Truth table33%1/3
A || B
A= $conf->{'lokiInstance'}B= hostname()
ABresult
1X1
011
000
22 2 ▶ $self->{env} = $conf->{lokiEnv} || 'prod';
Condition50%1/2
$conf->{'lokiEnv'} || 'prod'
l!l
02
MC/DC0%0/1
$conf->{'lokiEnv'} || 'prod'
$conf->{'lokiEnv'}
Truth table50%1/2
A
A= $conf->{'lokiEnv'}
Aresult
11
00
23 2 $self->{tenant} = $conf->{lokiTenant};
24 2 $self->{authorization} = $conf->{lokiAuthorization};
25 2 ▶ $self->{tenantHeader} = $conf->{lokiTenantHeader} || 'X-Scope-OrgID';
Condition50%1/2
$conf->{'lokiTenantHeader'} || 'X-Scope-OrgID'
l!l
02
MC/DC0%0/1
$conf->{'lokiTenantHeader'} || 'X-Scope-OrgID'
$conf->{'lokiTenantHeader'}
Truth table50%1/2
A
A= $conf->{'lokiTenantHeader'}
Aresult
11
00
26 2 $self->{proxy} = $conf->{lokiProxy};
27 $self->{service} =
28 $args{user}
29 ? ( $conf->{lokiUserService} || 'auth' )
30 2 ▶ : ( $conf->{lokiService} || 'llng' );
Branch: 1 branch
BranchTrueFalseTotal
$args{'user'} ? : 2 0 2
Condition25%1/4
$conf->{"lokiUserService"} or "auth"
l!l
02
$conf->{"lokiService"} or "llng"
l!l
00
MC/DC0%0/1
$conf->{"lokiUserService"} or "auth"
$conf->{"lokiUserService"}
MC/DC0%0/1
$conf->{"lokiService"} or "llng"
$conf->{"lokiService"}
Truth table50%1/2
A
A= $conf->{"lokiUserService"}
Aresult
11
00
Truth table0%0/2
A
A= $conf->{"lokiService"}
Aresult
11
00
31 2 my $show = 1;
32  
33 2 ▶ unless ( $self->{deferDir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"deferDir"}) 1 1 2
34 1 foreach (qw(url proxy)) {
35 2 ▶ if ( $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$_}) 1 1 2
36 1 my $h = eval { URI->new( $self->{$_} )->host };
37 1 ▶ die 'Bad loki' . ucfirst($_) . ' value' if $@ or !$h;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $h 0 1 1
Condition33%1/3
$@ or not $h
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or not $h
$@ not $h
Truth table33%1/3
A or not(B)
A= $@B= not $h
ABresult
1X1
011
000
38 1 ▶ $self->{host} = $h if $_ eq 'url';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "url" 1 0 1
39 }
40 }
41 }
42  
43 2 foreach (qw(error warn notice info debug)) {
44 10 ▶ if ($show) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show) { } 8 2 10
45 8 my $name = $_;
46 0 ▶ eval "sub $_ {shift->log('$name', \@_)}";
Subroutine error: not called CC=1 SCAR=0.2
Subroutine info: called CC=1 SCAR=0.2
Subroutine info: called CC=1 SCAR=0.2
Subroutine notice: called CC=1 SCAR=0.2
Subroutine notice: called CC=1 SCAR=0.2
Subroutine warn: not called CC=1 SCAR=0.2
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
47 8 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
48 }
49 else {
50 0 ▶ eval qq'sub $_ {1}';
Subroutine debug: not called CC=1 SCAR=1.2
Pod undocumented
51 }
52 10 ▶ $show = 0 if ( $level eq $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $level eq $_ 2 8 10
53 }
54 2 ▶ die "Unknown logLevel $level" if $show;
Branch: 1 branch
BranchTrueFalseTotal
if $show 0 2 2
55 2 return $self;
56 }
57  
58 6 ▶sub log {
Subroutine log: called CC=8 SCAR=23.4
Pod undocumented
59 6 my ( $self, $level, $message ) = @_;
60 my $logEntry = {
61 streams => [ {
62 stream => {
63 job => $self->{label},
64 instance => $self->{instance},
65 level => $level,
66 env => $self->{env},
67 service => $self->{service},
68 },
69 6 values =>
70 [ [ sprintf( "%19d", time * 1000000000 ), $message ] ],
71 },
72 ],
73 };
74 my $req = HTTP::Request->new(
75 POST => ( $self->{proxy} || $self->{url} ),
76 [
77 ( $self->{proxy} ? ( Host => $self->{host} ) : () ),
78 'Content-Type' => 'application/json',
79 (
80 $self->{authorization}
81 ? ( 'Authorization' => $self->{authorization} )
82 : ()
83 ),
84 (
85 $self->{tenant} ? ( $self->{tenantHeader} => $self->{tenant} )
86 : ()
87 )
88 ],
89 6 ▶ $self->{j}->encode($logEntry)
Branch: 3 branches
BranchTrueFalseTotal
$self->{'proxy'} ? : 0 6 6
$self->{'authorization'} ? : 0 6 6
$self->{'tenant'} ? : 0 6 6
Condition33%1/3
$self->{'proxy'} || $self->{'url'}
l!l&&r!l&&!r
060
MC/DC0%0/2
$self->{'proxy'} || $self->{'url'}
$self->{'proxy'} $self->{'url'}
Truth table33%1/3
A || B
A= $self->{'proxy'}B= $self->{'url'}
ABresult
1X1
011
000
90 );
91 6 ▶ if ( $self->{deferDir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'deferDir'}) { } 3 3 6
92 3 $req = $req->as_string;
93 3 ▶ if ( open my $f, '>', "$self->{deferDir}/" . time . ".$$" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $f, '>', "$self->{'deferDir'}/" . &Lemonldap::NG::Common::Logger::Loki::time() . ".${$}") { } 3 0 3
94 3 print $f $req;
95 3 close $f;
96 }
97 else {
98 0 print STDERR
99 "# UNABLE TO STORE LOKI LOG: $!, here is the content:\n$req";
100 }
101 }
102 else {
103 3 my $response = $self->{ua}->request($req);
104 3 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 3 3
105 0 print STDERR
106 "Unable to push log to loki\nMessage: $message\nResponse: "
107 . $response->status_line . "\n";
108 }
109 }
110 }
111  
112 1 1;
« blib/lib/Lemonldap/NG/Common/Notifications/File.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Apache/Session/SOAP.pm »