lib/eris/role/schema.pm

filter: Statementstmt 55.0% 33 / 60 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.5% 6 / 19 Podpod 100% 2 / 2 totaltotal 44.0% 41 / 93 CC 8 SCAR 18.4
CC
8
Cov
46%
to_document
20.0
as_bulk
12.0
_build_index_name
6.0
CRAP 6.3
« lib/eris/dictionary/syslog.pm ↑ Summary lib/eris/log/context/crond.pm »
1 package eris::role::schema;
2 # ABSTRACT: Role for implementing a schema
3  
4 2 ▶use eris::dictionary;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Types::Standard qw(Bool HashRef InstanceOf Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 with qw(
12 eris::role::plugin
13 );
14  
15 2 our $VERSION = '0.008'; # VERSION
16  
17  
18  
19 2 requires qw( match_log );
20  
21  
22 2 has 'index_name_strftime' => (
23 is => 'lazy',
24 isa => Str,
25 );
26 0 ▶sub _build_index_name_strftime { '%Y.%m.%d' }
Subroutine _build_index_name_strftime: not called CC=1 SCAR=6.9
27  
28  
29 2 has 'index_name' => (
30 is => 'lazy',
31 isa => Str,
32 );
33 sub _build_index_name {
34 0 ▶ my ($self) = @_;
Subroutine _build_index_name: not called CC=2 SCAR=17.9
35 0 my $class = ref $self;
36  
37 0 ▶ my $base = $class =~ /::schema::(.*)$/ ? $1
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /::schema::(.*)$/ ? : 0 0 0
38 : $self->name;
39  
40 0 return join('-', $base, $self->index_name_strftime);
41 }
42  
43  
44 2 has 'default_type' => (
45 is => 'lazy',
46 isa => Str,
47 );
48 0 ▶sub _build_default_type { 'log' }
Subroutine _build_default_type: not called CC=1 SCAR=6.9
49  
50  
51 2 has 'types' => (
52 is => 'lazy',
53 isa => HashRef,
54 );
55 sub _build_types {
56 0 ▶ my $self = shift;
Subroutine _build_types: not called CC=1 SCAR=6.9
57 0 return { $self->default_type => 1 };
58 }
59  
60  
61 2 has 'dictionary' => (
62 is => 'lazy',
63 isa => InstanceOf["eris::dictionary"],
64 );
65 sub _build_dictionary {
66 0 ▶ my $self = shift;
Subroutine _build_dictionary: not called CC=1 SCAR=6.9
67 0 return eris::dictionary->new( %{ $self->dictionaries } );
68 }
69  
70  
71 2 has 'use_dictionary' => (
72 is => 'lazy',
73 isa => Bool,
74 );
75 0 ▶sub _build_use_dictionary { 1 }
Subroutine _build_use_dictionary: not called CC=1 SCAR=6.9
76  
77 has 'dictionaries' => (
78 is => 'ro',
79 isa => HashRef,
80 0 ▶ default => sub { +{} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 2 );
82  
83  
84 2 has 'final' => (
85 is => 'lazy',
86 isa => Bool,
87 );
88 0 ▶sub _build_final { 1 }
Subroutine _build_final: not called CC=1 SCAR=6.9
89  
90  
91 2 has 'flatten' => (
92 is => 'lazy',
93 isa => Bool,
94 );
95 0 ▶sub _build_flatten { 1 }
Subroutine _build_flatten: not called CC=1 SCAR=6.9
96  
97  
98 has 'es_version' => (
99 is => 'ro',
100 isa => Str,
101 0 ▶ default => sub { '6.4' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 2 );
103  
104  
105 2 has 'es_template' => (
106 is => 'lazy',
107 isa => HashRef,
108 );
109 sub _build_es_template {
110 0 ▶ my $self = shift;
Subroutine _build_es_template: not called CC=2 SCAR=17.9
111  
112 return {
113 0 ▶ ($self->es_version > 6 ? 'index_pattern' : 'template' ) => sprintf "%s-*", $self->name,
Branch: 1 branch
BranchTrueFalseTotal
$self->es_version > 6 ? : 0 0 0
114 mapping => {
115 dynamic_properties => [
116 {
117 },
118 ],
119 properties => {
120 timestamp => { type => 'datetime' },
121 time => { type => 'datetime' },
122 },
123 }
124 };
125 }
126  
127  
128 sub as_bulk {
129 0 ▶ my ($self,$log) = @_;
Subroutine as_bulk: not called CC=3 SCAR=24.8
Pod documented
130  
131 return sprintf "%s\n%s\n",
132 0 ▶ map { encode_json($_) }
Branch: 2 branches
BranchTrueFalseTotal
exists $self->types->{$log->type} ? : 0 0 0
$log->uuid ? : 0 0 0
133 {
134 index => {
135 _index => strftime($self->index_name, gmtime $log->epoch ),
136 0 _type => exists $self->types->{$log->type} ? $log->type : $self->default_type,
137 $log->uuid ? ( _id => $log->uuid ) : (),
138 }
139 },
140 $self->to_document( $log );
141 }
142  
143  
144 sub to_document {
145 0 ▶ my ($self,$log) = @_;
Subroutine to_document: not called CC=4 SCAR=30.0
Pod documented
146  
147 # Clone Context or Complete
148 0 my $doc = $log->as_doc( complete => !$self->flatten );
149 # Prune Keys using the dictionary
150 0 ▶ if( $self->use_dictionary ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_dictionary) 0 0 0
151 0 foreach my $k ( keys %$doc ) {
152 0 ▶ delete $doc->{$k} unless $self->dictionary->lookup( $k );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dictionary->lookup($k) 0 0 0
153 }
154 }
155 0 return $doc;
156 }
157  
158 2 1;
159  
160 __END__
161  
162 =pod
163  
164 =encoding UTF-8
165  
166 =head1 NAME
167  
168 eris::role::schema - Role for implementing a schema
169  
170 =head1 VERSION
171  
172 version 0.008
173  
174 =head1 SYNOPSIS
175  
176 To implement a schema that takes the processed log entry
177 without field filtering, you could:
178  
179 package my::app::schema::full;
180  
181 use Moo;
182  
183 with qw(eris::role::schema);
184  
185 sub _build_flatten { 0 }
186 sub _build_use_dictionary { 0 }
187  
188 sub match_log { 1 }
189  
190 =head1 ATTRIBUTES
191  
192 =head2 index_name_strftime
193  
194 Defaults to a daily index pattern, '%Y.%m.%d' per Logstash.
195  
196 =head2 index_name
197  
198 Defaults to taking the schema's class, trimming off the namespace and then
199 replacing '::' with an underscore. It then joins this with
200 C<index_name_strftime> to form the index name that's evaluated foreach
201 document.
202  
203 Without overriding this via a config element, we get:
204  
205 Schema Class Index Name
206 -------------------- --------------
207 eris::schema::syslog yslog-%Y.%m.%d
208 eris::schema::access ccess-%Y.%m.%d
209 my::app::schema::log y_app_schema_log-%Y.%m.%d
210  
211 =head2 default_type
212  
213 The type for the Elasticsearch index to assume. Defaults to 'log'.
214  
215 =head2 types
216  
217 A HashRef of valid types for the Elasticsearch index, defaults to just
218 the default_type.
219  
220 =head2 dictionary
221  
222 An instance of L<eris::dictionary> configured for the schema. Parameters passed via
223 the C<dictionary> sub section of the config will be used to build the dictionary.
224  
225 =head2 use_dictionary
226  
227 A boolean, if true the fields in the document will be filtered by
228 the C<dictionary> element.
229  
230 =head2 dictionaries
231  
232 A hashref to configure the loading the dictionaries. Defaults to empty.
233  
234 =head2 final
235  
236 Boolean, defaults to true. If true this schema "steals" the document and
237 the only one bulk item will be appended to the C<as_bulk> per document.
238 Set to false to indexing the same log into multiple indices.
239  
240 You'll need to consider the B<priority> of the schema if you set this to false
241 to ensure the schema is early enough in the chain to accept the document.
242  
243 =head2 flatten
244  
245 Boolean, defaults to true. If true only the B<context> hash from the L<eris::log> object
246 is indexed. If set to false, the C<complete> hash is used instead.
247  
248 =head2 es_version
249  
250 The ElasticSearch version to target for commands and mappings. Defaults to B<6.4>.
251  
252 =head2 es_template
253  
254 The ElasticSearch index template definition from this schema.
255  
256 =head1 METHODS
257  
258 =head2 as_bulk
259  
260 Takes an L<eris::log> object and returns the bulk newline delimited JSON to add
261 that object to the cluster.
262  
263 =head2 to_document
264  
265 Takes an L<eris::log> object and returns a hash reference representing that document
266 for indexing.
267  
268 =head1 INTERFACE
269  
270 =head2 match_log()
271  
272 Takes an C<eris::log> and determines if this schema applies.
273  
274 Returns boolean
275  
276 =head1 AUTHOR
277  
278 Brad Lhotsky <brad@divisionbyzero.net>
279  
280 =head1 COPYRIGHT AND LICENSE
281  
282 This software is Copyright (c) 2015 by Brad Lhotsky.
283  
284 This is free software, licensed under:
285  
286 The (three-clause) BSD License
287  
288 =cut
« lib/eris/dictionary/syslog.pm ↑ Summary lib/eris/log/context/crond.pm »