line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Message::Passing::Filter::ToLogstash; |
2
|
2
|
|
|
2
|
|
37381
|
use Moo; |
|
2
|
|
|
|
|
32688
|
|
|
2
|
|
|
|
|
17
|
|
3
|
2
|
|
|
2
|
|
16787
|
use MooX::Types::MooseLike::Base qw/ ArrayRef /; |
|
2
|
|
|
|
|
16944
|
|
|
2
|
|
|
|
|
204
|
|
4
|
2
|
|
|
2
|
|
2444
|
use List::MoreUtils qw/ uniq /; |
|
2
|
|
|
|
|
3060
|
|
|
2
|
|
|
|
|
280
|
|
5
|
2
|
|
|
2
|
|
2772
|
use DateTime; |
|
2
|
|
|
|
|
361737
|
|
|
2
|
|
|
|
|
92
|
|
6
|
2
|
|
|
2
|
|
1008
|
use Sys::Hostname::Long; |
|
2
|
|
|
|
|
3683
|
|
|
2
|
|
|
|
|
141
|
|
7
|
2
|
|
|
2
|
|
18
|
use namespace::clean -except => 'meta'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
22
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
742
|
use constant HOSTNAME => hostname_long(); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
18
|
|
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
|
11
|
my ($self, $message) = @_; |
36
|
6
|
100
|
|
|
|
24
|
if ('HASH' ne ref($message)) { |
37
|
1
|
|
|
|
|
2
|
my $line = $message; |
38
|
1
|
|
|
|
|
11
|
$message = { |
39
|
|
|
|
|
|
|
message => $line, |
40
|
|
|
|
|
|
|
hostname => HOSTNAME, |
41
|
|
|
|
|
|
|
epochtime => AnyEvent->now, |
42
|
|
|
|
|
|
|
type => 'generic_line', |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
6
|
|
|
|
|
39
|
$message = { '@fields' => { %$message } }; |
46
|
6
|
100
|
|
|
|
24
|
if (exists($message->{'@fields'}{epochtime})) { |
47
|
2
|
|
|
|
|
24
|
$message->{'@timestamp'} = DateTime->from_epoch(epoch => delete($message->{'@fields'}{epochtime})) . '' |
48
|
|
|
|
|
|
|
} |
49
|
6
|
|
|
|
|
881
|
foreach my $k (keys %map) { |
50
|
36
|
|
|
|
|
64
|
my $v = $map{$k}; |
51
|
36
|
100
|
|
|
|
105
|
$v = [ '', $v ] if !ref $v; |
52
|
36
|
|
|
|
|
65
|
my ($prefix, $field) = @$v; |
53
|
36
|
|
|
|
|
50
|
$field = '@' . $field; |
54
|
36
|
100
|
66
|
|
|
188
|
if (exists($message->{'@fields'}{$k}) && !exists($message->{$field})) { |
55
|
8
|
|
|
|
|
40
|
$message->{$field} = $prefix . delete $message->{'@fields'}{$k}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
6
|
|
33
|
|
|
51
|
$message->{'@tags'} ||= $self->default_tags; |
59
|
6
|
|
|
|
|
7
|
$message->{'@tags'} = [ uniq @{ $message->{'@tags'} }, @{ $self->add_tags } ]; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
41
|
|
60
|
|
|
|
|
|
|
|
61
|
6
|
|
|
|
|
19
|
$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. |
92
|
|
|
|
|
|
|
|