logstash-codec-avro-data-file 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: fedde9770f0cee9dc35a13fb9a9b4a4a7714cbd1
4
+ data.tar.gz: 2086eb43ef61413f865db0fa1ad9ad945969443e
5
+ SHA512:
6
+ metadata.gz: d6af841762dace1ad1c4cc7f0108bdc30a64f8ae5abd63d1fef7c06ffb31eaeb3ed01b46ef95a138d6a83bfc9b6511cc151241e2aaa4552ff4c68dbe8046d8b1
7
+ data.tar.gz: 6b40bd57abef79f2ea29294a577e424809bacf2c2c52dca762339aa698bebd9548780a3967e6b42dd6fbf98cbfc94a43ae374dc65dcd8773e517a6fb96ad22f6
@@ -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
+ * Kyle Phelps - kphelps@salsify.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,2 @@
1
+ # logstash-codec-avro-data-file
2
+ Example codec plugin. This should help bootstrap your effort to write your own codec plugin!
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Salsify, Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+ # Logstash Avro Data File Codec
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash). It is intended to make it easy to parse Avro data files.
4
+
5
+
6
+ ## Documentation
7
+
8
+ 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/).
9
+
10
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
11
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
12
+
13
+ ## Need Help?
14
+
15
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
16
+
17
+ ## Developing
18
+
19
+ ### 1. Plugin Developement and Testing
20
+
21
+ #### Code
22
+ - To get started, you'll need JRuby with the Bundler gem installed.
23
+
24
+ - 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).
25
+
26
+ - Install dependencies
27
+ ```sh
28
+ bundle install
29
+ ```
30
+
31
+ #### Test
32
+
33
+ - Update your dependencies
34
+
35
+ ```sh
36
+ bundle install
37
+ ```
38
+
39
+ - Run tests
40
+
41
+ ```sh
42
+ bundle exec rspec
43
+ ```
44
+
45
+ ### 2. Running your unpublished Plugin in Logstash
46
+
47
+ #### 2.1 Run in a local Logstash clone
48
+
49
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
50
+ ```ruby
51
+ gem "logstash-codec-avro-data-file", :path => "/your/local/logstash-codec-avro-data-file"
52
+ ```
53
+ - Install plugin
54
+ ```sh
55
+ bin/logstash-plugin install --no-verify
56
+ ```
57
+ - Run Logstash with your plugin
58
+ ```sh
59
+ bin/logstash -e 'codec {avro-data-file {}}'
60
+ ```
61
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
62
+
63
+ #### 2.2 Run in an installed Logstash
64
+
65
+ 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:
66
+
67
+ - Build your plugin gem
68
+ ```sh
69
+ gem build logstash-codec-avro-data-file.gemspec
70
+ ```
71
+ - Install the plugin from the Logstash home
72
+ ```sh
73
+ bin/logstash-plugin install /your/local/plugin/logstash-codec-avro-data-file.gem
74
+ ```
75
+ - Start Logstash and proceed to test the plugin
76
+
77
+ ## Contributing
78
+
79
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
80
+
81
+ 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.
82
+
83
+ It is more important to the community that you are able to contribute.
84
+
85
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ require 'avro'
4
+ require 'logstash/codecs/base'
5
+ require 'logstash/namespace'
6
+ require 'tmpdir'
7
+
8
+ # This codec will append a string to the message field
9
+ # of an event, either in the decoding or encoding methods
10
+ #
11
+ # This is only intended to be used as an example.
12
+ #
13
+ # input {
14
+ # stdin { codec => 'avro-data-file' }
15
+ # }
16
+ #
17
+ # or
18
+ #
19
+ # output {
20
+ # stdout { codec => 'avro-data-file' }
21
+ # }
22
+ #
23
+ class LogStash::Codecs::AvroDataFile < LogStash::Codecs::Base
24
+
25
+ config_name 'avro-data-file'
26
+
27
+ # Set the directory where logstash will store the tmp files before processing them.
28
+ # default to the current OS temporary directory in linux /tmp/logstash/avro
29
+ config :temporary_directory, validate: :string, default: File.join(Dir.tmpdir, 'logstash', 'avro')
30
+
31
+ def register
32
+ require 'fileutils'
33
+ FileUtils.mkdir_p(temporary_directory) unless Dir.exist?(temporary_directory)
34
+ reset
35
+ end
36
+
37
+ def decode(data)
38
+ merge(data)
39
+ end
40
+
41
+ def flush
42
+ tempfile.flush
43
+ return unless block_given?
44
+
45
+ Avro::DataFile.open(tempfile.path, 'r') do |reader|
46
+ reader.each do |avro_message|
47
+ yield LogStash::Event.new(avro_message)
48
+ end
49
+ end
50
+ rescue => e
51
+ @logger.error('Avro parse error', error: e)
52
+ ensure
53
+ reset
54
+ end
55
+
56
+ def encode(_event)
57
+ raise 'Not implemented'
58
+ end
59
+
60
+ private
61
+
62
+ attr_accessor :tempfile
63
+ attr_reader :temporary_directory
64
+
65
+ def merge(bytes)
66
+ tempfile.write(bytes)
67
+ end
68
+
69
+ def reset
70
+ unless tempfile.nil?
71
+ begin
72
+ File.unlink(tempfile.path)
73
+ tempfile.close
74
+ rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
75
+ end
76
+ end
77
+ self.tempfile = Tempfile.create('', temporary_directory)
78
+ end
79
+ end
@@ -0,0 +1,35 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-codec-avro-data-file'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['MIT']
5
+ s.summary = 'Codec for parsing avro data files'
6
+ s.homepage = 'https://github.com/salsify/logstash-codec-avro-data-file'
7
+ s.authors = ['Kyle Phelps']
8
+ s.email = 'kphelps@salsify.com'
9
+ s.require_paths = ['lib']
10
+
11
+ s.files = Dir[
12
+ 'lib/**/*',
13
+ 'spec/**/*',
14
+ 'vendor/**/*',
15
+ '*.gemspec',
16
+ '*.md',
17
+ 'CONTRIBUTORS',
18
+ 'Gemfile',
19
+ 'LICENSE',
20
+ 'NOTICE.TXT'
21
+ ]
22
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
23
+
24
+ # Special flag to let us know this is actually a logstash plugin
25
+ s.metadata = { 'logstash_plugin' => 'true', 'logstash_group' => 'codec' }
26
+
27
+ s.add_runtime_dependency 'avro'
28
+ s.add_runtime_dependency 'logstash-codec-line'
29
+ s.add_runtime_dependency 'logstash-core-plugin-api', '~> 2.0'
30
+ s.add_development_dependency 'logstash-devutils'
31
+
32
+ s.add_development_dependency 'bundler', '~> 1.16'
33
+ s.add_development_dependency 'overcommit'
34
+ s.add_development_dependency 'salsify_rubocop', '~> 0.48.0'
35
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ require 'logstash/codecs/avro-data-file'
4
+ require 'json'
5
+
6
+ describe LogStash::Codecs::AvroDataFile do
7
+ let(:config) { {} }
8
+ let(:codec) { described_class.new(config).tap(&:register) }
9
+ let(:lines) { [] }
10
+ let(:events) { [] }
11
+ let(:expected_events) { [] }
12
+
13
+ before do
14
+ lines.each(&codec.method(:decode))
15
+ end
16
+
17
+ shared_examples "produces the correct output" do
18
+ specify do
19
+ expect(events.length).to eq expected_events.length
20
+ expect(events).to all(be_an_instance_of(LogStash::Event))
21
+ event_hashes = events.map(&:to_hash).map do |event|
22
+ event.except('@timestamp', '@version')
23
+ end
24
+ expected_hashes = expected_events.map(&:to_hash).map do |event|
25
+ event.except('@timestamp', '@version')
26
+ end
27
+
28
+ expect(event_hashes).to eq expected_hashes
29
+ end
30
+ end
31
+
32
+ describe "#decode" do
33
+
34
+ context "without flushing" do
35
+ let(:lines) { ['test', 'test2'] }
36
+
37
+ include_examples "produces the correct output"
38
+ end
39
+
40
+ context "with a flush" do
41
+
42
+ before do
43
+ codec.flush do |event|
44
+ events << event
45
+ end
46
+ end
47
+
48
+ context "invalid data" do
49
+ let(:lines) { ['test', 'test2'] }
50
+
51
+ include_examples "produces the correct output"
52
+ end
53
+
54
+ context "valid data" do
55
+ let(:schema) do
56
+ {
57
+ 'type' => 'record',
58
+ 'name' => 'mock_schema',
59
+ 'namespace' => 'com.salsify.test',
60
+ 'fields' => [
61
+ {
62
+ 'name' => 'id',
63
+ 'type' => 'long'
64
+ }
65
+ ]
66
+ }.to_json
67
+ end
68
+ let(:avro_ids) { (0...100).to_a }
69
+ let(:avro_messages) do
70
+ avro_ids.map do |id|
71
+ { 'id' => id }
72
+ end
73
+ end
74
+ let(:tempfile) do
75
+ Tempfile.new('restore-test')
76
+ end
77
+ let(:lines) do
78
+ Avro::DataFile.open(tempfile.path, 'w', schema) do |writer|
79
+ avro_messages.each do |message|
80
+ writer << message
81
+ end
82
+ end
83
+ File.open(tempfile.path, 'rb', &:to_a)
84
+ end
85
+ let(:expected_events) { avro_messages.map(&LogStash::Event.method(:new)) }
86
+
87
+ include_examples "produces the correct output"
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require 'logstash/devutils/rspec/spec_helper'
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-codec-avro-data-file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Phelps
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-12 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: '0'
19
+ name: avro
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: logstash-codec-line
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ name: logstash-core-plugin-api
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: logstash-devutils
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.16'
75
+ name: bundler
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: overcommit
90
+ prerelease: false
91
+ type: :development
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.48.0
103
+ name: salsify_rubocop
104
+ prerelease: false
105
+ type: :development
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.48.0
111
+ description:
112
+ email: kphelps@salsify.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - CHANGELOG.md
118
+ - CONTRIBUTORS
119
+ - DEVELOPER.md
120
+ - Gemfile
121
+ - LICENSE
122
+ - README.md
123
+ - lib/logstash/codecs/avro-data-file.rb
124
+ - logstash-codec-avro-data-file.gemspec
125
+ - spec/log_stash/codecs/avro_data_file_spec.rb
126
+ - spec/spec_helper.rb
127
+ homepage: https://github.com/salsify/logstash-codec-avro-data-file
128
+ licenses:
129
+ - MIT
130
+ metadata:
131
+ logstash_plugin: 'true'
132
+ logstash_group: codec
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.6.8
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Codec for parsing avro data files
153
+ test_files:
154
+ - spec/log_stash/codecs/avro_data_file_spec.rb
155
+ - spec/spec_helper.rb