blib/lib/Dancer2/Logger/Fluent.pm

filter: Statementstmt 85.1% 69 / 81 Branchbran 20.8% 5 / 24 Conditioncond 22.5% 7 / 31 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 57.7% 97 / 168 CC 26 SCAR 34.4
CC
26
Cov
60%
_fluent
94.8
log
20.7
_connect
6.6
CRAP 31.1
↑ Summary
1 package Dancer2::Logger::Fluent;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.06';
6  
7 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Fluent::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 with 'Dancer2::Core::Role::Logger';
18  
19 2 has tag_prefix => (
20 is => 'lazy',
21 isa => Str,
22 );
23  
24 2 has host => (
25 is => 'ro',
26 isa => Str,
27 );
28  
29 2 has port => (
30 is => 'ro',
31 isa => Str,
32 );
33  
34 2 has timeout => (
35 is => 'ro',
36 isa => Num,
37 predicate => '_has_timeout',
38 );
39  
40 2 has socket => (
41 is => 'ro',
42 isa => Str,
43 );
44  
45 2 has prefer_integer => (
46 is => 'ro',
47 isa => Str,
48 predicate => '_has_prefer_integer',
49 );
50  
51 2 has event_time => (
52 is => 'ro',
53 isa => Bool,
54 );
55  
56 2 has buffer_limit => (
57 is => 'ro',
58 isa => Int,
59 predicate => '_has_buffer_limit',
60 );
61  
62 2 has buffer_overflow_handler => (
63 is => 'ro',
64 isa => CodeRef,
65 predicate => '_has_buffer_overflow_handler',
66 );
67  
68 2 has truncate_buffer_at_overflow => (
69 is => 'ro',
70 isa => Bool,
71 );
72  
73 1 ▶sub _build_tag_prefix {
Subroutine _build_tag_prefix: called CC=4 SCAR=18.9
74 1 my $self = shift;
75 1 ▶ return $self->{tag_prefix} || $self->app_name || $ENV{DANCER_APPDIR} || basename($0);
Condition33%3/9
$self->{'tag_prefix'} || $self->app_name || $ENV{'DANCER_APPDIR'} || basename($0)
l!l&&r!l&&!r
100
$self->{"tag_prefix"} or $self->app_name or $ENV{"DANCER_APPDIR"}
l!l&&r!l&&!r
100
$self->{"tag_prefix"} or $self->app_name
l!l&&r!l&&!r
010
MC/DC0%0/4
$self->{'tag_prefix'} || $self->app_name || $ENV{'DANCER_APPDIR'} || basename($0)
$self->{"tag_prefix"} $self->app_name $ENV{"DANCER_APPDIR"} basename($0)
Truth table20%1/5
A or B or C || D
A= $self->{"tag_prefix"}B= $self->app_nameC= $ENV{"DANCER_APPDIR"}D= basename($0)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
76 }
77  
78 2 ▶sub _connect {
Subroutine _connect: called CC=5 SCAR=18.9
79 2 my $self = shift;
80 2 ▶ return defined $self->socket
Branch: 2 branches
BranchTrueFalseTotal
defined $self->socket ? : 0 2 2
$self->_has_timeout ? : 0 2 2
Condition50%2/4
$self->host || '127.0.0.1'
l!l
02
$self->port || 24224
l!l
02
MC/DC0%0/1
$self->host || '127.0.0.1'
$self->host
MC/DC0%0/1
$self->port || 24224
$self->port
Truth table50%1/2
A
A= $self->host
Aresult
11
00
Truth table50%1/2
A
A= $self->port
Aresult
11
00
81 ? IO::Socket::UNIX->new( Peer => $self->socket )
82 : IO::Socket::INET->new(
83 PeerAddr => $self->host || '127.0.0.1',
84 PeerPort => $self->port || 24224,
85 Proto => 'tcp',
86 Timeout => $self->_has_timeout ? $self->timeout : 3.0,
87 ReuseAddr => 1,
88 );
89 }
90  
91 2 ▶sub _fluent {
Subroutine _fluent: called CC=11 SCAR=45.5
92 2 my $self = shift;
93  
94 2 ▶ return unless $self->_connect;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_connect 2 0 2
95  
96 0 ▶ unless ( exists $self->{_fluent} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_fluent"}) 0 0 0
97 $self->{_fluent} = Fluent::Logger->new(
98 host => $self->host || '127.0.0.1',
99 port => $self->port || 24224,
100 timeout => $self->_has_timeout ? $self->timeout : 3.0,
101 socket => $self->socket,
102 prefer_integer => $self->_has_prefer_integer ? $self->prefer_integer : 1,
103 event_time => $self->event_time || 0,
104 buffer_limit => $self->_has_buffer_limit ? $self->buffer_limit : 8388608,
105 0 ▶ buffer_overflow_handler => $self->_has_buffer_overflow_handler ? $self->buffer_overflow_handler : sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 ▶ truncate_buffer_at_overflow => $self->truncate_buffer_at_overflow || 0,
Branch: 4 branches
BranchTrueFalseTotal
$self->_has_timeout ? : 0 0 0
$self->_has_prefer_integer ? : 0 0 0
$self->_has_buffer_limit ? : 0 0 0
$self->_has_buffer_overflow_handler ? : 0 0 0
Condition0%0/8
$self->host || '127.0.0.1'
l!l
00
$self->port || 24224
l!l
00
$self->event_time || 0
l!l
00
$self->truncate_buffer_at_overflow || 0
l!l
00
MC/DC0%0/1
$self->host || '127.0.0.1'
$self->host
MC/DC0%0/1
$self->port || 24224
$self->port
MC/DC0%0/1
$self->event_time || 0
$self->event_time
MC/DC0%0/1
$self->truncate_buffer_at_overflow || 0
$self->truncate_buffer_at_overflow
Truth table0%0/2
A
A= $self->host
Aresult
11
00
Truth table0%0/2
A
A= $self->port
Aresult
11
00
Truth table0%0/2
A
A= $self->event_time
Aresult
11
00
Truth table0%0/2
A
A= $self->truncate_buffer_at_overflow
Aresult
11
00
107 );
108 }
109  
110 0 return $self->{_fluent};
111 }
112  
113 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=16.5
114 1 my $self = shift;
115 1 ▶ return unless $self->_fluent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_fluent 1 0 1
116 0 ▶ $self->_fluent->{pending} ||= ''; # Fluent::Logger->close performs length checks without checking if value is defined first
Condition0%0/2
$self->_fluent->{"pending"} ||= ""
l!l
00
MC/DC0%0/1
$self->_fluent->{"pending"} ||= ""
$self->_fluent->{"pending"}
Truth table0%0/2
A
A= $self->_fluent->{"pending"}
Aresult
11
00
117 0 $self->_fluent->close;
118 }
119  
120 1 ▶sub log {
Subroutine log: called CC=7 SCAR=30.3
Pod documented
121 1 my ($self, $level, $message) = @_;
122  
123 my $fluent_message = {
124 1 ▶ env => $ENV{DANCER_ENVIRONMENT} || $ENV{PLACK_ENV} || 'development',
Condition40%2/5
$ENV{'DANCER_ENVIRONMENT'} || $ENV{'PLACK_ENV'} || 'development'
l!l
01
$ENV{"DANCER_ENVIRONMENT"} or $ENV{"PLACK_ENV"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$ENV{'DANCER_ENVIRONMENT'} || $ENV{'PLACK_ENV'} || 'development'
$ENV{"DANCER_ENVIRONMENT"} $ENV{"PLACK_ENV"}
Truth table33%1/3
A or B
A= $ENV{"DANCER_ENVIRONMENT"}B= $ENV{"PLACK_ENV"}
ABresult
1X1
011
000
125 timestamp => Time::Moment->now_utc->strftime("%Y-%m-%dT%H:%M:%S.%6N%Z"),
126 host => hostname(),
127 level => $level,
128 message => $message,
129 pid => $$
130 };
131  
132 # Queue pending messages until connectivity is restored
133 1 ▶ unless ( $self->_fluent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_fluent) 1 0 1
134 1 push @{ $self->{pending} }, $fluent_message;
135 1 return;
136 }
137  
138 0 ▶ if ( exists $self->{pending} and @{ $self->{pending} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"pending"} and @{$self->{"pending"};}) 0 0 0
Condition0%0/3
exists $self->{"pending"} and @{$self->{"pending"};}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"pending"} and @{$self->{"pending"};}
exists $self->{"pending"} @{$self->{"pending"};}
Truth table0%0/3
A and B
A= exists $self->{"pending"}B= @{$self->{"pending"};}
ABresult
0X0
100
111
139 0 ▶ while ( my $pending_message = shift @{ $self->{pending} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pending_message = shift @{$$self{"pending"};}) 0 0 0
140 0 $self->_fluent->post( $self->tag_prefix, $pending_message );
141 }
142 }
143 0 $self->_fluent->post( $self->tag_prefix, $fluent_message );
144 }
145  
146 2 1;
147 __END__
↑ Summary