line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Message::Passing; |
2
|
3
|
|
|
3
|
|
5465
|
use Moo; |
|
3
|
|
|
|
|
50422
|
|
|
3
|
|
|
|
|
22
|
|
3
|
3
|
|
|
3
|
|
7670
|
use Config::Any; |
|
3
|
|
|
|
|
40172
|
|
|
3
|
|
|
|
|
140
|
|
4
|
3
|
|
|
3
|
|
2334
|
use Message::Passing::Role::CLIComponent; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
27
|
|
5
|
3
|
|
|
3
|
|
2086
|
use Message::Passing::DSL; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
461
|
|
6
|
3
|
|
|
3
|
|
20
|
use Carp qw/ confess /; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
274
|
|
7
|
3
|
|
|
3
|
|
17
|
use MooX::Options flavour => [qw( pass_through )], protect_argv => 0; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
34
|
|
8
|
3
|
|
|
3
|
|
794988
|
use namespace::clean -except => [qw/ meta new_with_options parse_options _options_data _options_config/]; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
38
|
|
9
|
3
|
|
|
3
|
|
1322
|
use 5.008004; |
|
3
|
|
|
|
|
18
|
|
|
3
|
|
|
|
|
2028
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.115'; |
12
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
around 'parse_options' => sub { |
15
|
|
|
|
|
|
|
my $orig = shift; |
16
|
|
|
|
|
|
|
my $class = shift; |
17
|
|
|
|
|
|
|
my %args = $orig->($class, @_); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
if (my $conf = $args{configfile}) { |
20
|
|
|
|
|
|
|
my $cfg = $class->get_config_from_file($conf); |
21
|
|
|
|
|
|
|
foreach my $k (keys %$cfg) { |
22
|
|
|
|
|
|
|
if (!exists $args{$k}) { |
23
|
|
|
|
|
|
|
$args{$k} = $cfg->{$k}; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
return %args; |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
with |
33
|
|
|
|
|
|
|
CLIComponent( name => 'input' ), |
34
|
|
|
|
|
|
|
CLIComponent( name => 'output' ), |
35
|
|
|
|
|
|
|
CLIComponent( name => 'filter', default => 'Null' ), |
36
|
|
|
|
|
|
|
CLIComponent( name => 'decoder', default => 'JSON' ), |
37
|
|
|
|
|
|
|
CLIComponent( name => 'encoder', default => 'JSON' ), |
38
|
|
|
|
|
|
|
CLIComponent( name => 'error', default => 'STDERR' ), |
39
|
|
|
|
|
|
|
CLIComponent( name => 'error_encoder', default => 'Message::Passing::Filter::Encoder::JSON' ), |
40
|
|
|
|
|
|
|
'Message::Passing::Role::Script'; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
option configfile => ( |
43
|
|
|
|
|
|
|
is => 'ro', |
44
|
|
|
|
|
|
|
format => 's', |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get_config_from_file { |
48
|
1
|
|
|
1
|
0
|
3
|
my ($class, $filename) = @_; |
49
|
1
|
|
|
|
|
2
|
my ($fn, $cfg) = %{ Config::Any->load_files({ |
|
1
|
|
|
|
|
13
|
|
50
|
|
|
|
|
|
|
files => [$filename], |
51
|
|
|
|
|
|
|
use_ext => 1, |
52
|
|
|
|
|
|
|
})->[0] }; |
53
|
1
|
|
|
|
|
15558
|
return $cfg; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub build_chain { |
57
|
1
|
|
|
1
|
1
|
2178
|
my $self = shift; |
58
|
|
|
|
|
|
|
message_chain { |
59
|
1
|
|
|
|
|
10
|
error_log( |
60
|
1
|
|
|
|
|
10
|
%{ $self->error_encoder_options }, |
61
|
|
|
|
|
|
|
class => $self->error_encoder, |
62
|
|
|
|
|
|
|
output_to => output error => ( |
63
|
1
|
|
|
1
|
|
2
|
%{ $self->error_options }, |
64
|
|
|
|
|
|
|
class => $self->error, |
65
|
|
|
|
|
|
|
), |
66
|
|
|
|
|
|
|
); |
67
|
1
|
|
|
|
|
12
|
output output => ( |
68
|
1
|
|
|
|
|
44
|
%{ $self->output_options }, |
69
|
|
|
|
|
|
|
class => $self->output, |
70
|
|
|
|
|
|
|
); |
71
|
1
|
|
|
|
|
14
|
encoder("encoder", |
72
|
1
|
|
|
|
|
3
|
%{ $self->encoder_options }, |
73
|
|
|
|
|
|
|
class => $self->encoder, |
74
|
|
|
|
|
|
|
output_to => 'output', |
75
|
|
|
|
|
|
|
); |
76
|
1
|
|
|
|
|
11
|
filter filter => ( |
77
|
1
|
|
|
|
|
2
|
%{ $self->filter_options }, |
78
|
|
|
|
|
|
|
class => $self->filter, |
79
|
|
|
|
|
|
|
output_to => 'encoder', |
80
|
|
|
|
|
|
|
); |
81
|
1
|
|
|
|
|
13
|
decoder("decoder", |
82
|
1
|
|
|
|
|
3
|
%{ $self->decoder_options }, |
83
|
|
|
|
|
|
|
class => $self->decoder, |
84
|
|
|
|
|
|
|
output_to => 'filter', |
85
|
|
|
|
|
|
|
); |
86
|
1
|
|
|
|
|
14
|
input input => ( |
87
|
1
|
|
|
|
|
3
|
%{ $self->input_options }, |
88
|
|
|
|
|
|
|
class => $self->input, |
89
|
|
|
|
|
|
|
output_to => 'decoder', |
90
|
|
|
|
|
|
|
); |
91
|
1
|
|
|
|
|
12
|
}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 NAME |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Message::Passing - a simple way of doing messaging. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
message-pass --input STDIN --output STDOUT |
103
|
|
|
|
|
|
|
{"foo": "bar"} |
104
|
|
|
|
|
|
|
{"foo":"bar"} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 DESCRIPTION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
A library for building high performance, loosely coupled and reliable/resilient applications, |
109
|
|
|
|
|
|
|
structured as small services which communicate over the network by passing messages. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 BASIC PREMISE |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
You have data for discrete events, represented by a hash (and |
114
|
|
|
|
|
|
|
serialized as JSON). |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This could be a text log line, an audit record of an API |
117
|
|
|
|
|
|
|
event, a metric emitted from your application that you wish |
118
|
|
|
|
|
|
|
to aggregate and process - anything that can be a simple hash really.. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
You want to be able to shove these events over the network easily, |
121
|
|
|
|
|
|
|
and aggregate them / filter and rewrite them / split them into worker queues. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This module is designed as a simple framework for writing components |
124
|
|
|
|
|
|
|
that let you do all of these things, in a simple and easily extensible |
125
|
|
|
|
|
|
|
manor. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
For a practical example, You generate events from a source (e.g. |
128
|
|
|
|
|
|
|
L output of logs and performance metrics from your L FCGI |
129
|
|
|
|
|
|
|
or L workers) and run one script that will give you a central |
130
|
|
|
|
|
|
|
application log file, or push the logs into L. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
There are a growing set of components you can plug together |
133
|
|
|
|
|
|
|
to make your solution. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Getting started is really easy - you can just use the C |
136
|
|
|
|
|
|
|
command installed by the distribution. If you have a common config |
137
|
|
|
|
|
|
|
that you want to repeat, or you want to write your own server |
138
|
|
|
|
|
|
|
which does something more flexible than the normal script allows, |
139
|
|
|
|
|
|
|
then see L. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
To dive straight in, see the documentation for the command line utility |
142
|
|
|
|
|
|
|
L, and see the examples in L. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
For more about how the system works, see L. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 COMPONENTS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Below is a non-exhaustive list of components available. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 INPUTS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Inputs receive data from a source (usually a network protocol). |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
They are responsible for decoding the data into a hash before passing |
155
|
|
|
|
|
|
|
it onto the next stage. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Inputs include: |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=over |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item L |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item L |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item L |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item L |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item L |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item L |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item L |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=back |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
You can easily write your own input, just use L, and |
178
|
|
|
|
|
|
|
consume L. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 FILTER |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Filters can transform a message in any way. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Examples include: |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=over |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item L - Returns the input unchanged. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item L - Stops any messages it receives from being passed to the output. I.e. literally filters all input out. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item L - Splits the incoming message to multiple outputs. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=back |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
You can easily write your own filter, just consume |
197
|
|
|
|
|
|
|
L. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Note that filters can be chained, and a filter can return undef to |
200
|
|
|
|
|
|
|
stop a message being passed to the output. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 OUTPUTS |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Outputs send data to somewhere, i.e. they consume messages. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=over |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item L |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item L |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item L |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item L |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item L |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item L - (L) |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item L |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item L |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=back |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 SEE ALSO |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=over |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item L - The manual (contributions cherished) |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item L - Slide deck! |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item L - For creating your log messages. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item L - use Message::Passing outputs from L. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=back |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 THIS MODULE |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
This is a simple L script, with one input, one filter |
243
|
|
|
|
|
|
|
and one output. To build your own similar scripts, see: |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=over |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item L - To declare your message chains |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item L - To provide C and C attribute pairs. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item L - To provide daemonization features. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=back |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head2 METHODS |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head3 build_chain |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Builds and returns the configured chain of input => filter => output |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head3 start |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Class method to call the run_message_server function with the results of |
264
|
|
|
|
|
|
|
having constructed an instance of this class, parsed command line options |
265
|
|
|
|
|
|
|
and constructed a chain. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
This is the entry point for the script. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 AUTHOR |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Tomas (t0m) Doran |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 SUPPORT |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head2 Bugs |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Please log bugs at L. Each distribution has a bug tracker |
278
|
|
|
|
|
|
|
link in it's L page. |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head2 Discussion |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
L<#message-passing> on L. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head2 Source code |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Source code for all modules is available at L |
287
|
|
|
|
|
|
|
and forks / patches are very welcome. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head1 SPONSORSHIP |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
This module exists due to the wonderful people at Suretec Systems Ltd. |
292
|
|
|
|
|
|
|
who sponsored its development for its |
293
|
|
|
|
|
|
|
VoIP division called SureVoIP for use with |
294
|
|
|
|
|
|
|
the SureVoIP API - |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head1 COPYRIGHT |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
Copyright Suretec Systems Ltd. 2012. |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Logstash (upon which many ideas for this project is based, but |
302
|
|
|
|
|
|
|
which we do not reuse any code from) is copyright 2010 Jorden Sissel. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head1 LICENSE |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
GNU Library General Public License, Version 2.1 |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=cut |
309
|
|
|
|
|
|
|
|