| 1 | package Message::Passing::Filter::ToLogstash; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use MooX::Types::MooseLike::Base qw/ ArrayRef /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use List::MoreUtils qw/ uniq /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Sys::Hostname::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use constant HOSTNAME => hostname_long(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | with 'Message::Passing::Role::Filter'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | has default_tags => ( | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 16 | 6 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has add_tags => ( | ||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | 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 | ▶ | my ($self, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | if ('HASH' ne ref($message)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | my $line = $message; | |||||||||||||||||||||||||||
| 38 | 1 | $message = { | |||||||||||||||||||||||||||
| 39 | message => $line, | ||||||||||||||||||||||||||||
| 40 | hostname => HOSTNAME, | ||||||||||||||||||||||||||||
| 41 | epochtime => AnyEvent->now, | ||||||||||||||||||||||||||||
| 42 | type => 'generic_line', | ||||||||||||||||||||||||||||
| 43 | }; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 6 | $message = { '@fields' => { %$message } }; | |||||||||||||||||||||||||||
| 46 | 6 | ▶ | if (exists($message->{'@fields'}{epochtime})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 2 | $message->{'@timestamp'} = DateTime->from_epoch(epoch => delete($message->{'@fields'}{epochtime})) . '' | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 6 | foreach my $k (keys %map) { | |||||||||||||||||||||||||||
| 50 | 36 | my $v = $map{$k}; | |||||||||||||||||||||||||||
| 51 | 36 | ▶ | $v = [ '', $v ] if !ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 36 | my ($prefix, $field) = @$v; | |||||||||||||||||||||||||||
| 53 | 36 | $field = '@' . $field; | |||||||||||||||||||||||||||
| 54 | 36 | ▶ | if (exists($message->{'@fields'}{$k}) && !exists($message->{$field})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $message->{"\@fields"}{$k} and not exists $message->{$field}
MC/DC50%1/2
exists $message->{"\@fields"}{$k} and not exists $message->{$field}
exists $message->{"\@fields"}{$k}
not exists $message->{$field}
Truth table66%2/3
A and not(B)
A= exists $message->{"\@fields"}{$k}B= not exists $message->{$field}
| |||||||||||||||||||||||||||||
| 55 | 8 | $message->{$field} = $prefix . delete $message->{'@fields'}{$k}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 6 | ▶ | $message->{'@tags'} ||= $self->default_tags; | ||||||||||||||||||||||||||
|
Condition33%1/3
$message->{"\@tags"} ||= $self->default_tags
MC/DC0%0/2
$message->{"\@tags"} ||= $self->default_tags
$message->{"\@tags"}
$self->default_tags
Truth table33%1/3
A ||= B
A= $message->{"\@tags"}B= $self->default_tags
| |||||||||||||||||||||||||||||
| 59 | 6 | $message->{'@tags'} = [ uniq @{ $message->{'@tags'} }, @{ $self->add_tags } ]; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 6 | $message; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | 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 | |||||||||||||||||||||||||||||