[SCM] libmessage-passing-perl Debian packaging branch, master, updated. debian/0.111-3-14-g44f6e88

Tomas Doran bobtfish at bobtfish.net
Mon May 6 11:57:43 UTC 2013


The following commit has been merged in the master branch:
commit 4446c17b9241047d4767ce16dc1516dc14d6b7ae
Author: Tomas Doran <bobtfish at bobtfish.net>
Date:   Mon Sep 3 21:53:40 2012 +0100

    Add a to logstash filter, and make file tailing less raw

diff --git a/Makefile.PL b/Makefile.PL
index 18cbf56..4c7752f 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -26,6 +26,8 @@ requires 'String::RewritePrefix';
 requires 'JSON' => '2.0';
 requires 'Try::Tiny';
 requires 'Task::Weaken';
+requires 'Sys::Hostname';
+requires 'DateTime';
 
 install_script 'script/message-pass';
 
diff --git a/README b/README
index 4836557..f7ad18a 100644
--- a/README
+++ b/README
@@ -7,9 +7,6 @@ SYNOPSIS
         {"foo":"bar"}
 
 DESCRIPTION
-    This implementation is currently a prototype, and as such should be
-    considered alpha and subject to change at any point.
-
     A library for building high performance, loosely coupled and
     reliable/reseliant applications, structured as small services which
     communicate over the network by passing messages.
diff --git a/lib/Message/Passing/Filter/ToLogstash.pm b/lib/Message/Passing/Filter/ToLogstash.pm
new file mode 100644
index 0000000..955a003
--- /dev/null
+++ b/lib/Message/Passing/Filter/ToLogstash.pm
@@ -0,0 +1,65 @@
+package Message::Passing::Filter::ToLogStash;
+use Moo;
+use MooX::Types::MooseLike::Base qw/ ArrayRef /;
+use List::MoreUtils qw/ uniq /;
+use DateTime;
+use Sys::Hostname ();
+use namespace::clean -except => 'meta';
+
+use constant HOSTNAME => Sys::Hostname::hostname();
+
+with 'Message::Passing::Role::Filter';
+
+has default_tags => (
+    is => 'ro',
+    isa => ArrayRef,
+    default => sub { [] },
+);
+
+has add_tags => (
+    is => 'ro',
+    isa => ArrayRef,
+    default => sub { [] },
+);
+
+my %map = (
+    '__CLASS__' => [ 'perl:Class', 'type' ],
+    hostname    => 'source_host',
+    message     => 'message',
+    filename    => 'source_path',
+    date        => 'timestamp',
+    type        => 'type',
+);
+
+sub filter {
+    my ($self, $message) = @_;
+    if ('HASH' ne ref($message)) {
+        my $line = $message;
+        $message = {
+            message   => $line,
+            hostname  => HOSTNAME,
+            epochtime => AnyEvent->now,
+            type      => 'generic_line',
+        };
+    }
+    $message = { '@fields' => { %$message } };
+    if (exists($message->{'@fields'}{epochtime})) {
+        $message->{'@timestamp'} = DateTime->from_epoch(epoch => delete($message->{'@fields'}{epochtime}))
+    }
+    foreach my $k (keys %map) {
+        my $v = $map{$k};
+        $v = [ '', $v ] if !ref $v;
+        my ($prefix, $field) = @$v;
+        $field = '@' . $field;
+        if (exists($message->{'@fields'}{$field}) && !exists($message->{$field})) {
+            $message->{$field} = $prefix . delete $message->{'@fields'}{$field};
+        }
+    }
+    $message->{'@tags'} ||= $self->default_tags;
+    $message->{'@tags'} = [ uniq @{ $message->{'@tags'} }, @{ $self->add_tags } ];
+
+    $message;
+}
+
+1;
+
diff --git a/lib/Message/Passing/Input/FileTail.pm b/lib/Message/Passing/Input/FileTail.pm
index 0abcc54..6baedb1 100644
--- a/lib/Message/Passing/Input/FileTail.pm
+++ b/lib/Message/Passing/Input/FileTail.pm
@@ -4,8 +4,11 @@ use MooX::Types::MooseLike::Base qw/ Str Int /;
 use AnyEvent;
 use Scalar::Util qw/ weaken /;
 use POSIX ":sys_wait_h";
+use Sys::Hostname ();
 use namespace::clean -except => 'meta';
 
+use constant HOSTNAME => Sys::Hostname::hostname();
+
 with 'Message::Passing::Role::Input';
 
 has filename => (
@@ -21,6 +24,24 @@ has _tail_handle => (
     clearer => '_clear_tail_handle',
 );
 
+has raw => (
+    is => 'ro',
+    default => sub { 0 },
+);
+
+sub _emit_line {
+    my ($self, $line) = @_;
+
+    my $data = $self->raw ? $line : {
+        filename  => $self->filename,
+        message   => $line,
+        hostname  => HOSTNAME,
+        epochtime => AnyEvent->now,
+        type      => 'log_line',
+    };
+    $self->output_to->consume($data);
+}
+
 sub _build_tail_handle {
     my $self = shift;
     weaken($self);
@@ -41,7 +62,7 @@ sub _build_tail_handle {
                 });
                 return;
             }
-            $self->output_to->consume($input);
+            $self->_emit_line($input);
         },
     );
 }
@@ -61,16 +82,21 @@ Message::Passing::Input::FileTail - File tailing input
 =head1 SYNOPSIS
 
     message-pass --input FileTail --input_options '{"filename": "/var/log/foo.log"} --output STDOUT
-    {"foo":"bar"}
+    {"filename":"/var/log/foo.log","message":"example line","hostname":"www.example.com","epochtime":"1346705476","type":"log_line"}
 
 =head1 DESCRIPTION
 
-=head1 METHODS
+=head1 ATTRIBUTES
 
 =head2 filename
 
 The filename of the file to tail.
 
+=head2 raw
+
+If the file data should be output raw (as just a line). Normally lines are
+output as a hash of data including the fields showing in the SYNOPSIS.
+
 =head1 SEE ALSO
 
 L<Message::Passing>
@@ -80,11 +106,12 @@ L<Message::Passing>
 This module exists due to the wonderful people at Suretec Systems Ltd.
 <http://www.suretecsystems.com/> who sponsored its development for its
 VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
-the SureVoIP API - 
+the SureVoIP API -
 <http://www.surevoip.co.uk/support/wiki/api_documentation>
 
 =head1 AUTHOR, COPYRIGHT AND LICENSE
 
 See L<Message::Passing>.
 
-=cut
\ No newline at end of file
+=cut
+
diff --git a/t/00_compile.t b/t/00_compile.t
index e09a3b8..761d649 100644
--- a/t/00_compile.t
+++ b/t/00_compile.t
@@ -26,6 +26,7 @@ use_ok('Message::Passing::Role::HasHostnameAndPort');
 use_ok('Message::Passing::Role::HasUsernameAndPassword');
 use_ok('Message::Passing::Role::HasErrorChain');
 use_ok('Message::Passing::Input::Socket::UDP');
+use_ok('Message::Passing::Filter::ToLogstash');
 
 done_testing;
 

-- 
libmessage-passing-perl Debian packaging



More information about the Pkg-perl-cvs-commits mailing list