« 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'
MC/DC0%0/1
$conf->{'logLevel'} || 'info'
$conf->{'logLevel'}
Truth table50%1/2
A
A= $conf->{'logLevel'}
| |||||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$conf->{'lokiUrl'} || 'http://localhost:3100/loki/api/v1/push'
$conf->{'lokiUrl'}
Truth table50%1/2
A
A= $conf->{'lokiUrl'}
| |||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | $self->{label} = $conf->{lokiLabel} || 'llng'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'lokiLabel'} || 'llng'
MC/DC0%0/1
$conf->{'lokiLabel'} || 'llng'
$conf->{'lokiLabel'}
Truth table50%1/2
A
A= $conf->{'lokiLabel'}
| |||||||||||||||||||||||||||||||||||||
| 18 | 2 | $self->{j} = JSON->new->canonical; | |||||||||||||||||||||||||||||||||||
| 19 | $self->{ua} = Lemonldap::NG::Common::UserAgent->new($conf) | ||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | unless $self->{deferDir}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | $self->{instance} = $conf->{lokiInstance} || hostname; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$conf->{'lokiInstance'} || hostname()
MC/DC0%0/2
$conf->{'lokiInstance'} || hostname()
$conf->{'lokiInstance'}
hostname()
Truth table33%1/3
A || B
A= $conf->{'lokiInstance'}B= hostname()
| |||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | $self->{env} = $conf->{lokiEnv} || 'prod'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'lokiEnv'} || 'prod'
MC/DC0%0/1
$conf->{'lokiEnv'} || 'prod'
$conf->{'lokiEnv'}
Truth table50%1/2
A
A= $conf->{'lokiEnv'}
| |||||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$conf->{'lokiTenantHeader'} || 'X-Scope-OrgID'
$conf->{'lokiTenantHeader'}
Truth table50%1/2
A
A= $conf->{'lokiTenantHeader'}
| |||||||||||||||||||||||||||||||||||||
| 26 | 2 | $self->{proxy} = $conf->{lokiProxy}; | |||||||||||||||||||||||||||||||||||
| 27 | $self->{service} = | ||||||||||||||||||||||||||||||||||||
| 28 | $args{user} | ||||||||||||||||||||||||||||||||||||
| 29 | ? ( $conf->{lokiUserService} || 'auth' ) | ||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | : ( $conf->{lokiService} || 'llng' ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$conf->{"lokiUserService"} or "auth"
$conf->{"lokiService"} or "llng"
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"}
Truth table0%0/2
A
A= $conf->{"lokiService"}
| |||||||||||||||||||||||||||||||||||||
| 31 | 2 | my $show = 1; | |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | unless ( $self->{deferDir} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 34 | 1 | foreach (qw(url proxy)) { | |||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | if ( $self->{$_} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 36 | 1 | my $h = eval { URI->new( $self->{$_} )->host }; | |||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | die 'Bad loki' . ucfirst($_) . ' value' if $@ or !$h; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $h
MC/DC0%0/2
$@ or not $h
$@
not $h
Truth table33%1/3
A or not(B)
A= $@B= not $h
| |||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $self->{host} = $h if $_ eq 'url'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | 2 | foreach (qw(error warn notice info debug)) { | |||||||||||||||||||||||||||||||||||
| 44 | 10 | ▶ | if ($show) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | die "Unknown logLevel $level" if $show; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->{'proxy'} || $self->{'url'}
MC/DC0%0/2
$self->{'proxy'} || $self->{'url'}
$self->{'proxy'}
$self->{'url'}
Truth table33%1/3
A || B
A= $self->{'proxy'}B= $self->{'url'}
| |||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||
| 91 | 6 | ▶ | if ( $self->{deferDir} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 92 | 3 | $req = $req->as_string; | |||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | if ( open my $f, '>', "$self->{deferDir}/" . time . ".$$" ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||