logstash-filter-edn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3dd1567b8932d848fe00c0b0f6202363666578c3
4
+ data.tar.gz: b3a2895db4c74c7c52295015e3783b70ce460fbe
5
+ SHA512:
6
+ metadata.gz: 43b571d82db0e6cef985630d1c02def78b76ec623a9351f3e6a26b32a91b4990df55afd1edf48432a50353eb578c71dd4d7f14e7540823c2d398647af2e1c9a4
7
+ data.tar.gz: 93a9114102302f496b71361d475daca5312329346f6d28022fa4d1ad7b69c4c3729539cc0002f6187b53b434ffeab9e841bcad88a1d8be7f3c130f500cca60e5
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Evan Niessen-Derry - eniessenderry@gmail.com
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
@@ -0,0 +1,3 @@
1
+ # logstash-filter-edn
2
+
3
+ Convert EDN data into JSON data. Similar in structure to `logstash-filter-json`.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,112 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require "logstash/json"
5
+ require "edn"
6
+ require "json"
7
+
8
+ # This is an EDN parsing filter which takes an existing field containing EDN
9
+ # and expands it into a data structure within the Logstash event. It takes a
10
+ # lot of inspiration from the JSON filter.
11
+ #
12
+ # By default, it will place the parsed EDN in the root of the Logstash event,
13
+ # but you can specify any arbitrary event field using the `target`
14
+ # configuration option.
15
+ #
16
+ #
17
+ class LogStash::Filters::EDN < LogStash::Filters::Base
18
+
19
+ config_name "edn"
20
+
21
+ # The configuration options for the EDN filter:
22
+ # [source, ruby]
23
+ # source => source_field, required
24
+ # target => target_field, optional
25
+ #
26
+ # Which field contains the EDN data?
27
+ # [source, ruby]
28
+ # -------------------------------
29
+ # filter {
30
+ # edn {
31
+ # source => "message"
32
+ # }
33
+ # }
34
+ # -------------------------------
35
+ config :source, :validate => :string, :required => true
36
+
37
+ # Where should we store the parsed data? If this setting is omitted, the EDN
38
+ # data will be store at the top level of the event.
39
+ #
40
+ # NB: This is required if the EDN data isn't a map
41
+ #
42
+ # [source, ruby]
43
+ # -------------------------------
44
+ # filter {
45
+ # edn {
46
+ # source => "message"
47
+ # target => "edn"
48
+ # }
49
+ # }
50
+ # -------------------------------
51
+ #
52
+ # NB: The `target` field will be overwitten if it exists
53
+ config :target, :validate => :string
54
+
55
+ # Array of values to append to the event tags upon parsing failure
56
+ config :tag_on_failure, :validate => :array, :default => [ "_ednparsefailure" ]
57
+
58
+ # Should we pass the event through without error/tagging upon parsing failure?
59
+ config :skip_on_invalid_edn, :validate => :boolean, :default => false
60
+
61
+ public
62
+ def register
63
+ # This space intentionally empty
64
+ end
65
+
66
+ public
67
+ def filter(event)
68
+
69
+ @logger.debug? && @logger.debug("Running EDN filter", :event => event)
70
+
71
+ # Let's grab the field we're parsing; bail if it's not there
72
+ source = event.get(@source)
73
+ return unless source
74
+
75
+ begin
76
+ # Since ES needs this in JSON, we're going to first parse the EDN to Ruby
77
+ # values, then into JSON values
78
+ parsed = LogStash::Json.load(EDN.read(source).to_json)
79
+ rescue Exception => e
80
+ unless @skip_on_invalid_edn
81
+ @tag_on_failure.each{|tag| event.tag(tag)}
82
+ @logger.warn("Error parsing edn", :source => @source, :raw => source, :exception => e)
83
+ end
84
+ return
85
+ end
86
+
87
+ # The user told us where they'd like the data, so let's put it there
88
+ if @target
89
+ event.set(@target, parsed)
90
+ else
91
+ # If we don't have a hash, then we don't have keys to tie the values to.
92
+ # We could guess about the key to tie them to, but instead we'll just
93
+ # force people to specify one.
94
+ unless parsed.is_a?(Hash)
95
+ @tag_on_failure.each{|tag| event.tag(tag)}
96
+ @logger.warn("Non-map EDN values require a :target to be specified; exiting EDN filter.", :source => @source, :raw => source)
97
+ return
98
+ end
99
+
100
+ # Persist every parsed field into the event
101
+ # JSON doesn't have Symbol/Keyword types, so we convert them to JSON.
102
+ parsed.each{|k, v| event.set(k, v)}
103
+ end
104
+
105
+ @logger.debug? && @logger.debug("Event post EDN filtering", :event => event)
106
+
107
+ # filter_matched should go in the last line of our successful code
108
+ filter_matched(event)
109
+ end # def filter
110
+ end # class LogStash::Filters::Edn
111
+
112
+ # vim: set et ts=2 sw=2 :
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-edn'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'A Logstash filter for EDN data.'
6
+ s.homepage = "https://github.com/kidblog/logstash-filter-edn"
7
+ s.authors = ['Evan Niessen-Derry']
8
+ s.email = 'eniessenderry@gmail.com'
9
+ s.require_paths = ['lib']
10
+
11
+ # Files
12
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
13
+ # Tests
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+
16
+ # Special flag to let us know this is actually a logstash plugin
17
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
18
+
19
+ # Gem dependencies
20
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.1"
21
+ s.add_development_dependency 'logstash-devutils', "~> 1.3"
22
+ #s.add_development_dependency 'pry-byebug', '~>3.6'
23
+ #s.add_development_dependency 'pry-debugger', '~>0.2'
24
+ s.add_development_dependency 'ruby-debug', '~> 0.10'
25
+
26
+ # Our particular dependencies
27
+ s.add_runtime_dependency 'edn', "~> 1.1"
28
+ end
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/edn"
4
+
5
+ describe LogStash::Filters::EDN do
6
+ # Parses an event's source and puts the parsed data on the top level if no
7
+ # :target
8
+ describe "parse EDN into event" do
9
+ config <<-CONFIG
10
+ filter {
11
+ edn {
12
+ # Parse message as edn
13
+ source => "message"
14
+ }
15
+ }
16
+ CONFIG
17
+
18
+ sample '{:hello "world", "list" [1 2 3], :map { "foo" "bar" }, 1 "one cookie"}' do
19
+ #debugger
20
+ insist { subject.get("hello") } == "world"
21
+ insist { subject.get("list").to_a } == [1,2,3]
22
+ insist { subject.get("map") } == { "foo"=>"bar" }
23
+ insist { subject.get("1") } == "one cookie"
24
+ end
25
+ end
26
+
27
+ # Puts the parsed data under :target if specified
28
+ # Throws an error if passed non-map data and no :target spec'd
29
+ describe "Put parsed EDN into target field" do
30
+ config <<-CONFIG
31
+ filter {
32
+ edn {
33
+ # Parse message as edn
34
+ source => "message"
35
+ target => "edn"
36
+ }
37
+ }
38
+ CONFIG
39
+
40
+ sample '{:hello "world"}' do
41
+ insist { subject.get("edn") } == LogStash::Json.load('{"hello":"world"}')
42
+ end
43
+ end
44
+
45
+ # Throws an error if passed non-map data and no :target spec'd
46
+ describe "Put parsed EDN into target field" do
47
+ config <<-CONFIG
48
+ filter {
49
+ edn {
50
+ # Parse message as edn
51
+ source => "message"
52
+ }
53
+ }
54
+ CONFIG
55
+
56
+ sample '{:hello}' do
57
+ expect(subject.get('tags')).to include("_ednparsefailure")
58
+ end
59
+ end
60
+
61
+ # Throws an error if passed non-map data and no :target spec'd
62
+ # except not if :skip_on_invalid_edn
63
+ describe "Put parsed EDN into target field" do
64
+ config <<-CONFIG
65
+ filter {
66
+ edn {
67
+ # Parse message as edn
68
+ source => "message"
69
+ skip_on_invalid_edn => true
70
+ }
71
+ }
72
+ CONFIG
73
+
74
+ sample '{:hello}' do
75
+ expect(subject.get('tags')).to be_nil
76
+ end
77
+ end
78
+ end
79
+
80
+ # vim: set et ts=2 sw=2 :
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-edn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evan Niessen-Derry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.1'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.3'
33
+ name: logstash-devutils
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.10'
47
+ name: ruby-debug
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.1'
61
+ name: edn
62
+ prerelease: false
63
+ type: :runtime
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description:
70
+ email: eniessenderry@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - CONTRIBUTORS
77
+ - DEVELOPER.md
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - lib/logstash/filters/edn.rb
82
+ - logstash-filter-edn.gemspec
83
+ - spec/filters/edn_spec.rb
84
+ - spec/spec_helper.rb
85
+ homepage: https://github.com/kidblog/logstash-filter-edn
86
+ licenses:
87
+ - Apache-2.0
88
+ metadata:
89
+ logstash_plugin: 'true'
90
+ logstash_group: filter
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.14.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A Logstash filter for EDN data.
111
+ test_files:
112
+ - spec/filters/edn_spec.rb
113
+ - spec/spec_helper.rb