logstash-filter-dateparts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18f2c742ff7b1224e1332c29188a86b76e7d6873
4
+ data.tar.gz: fd2520f90581b63177f112b3f99b9d79db36b884
5
+ SHA512:
6
+ metadata.gz: d7c58b44fcbe9711f23ba0e3813a8c544a5b846177bd987f50e11ae7dc47fa24ef150e3be55feba1f36eef1507d3b2c9d1cfebfa429bd2d6945f55ef5d81b339
7
+ data.tar.gz: b0c32973d8f267a68f547373d16701d5e1e8d8d47460b412b6183d4937ad41bd4a83f4fc2dc2d38dde4217b635539455400d7b5af24fd37ebaa7606491b48ff8
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2014–2015 Mike Baranski <http://www.mikeski.net>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ ## License ##
6
+
7
+ Copyright (c) 2014–2015 Mike Baranski <http://www.mikeski.net>
8
+
9
+ Licensed under the Apache License, Version 2.0 (the "License");
10
+ you may not use this file except in compliance with the License.
11
+ You may obtain a copy of the License at
12
+
13
+ http://www.apache.org/licenses/LICENSE-2.0
14
+
15
+ Unless required by applicable law or agreed to in writing, software
16
+ distributed under the License is distributed on an "AS IS" BASIS,
17
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ See the License for the specific language governing permissions and
19
+ limitations under the License.
20
+
21
+ ## About
22
+
23
+ This plugin is useful if you want to easily query Logstash data on *day of week*, *hour of day*, or other parts of a date. See the usage below for details on the output of the plugin. The date parts that can be generated are:
24
+
25
+ * day
26
+ * wday
27
+ * yday
28
+ * month
29
+ * year
30
+ * hour
31
+ * min
32
+ * sec
33
+
34
+ ## Documentation
35
+
36
+ ### Installation
37
+
38
+ To manually install the plugin, download the gem and run:
39
+
40
+ `bin/plugin install --no-verify logstash-filter-dateparts-1.0.0.gem`
41
+
42
+ ### Usage
43
+
44
+ To see the most basic usage, you can run the following (on Linux):
45
+
46
+ `echo "HI" | bin/logstash -e 'input { stdin {} } filter {dateparts { }} output { stdout { codec=> rubydebug}}'`
47
+
48
+ You could also use the logstash generator:
49
+
50
+ `bin/logstash -e 'input { generator { lines => ["HI"] count => 1 } } filter {dateparts { }} output { stdout { codec=> rubydebug}}'`
51
+
52
+ Here is the sample output:
53
+
54
+ {
55
+ "message" => "HI",
56
+ "@version" => "1",
57
+ "@timestamp" => "2015-11-20T12:24:40.217Z",
58
+ "host" => "mike-VirtualBox",
59
+ "day" => 20,
60
+ "wday" => 5,
61
+ "yday" => 324,
62
+ "month" => 11,
63
+ "year" => 2015,
64
+ "hour" => 12,
65
+ "min" => 24,
66
+ "sec" => 40
67
+ }
68
+
69
+
70
+ This uses the default configuration, which generates the following fields from the `@timestamp` field of the event:
71
+
72
+ * day
73
+ * wday
74
+ * yday
75
+ * month
76
+ * year
77
+ * hour
78
+ * min
79
+ * sec
80
+
81
+ ### Configuration
82
+
83
+ #### Fields
84
+
85
+ The generated fields are based on the date functions available in the [Ruby time class](http://ruby-doc.org/core-2.2.0/Time.html). You can specify any valid function and it will be added to the event.
86
+
87
+ For example, this will add 2 fields, *sec* corresponding to `time.sec()` and *hour* corresponding to `time.hour()`:
88
+
89
+ filter {
90
+ dateparts {
91
+ "fields" => ["sec", "hour"]
92
+ }
93
+ }
94
+
95
+ #### Time Field
96
+
97
+ By default, the plugin will use the *@timestamp* field, but you can specify a different one:
98
+
99
+ filter {
100
+ dateparts {
101
+ "time_field" => "some_other_field"
102
+ }
103
+ }
104
+
105
+ #### Error Tags
106
+
107
+ By default, the tag *_dateparts_error* is added on exception. You can specify different tag(s) like so:
108
+
109
+ filter {
110
+ dateparts {
111
+ "error_tags" => ["bad_dates", "xyz"]
112
+ }
113
+ }
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+ # This filter will add date parts to your record based on
6
+ # the timestamp field.
7
+ #
8
+ class LogStash::Filters::DateParts < LogStash::Filters::Base
9
+ # Setting the config_name here is required. This is how you
10
+ # configure this filter from your Logstash config.
11
+ #
12
+ # filter {
13
+ # dateparts {
14
+ #
15
+ # }
16
+ # }
17
+ #
18
+ config_name "dateparts"
19
+ config :fields, :validate => :array, :default => ["day", "wday", "yday", "month", "year", "hour", "min", "sec"], :required => true
20
+ config :time_field, :validate => :string, :default => "@timestamp", :required => true
21
+ config :error_tags, :validate => :array, :default => ["_dateparts_error"], :required => true
22
+
23
+ public
24
+ def register
25
+ logger.debug? and logger.debug("DateParts filter registered")
26
+ end
27
+
28
+ def plugin_error(message, event)
29
+ logger.error("DatePart filter error: " + message)
30
+ LogStash::Util::Decorators.add_tags(@error_tags, event, "filters/#{self.class.name}")
31
+ end
32
+
33
+ def get_time_from_field(f)
34
+ if f.class == Time
35
+ return f
36
+ elsif f.respond_to?("time")
37
+ logger.info("Class is #{f.class}")
38
+ return f.time()
39
+ else
40
+ return nil
41
+ end
42
+ end
43
+
44
+ public
45
+ def filter(event)
46
+ if @fields.respond_to?("each") and @fields.respond_to?("join")
47
+ logger.debug? and logger.debug("DateParts plugin filtering #{@time_field} time_field and adding fields: " + @fields.join(", "))
48
+ t = get_time_from_field(event[@time_field])
49
+ if t == nil
50
+ plugin_error("Invalid time field #{@time_field}; Time field must be an instance of Time or provide a time method that returns one", event)
51
+ return
52
+ end
53
+ @fields.each do |field|
54
+ begin
55
+ event[field] = t.send(field)
56
+ rescue
57
+ plugin_error("No such method: #{field}\n", event)
58
+ end
59
+ end
60
+ else
61
+ plugin_error("DateParts plugin fields invalid, should be an array of function names")
62
+ return
63
+ end
64
+
65
+ filter_matched(event)
66
+ end # def filter
67
+
68
+ end # class LogStash::Filters::DateParts
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-dateparts'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = 'This dateparts fileter adds date information to your event based on your timestamp'
6
+ s.description = 'This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program'
7
+ s.authors = ['Mike Baranski']
8
+ s.email = 'mike.baranski@gmail.com'
9
+ s.homepage = 'http://mikeski.net'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','Gemfile','LICENSE']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { 'logstash_plugin' => 'true', 'logstash_group' => 'filter' }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency 'logstash-core', '>= 2.0.0.beta2', '< 3.0.0'
22
+ s.add_development_dependency 'logstash-devutils'
23
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+ require "logstash/filters/dateparts"
3
+ require "logstash/timestamp"
4
+ require "logstash/event"
5
+
6
+ def get_event(contents = {})
7
+ contents["@timestamp"] = LogStash::Timestamp.new
8
+ event = LogStash::Event.new(contents)
9
+ return event
10
+ end
11
+
12
+ describe LogStash::Filters::DateParts do
13
+ default_ts = "@timestamp"
14
+ alt_ts_field = "zxlk"
15
+
16
+ it "Default config should result in filter with 8 functions, one error tag and @timestamp as the time field" do
17
+ f = LogStash::Filters::DateParts.new({})
18
+
19
+ expect(f.class).to eq(LogStash::Filters::DateParts)
20
+ expect(f.fields.length).to eq(8)
21
+ expect(f.time_field).to eq(default_ts)
22
+ expect(f.error_tags.length).to eq(1)
23
+ end
24
+
25
+ it "Config should result in filter with 2 functions and the alt timestamp field" do
26
+ f = LogStash::Filters::DateParts.new({
27
+ "fields" => ["sec", "hour"],
28
+ "time_field" => alt_ts_field
29
+ })
30
+
31
+ expect(f.class).to eq(LogStash::Filters::DateParts)
32
+ expect(f.fields.length).to eq(2)
33
+ expect(f.fields[0]).to eq("sec")
34
+ expect(f.time_field).to eq(alt_ts_field)
35
+ end
36
+
37
+ it "Should generate the default fields (8 of them)" do
38
+ event = get_event()
39
+ count = event.to_hash().count
40
+ f = LogStash::Filters::DateParts.new({})
41
+ f.filter(event)
42
+
43
+ expect(event.to_hash().count).to eq(count + 8)
44
+ expect(event['sec']).to be_truthy
45
+ expect(event['hour']).to be_truthy
46
+ expect(event['min']).to be_truthy
47
+ expect(event['month']).to be_truthy
48
+ expect(event['year']).to be_truthy
49
+ expect(event['day']).to be_truthy
50
+ expect(event['wday']).to be_truthy
51
+ expect(event['yday']).to be_truthy
52
+ expect(event['tags']).to be_nil
53
+ end
54
+
55
+ it "Should generate only the specified fields" do
56
+ event = get_event()
57
+ count = event.to_hash.count
58
+ f = LogStash::Filters::DateParts.new({
59
+ "fields" => ["sec", "hour"]
60
+ })
61
+ f.filter(event)
62
+ expect(event.to_hash().count).to eq(count + 2)
63
+ expect(event['sec']).to be_truthy
64
+ expect(event['hour']).to be_truthy
65
+ expect(event['min']).to be_nil
66
+ expect(event['month']).to be_nil
67
+ expect(event['year']).to be_nil
68
+ expect(event['day']).to be_nil
69
+ expect(event['wday']).to be_nil
70
+ expect(event['yday']).to be_nil
71
+ expect(event['tags']).to be_nil
72
+ end
73
+
74
+ it "Should set the error tag on an invalid time field" do
75
+ event = get_event()
76
+ count = event.to_hash().count
77
+ f = LogStash::Filters::DateParts.new({ "time_field" => alt_ts_field })
78
+
79
+ f.filter(event)
80
+ expect(event['tags'].include? '_dateparts_error').to eq(true)
81
+ end
82
+ end
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-dateparts
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Baranski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0.beta2
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0.beta2
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: logstash-devutils
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: This gem is a logstash plugin required to be installed on top of the
48
+ Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not
49
+ a stand-alone program
50
+ email: mike.baranski@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - CHANGELOG.md
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - lib/logstash/filters/dateparts.rb
60
+ - logstash-filter-dateparts.gemspec
61
+ - spec/filters/dateparts_spec.rb
62
+ - spec/spec_helper.rb
63
+ homepage: http://mikeski.net
64
+ licenses:
65
+ - Apache License (2.0)
66
+ metadata:
67
+ logstash_plugin: 'true'
68
+ logstash_group: filter
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: This dateparts fileter adds date information to your event based on your
89
+ timestamp
90
+ test_files:
91
+ - spec/filters/dateparts_spec.rb
92
+ - spec/spec_helper.rb