lib/eris/log.pm

filter: Statementstmt 45.2% 33 / 73 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.8% 7 / 18 Podpod 100% 4 / 4 totaltotal 36.6% 44 / 120 CC 13 SCAR 28.9
CC
13
Cov
34%
add_context
56.0
as_doc
20.0
add_tags
12.0
CRAP 18.0
« lib/eris/log/decoder/syslog.pm ↑ Summary lib/eris/dictionary.pm »
1 package eris::log;
2 # ABSTRACT: Structured log or event object implementation
3  
4 3 ▶use Hash::Flatten ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Hash::Merge::Simple qw(clone_merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Types::Common::Numeric qw(PositiveNum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Types::Standard qw(ArrayRef Bool HashRef Maybe Num Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Ref::Util qw(is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.008'; # VERSION
14  
15  
16 3 has raw => (
17 is => 'ro',
18 isa => Str,
19 required => 1,
20 );
21  
22  
23 has decoded => (
24 is => 'rw',
25 isa => HashRef,
26 lazy => 1,
27 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 3 );
29  
30  
31 has context => (
32 is => 'rw',
33 isa => HashRef,
34 lazy => 1,
35 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 3 );
37  
38  
39 has complete => (
40 is => 'rw',
41 isa => HashRef[HashRef],
42 lazy => 1,
43 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 3 );
45  
46  
47 has timing => (
48 is => 'rw',
49 isa => ArrayRef,
50 lazy => 1,
51 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 3 );
53  
54  
55 has tags => (
56 is => 'rw',
57 isa => ArrayRef,
58 lazy => 1,
59 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 3 );
61  
62  
63 3 has total_time => (
64 is => 'rw',
65 isa => Maybe[PositiveNum],
66 );
67  
68  
69 has 'epoch' => (
70 is => 'rw',
71 isa => Num,
72 0 ▶ default => sub { time },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 3 );
74  
75  
76 has 'type' => (
77 is => 'rw',
78 isa => Str,
79 0 ▶ default => sub { 'log' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 3 );
81  
82  
83 3 has 'uuid' => (
84 is => 'rw',
85 isa => Maybe[Str],
86 );
87  
88  
89 sub add_context {
90 0 ▶ my ($self,$name,$href) = @_;
Subroutine add_context: not called CC=7 SCAR=40.3
Pod documented
91 0 my $complete = $self->complete;
92  
93 0 ▶ unless( defined $href
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $href and Ref::Util::XS::is_hashref($href) and scalar keys %$href) 0 0 0
Condition0%0/5
defined $href and Ref::Util::XS::is_hashref($href) and scalar keys %$href
!ll
00
defined $href and Ref::Util::XS::is_hashref($href)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $href and Ref::Util::XS::is_hashref($href) and scalar keys %$href
defined $href Ref::Util::XS::is_hashref($href)
Truth table0%0/3
A and B
A= defined $hrefB= Ref::Util::XS::is_hashref($href)
ABresult
0X0
100
111
94 && is_hashref($href)
95 && scalar keys %$href
96 ) {
97 0 return;
98 }
99  
100 # Tag the message
101 0 ▶ push @{ $self->tags }, $name if $name ne 'raw';
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "raw" 0 0 0
102  
103 # Install the context
104 0 ▶ $complete->{$name} = exists $complete->{$name} ? clone_merge( $complete->{$name}, $href ) : $href;
Branch: 1 branch
BranchTrueFalseTotal
exists $complete->{$name} ? : 0 0 0
105  
106 # Check for UUID
107 0 ▶ $self->uuid($href->{_id}) if exists $href->{_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $href->{"_id"} 0 0 0
108  
109 # Complete merge
110 0 my $ctx = clone_merge( $self->context, $href );
111 0 $self->context($ctx);
112 }
113  
114  
115 sub add_tags {
116 0 ▶ my ($self,@tags) = @_;
Subroutine add_tags: not called CC=3 SCAR=24.8
Pod documented
117 0 my %tags = map { $_ => 1 } @{ $self->tags };
118  
119 0 foreach my $t (@tags) {
120 0 ▶ push @{ $self->tags }, $t unless exists $tags{$t};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tags{$t} 0 0 0
121 0 $tags{$t} = 1;
122 }
123  
124 0 return $self;
125 }
126  
127  
128 sub add_timing {
129 0 ▶ my ($self,%args) = @_;
Subroutine add_timing: not called CC=2 SCAR=17.9
Pod documented
130 0 ▶ if( exists $args{total} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"total"}) 0 0 0
131 0 $self->total_time( delete $args{total} );
132 }
133 0 my $t = $self->timing;
134 0 push @{ $t },
135 0 map { +{ phase => $_, seconds => $args{$_} } }
136 keys %args;
137 0 return $self;
138 }
139  
140  
141 sub as_doc {
142 0 ▶ my ($self,%args) = @_;
Subroutine as_doc: not called CC=4 SCAR=30.0
Pod documented
143  
144 # Default to just the context;
145 0 ▶ my $doc = $args{complete} ? $self->complete : $self->context;
Branch: 1 branch
BranchTrueFalseTotal
$args{'complete'} ? : 0 0 0
146 0 $doc->{timing} = $self->timing;
147 0 $doc->{tags} = $self->tags;
148 0 ▶ $doc->{total_time} = $self->total_time if $self->total_time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->total_time 0 0 0
149 0 ▶ return $args{flatten} ? Hash::Flatten::flatten($doc) : $doc;
Branch: 1 branch
BranchTrueFalseTotal
$args{'flatten'} ? : 0 0 0
150 }
151  
152  
153 3 1;
154  
155 __END__
156  
157 =pod
158  
159 =encoding UTF-8
160  
161 =head1 NAME
162  
163 eris::log - Structured log or event object implementation
164  
165 =head1 VERSION
166  
167 version 0.008
168  
169 =head1 SYNOPSIS
170  
171 =head1 ATTRIBUTES
172  
173 =head2 raw
174  
175 The unstructured log or event as passed to the contextualizer.
176  
177 =head2 decoded
178  
179 Hash of hashes built by the L<eris::log::decoders->decode()> step.
180  
181 =head2 context
182  
183 Compressed hash built by the L<eris::log::contexts->contextualize> step.
184 Contexts are called in order by priority. As each context runs, it's possible
185 to clobber the context key of a previous context. This represents the final
186 state of the compressed namespace for the structured event.
187  
188 =head2 complete
189  
190 Also populated by the L<eris::log::contexts->contextualize> step, but uses a
191 first level key of the context name to preserve where information originated
192 and protect duplicate keys.
193  
194 =head2 timing
195  
196 ArrayRef storing hash references representing the time each step of the
197 contextualizer took while turning the unstructured event into a structured
198 one.
199  
200 [
201 { phase => 'context::sshd', seconds => 0.0003 },
202 ]
203  
204 Add new timings using the L<add_timings> method like:
205  
206 $log->add_timing( thing => 0.212 );
207  
208 =head2 tags
209  
210 ArrayRef of tags to index with the doc. Add new tags with the L<add_tags>
211 method.
212  
213 =head2 total_time
214  
215 Stores the time the entire process took as a single element.
216  
217 =head2 epoch
218  
219 B<Required>: The numeric representation of the time since the UNIX Epoch. Can
220 be an integer or float. This is required to index the log in the right time.
221  
222 =head2 type
223  
224 Provided to override the type in the schema if being sent to Elasticsearch.
225  
226 You probably shouldn't use this just set a default_type in your schema.
227 Elastic's official docs recommend a single type per index.
228  
229 =head2 uuid
230  
231 Optional GUID representation. If set will be passed along to Elasticsearch as
232 the document B<_id>, otherwise Elasticsearch will autogenerate it.
233  
234 Unless you know what you're doing, you probably shouldn't set this in your
235 contexts or schemas.
236  
237 =head1 METHODS
238  
239 =head2 add_context
240  
241 Takes two parameters: context name as a string, a hash reference containing the context to add
242  
243 Use this to add add context data in your own L<eris::role::context> plugins:
244  
245 sub contextualize_message {
246 my ($self,$log) = @_;
247  
248 $log->add_context( $self->name, { static_key => "static values are shocking" } )l
249 }
250  
251 =head2 add_tags
252  
253 Takes a list of string tags to add to the object. Dedupes and adds new
254 tags to the object.
255  
256 =head2 add_timing
257  
258 Takes a hash of timing data to add to the timing store. This is called by the
259 L<eris::log::contexts>, L<eris::log::decoders>, and
260 L<eris::log::contextualizer> automatically. You probably never need to call
261 this method yourself.
262  
263 =head2 as_doc
264  
265 Arguments parsed from a hash:
266  
267 =over 2
268  
269 =item B<complete>
270  
271 Boolean, default false. If set to true, the document is sourced for the
272 C<eris::log> complete event with the context namespaces as first level keys is
273 returned. By default the C<eris::log> context hash is returned which is a
274 merged set of keys from all contexts.
275  
276 =item B<flatten>
277  
278 Do we flatten the key space using L<Hash::Flatten>. This defaults to false see
279 the differences:
280  
281 # Default (false)
282 {
283 a => {
284 b => 0,
285 c => 1,
286 }
287 }
288  
289 # Flatten (true)
290 {
291 a.b => 0,
292 a.c => 1,
293 }
294  
295 =back
296  
297 =head1 SEE ALSO
298  
299 L<eris::log::contextualizer>, L<eris::log::decoders>, L<eris::log::contexts>
300  
301 =head1 AUTHOR
302  
303 Brad Lhotsky <brad@divisionbyzero.net>
304  
305 =head1 COPYRIGHT AND LICENSE
306  
307 This software is Copyright (c) 2015 by Brad Lhotsky.
308  
309 This is free software, licensed under:
310  
311 The (three-clause) BSD License
312  
313 =cut
« lib/eris/log/decoder/syslog.pm ↑ Summary lib/eris/dictionary.pm »