line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Message::Passing::Filter::ToLogstash; |
2
|
2
|
|
|
2
|
|
90756
|
use Moo; |
|
2
|
|
|
|
|
9453
|
|
|
2
|
|
|
|
|
12
|
|
3
|
2
|
|
|
2
|
|
2036
|
use MooX::Types::MooseLike::Base qw/ ArrayRef /; |
|
2
|
|
|
|
|
5457
|
|
|
2
|
|
|
|
|
152
|
|
4
|
2
|
|
|
2
|
|
15
|
use List::Util qw( uniq ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
166
|
|
5
|
2
|
|
|
2
|
|
1763
|
use DateTime; |
|
2
|
|
|
|
|
925735
|
|
|
2
|
|
|
|
|
108
|
|
6
|
2
|
|
|
2
|
|
599
|
use Sys::Hostname::Long; |
|
2
|
|
|
|
|
2441
|
|
|
2
|
|
|
|
|
135
|
|
7
|
2
|
|
|
2
|
|
19
|
use namespace::clean -except => 'meta'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
20
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
657
|
use constant HOSTNAME => hostname_long(); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
8
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Message::Passing::Role::Filter'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has default_tags => ( |
14
|
|
|
|
|
|
|
is => 'ro', |
15
|
|
|
|
|
|
|
isa => ArrayRef, |
16
|
|
|
|
|
|
|
default => sub { [] }, |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has add_tags => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
isa => ArrayRef, |
22
|
|
|
|
|
|
|
default => sub { [] }, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my %map = ( |
26
|
|
|
|
|
|
|
'__CLASS__' => [ 'perl:Class:', 'type' ], |
27
|
|
|
|
|
|
|
hostname => 'source_host', |
28
|
|
|
|
|
|
|
message => 'message', |
29
|
|
|
|
|
|
|
filename => 'source_path', |
30
|
|
|
|
|
|
|
date => 'timestamp', |
31
|
|
|
|
|
|
|
type => 'type', |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub filter { |
35
|
6
|
|
|
6
|
1
|
10
|
my ($self, $message) = @_; |
36
|
6
|
100
|
|
|
|
14
|
if ('HASH' ne ref($message)) { |
37
|
1
|
|
|
|
|
2
|
my $line = $message; |
38
|
1
|
|
|
|
|
7
|
$message = { |
39
|
|
|
|
|
|
|
message => $line, |
40
|
|
|
|
|
|
|
hostname => HOSTNAME, |
41
|
|
|
|
|
|
|
epochtime => AnyEvent->now, |
42
|
|
|
|
|
|
|
type => 'generic_line', |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
6
|
|
|
|
|
28
|
$message = { '@fields' => { %$message } }; |
46
|
6
|
100
|
|
|
|
14
|
if (exists($message->{'@fields'}{epochtime})) { |
47
|
2
|
|
|
|
|
20
|
$message->{'@timestamp'} = DateTime->from_epoch(epoch => delete($message->{'@fields'}{epochtime})) . '' |
48
|
|
|
|
|
|
|
} |
49
|
6
|
|
|
|
|
824
|
foreach my $k (keys %map) { |
50
|
36
|
|
|
|
|
54
|
my $v = $map{$k}; |
51
|
36
|
100
|
|
|
|
64
|
$v = [ '', $v ] if !ref $v; |
52
|
36
|
|
|
|
|
58
|
my ($prefix, $field) = @$v; |
53
|
36
|
|
|
|
|
51
|
$field = '@' . $field; |
54
|
36
|
100
|
66
|
|
|
85
|
if (exists($message->{'@fields'}{$k}) && !exists($message->{$field})) { |
55
|
8
|
|
|
|
|
23
|
$message->{$field} = $prefix . delete $message->{'@fields'}{$k}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
6
|
|
33
|
|
|
33
|
$message->{'@tags'} ||= $self->default_tags; |
59
|
6
|
|
|
|
|
8
|
$message->{'@tags'} = [ uniq @{ $message->{'@tags'} }, @{ $self->add_tags } ]; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
22
|
|
60
|
|
|
|
|
|
|
|
61
|
6
|
|
|
|
|
24
|
$message; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 NAME |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Method::Passing::Filter::ToLogstash |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This filter changes the message format to comply with LogStash. |
73
|
|
|
|
|
|
|
Duplicate tags will be removed. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 default_tags |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
This is the list of tags which get added to the messages' @tags field in case |
80
|
|
|
|
|
|
|
none have been included already. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 add_tags |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This is the list of tags which get added to the messages' @tags field in all |
85
|
|
|
|
|
|
|
cases. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 METHODS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 filter |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Filter the message. |