logstash-codec-json_gz 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3dc0dc85897ec53fab0ea15f90344eb811f5be93c466c0547d666b406e77dc0f
4
+ data.tar.gz: e155156855578909c78f5afb322517389be6c00009f48de4c3fade559fa2d086
5
+ SHA512:
6
+ metadata.gz: d8dd4fb16c50aa2dbac52e1518234186298a08572af17b2460961ca3d8883beece5f06d66398b1ff21ca3a960649c63b0e56da6fd29f2392895eaeb3c798d276
7
+ data.tar.gz: add1cd7a6c8cdf85ec146d8b8272ff5c15010444f581b1ce40919781e11bc956610136dfcc51acf2e68fd59a0ddc479514a49870859500719fd7a09e9e6199f3
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 1.0.0
2
+ - Initial version with support for decoding gzip json, json arrays and json lines.
data/CONTRIBUTORS ADDED
@@ -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
+ * Dimo Terziev - dimo.terziev@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.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
6
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
7
+
8
+ if Dir.exist?(logstash_path) && use_logstash_source
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ end
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.
data/README.md ADDED
@@ -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-codec-awesome", :path => "/your/local/logstash-codec-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 'codec {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-codec-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-codec-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,82 @@
1
+ # encoding: utf-8
2
+ require "logstash/codecs/base"
3
+ require "logstash/util/charset"
4
+ require "logstash/json"
5
+ require "logstash/event"
6
+ require "zlib"
7
+ require 'stringio'
8
+
9
+ # This codec will read gzip encoded json content.
10
+ #
11
+ # Example usage:
12
+ #
13
+ # input {
14
+ # tcp {
15
+ # port=>5004
16
+ # codec => json_gz { json_type => "auto" }
17
+ # }
18
+ # }
19
+ #
20
+ class LogStash::Codecs::JsonGz < LogStash::Codecs::Base
21
+ config_name "json_gz"
22
+
23
+ # The character encoding used in this codec. Examples include "UTF-8" and
24
+ # "CP1252"
25
+ #
26
+ # JSON requires valid UTF-8 strings, but in some cases, software that
27
+ # emits JSON does so in another encoding (nxlog, for example). In
28
+ # weird cases like this, you can set the charset setting to the
29
+ # actual encoding of the text and logstash will convert it for you.
30
+ #
31
+ # For nxlog users, you'll want to set this to "CP1252"
32
+ config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"
33
+
34
+ # The expected format of each event. The following are supported
35
+ # "json" - for json documents or json arrays (default).
36
+ # "json_lines" - json lines delimited by '\n'.
37
+ # "auto" - attempts to auto-detect if the json represents an array or lines.
38
+ config :json_type, :validate => ["json","json_lines", "auto"], :default => "json"
39
+
40
+ public
41
+
42
+ def register
43
+ @converter = LogStash::Util::Charset.new(@charset)
44
+ @converter.logger = @logger
45
+ end
46
+
47
+ def decode(data, &block)
48
+ data = decompress(StringIO.new(data), &block)
49
+ data = @converter.convert(data)
50
+
51
+ if @json_type == "json" || (@json_type == "auto" && data[0] == '[')
52
+ from_json_parse(data, &block)
53
+ else
54
+ data.each_line { |l| from_json_parse(l, &block) }
55
+ end
56
+
57
+ rescue => e
58
+ @logger.error("err: #{e}")
59
+ yield LogStash::Event.new("message" => data, "tags" => ["_jsongzparsefailure"])
60
+ end
61
+
62
+ def encode(data)
63
+ raise RuntimeError.new("This codec is only used to decode gzip encoded json.")
64
+ end
65
+
66
+ private
67
+
68
+ def from_json_parse(json, &block)
69
+ LogStash::Event.from_json(json).each { |event| yield event }
70
+ rescue LogStash::Json::ParserError => e
71
+ @logger.error("JSON parse error, original data now in message field", :error => e, :data => json)
72
+ yield LogStash::Event.new("message" => json, "tags" => ["_jsonparsefailure"])
73
+ end
74
+
75
+ def decompress(data)
76
+ gz = Zlib::GzipReader.new(data)
77
+ gz.read
78
+ rescue Zlib::Error, Zlib::GzipFile::Error => e
79
+ @logger.error("Error decompressing gzip data: #{e}")
80
+ end
81
+
82
+ end # class LogStash::Codecs::JsonGz
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-codec-json_gz'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = "Reads gzip encoded JSON formatted content, creating one event per element in a JSON array or JSON line."
6
+ s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
7
+ s.homepage = 'https://github.com/dterziev/logstash-codec-json_gz'
8
+ s.authors = ['Dimo Terziev']
9
+ s.email = 'dimo.terziev@gmail.com'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
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" => "codec" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency 'logstash-core-plugin-api', ">= 1.60", "<= 2.99"
22
+ s.add_development_dependency 'logstash-devutils'
23
+ end
@@ -0,0 +1,115 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/codecs/json_gz"
4
+ require "logstash/codecs/base"
5
+ require "logstash/util/charset"
6
+ require "logstash/json"
7
+ require "logstash/event"
8
+ require "zlib"
9
+ require 'stringio'
10
+ require "logstash/errors"
11
+ require "insist"
12
+
13
+
14
+
15
+ def compress_with_gzip(io)
16
+ compressed = StringIO.new
17
+ gzip = Zlib::GzipWriter.new(compressed)
18
+ gzip.write(io.read)
19
+ gzip.finish
20
+ compressed.rewind
21
+ compressed
22
+ end
23
+
24
+ def uncompressed_log_array(n)
25
+ str = StringIO.new
26
+ str << "["
27
+ (1..n).each do |i|
28
+ str << "," if i > 1
29
+ d = {"@timestamp" => DateTime.now.new_offset(0).strftime("%Y-%m-%dT%H:%M:%SZ"), "message" => "message #{i}"}
30
+ str << LogStash::Json.dump(d)
31
+ end
32
+ str << "]"
33
+ str.rewind
34
+ str
35
+ end
36
+
37
+ def uncompressed_log_lines(n)
38
+ str = StringIO.new
39
+ (1..n).each do |i|
40
+ d = {"@timestamp" => DateTime.now.new_offset(0).strftime("%Y-%m-%dT%H:%M:%SZ"), "message" => "message #{i}"}
41
+ str << LogStash::Json.dump(d)
42
+ str << "\n"
43
+ end
44
+ str.rewind
45
+ str
46
+ end
47
+
48
+ def verify_decoded_events(data, event_count)
49
+ events = []
50
+ i = 0
51
+ subject.decode(data) do |event|
52
+ i += 1
53
+ insist { event.is_a? LogStash::Event }
54
+ insist { event.get("message") } == "message #{i}"
55
+ events << event
56
+ end
57
+
58
+ expect(events.size).to eq(event_count)
59
+ end
60
+
61
+ describe LogStash::Codecs::JsonGz do
62
+
63
+ context "#decode" do
64
+
65
+ event_count = 10000
66
+ json_array_data = compress_with_gzip(uncompressed_log_array(event_count)).string
67
+ json_lines_data = compress_with_gzip(uncompressed_log_array(event_count)).string
68
+ json_object_data = compress_with_gzip(uncompressed_log_lines(1)).string
69
+
70
+ context "when json_type = json" do
71
+
72
+ subject{LogStash::Codecs::JsonGz.new("json_type" => "json")}
73
+
74
+ it "should create events from gz json array" do
75
+ verify_decoded_events(json_array_data, event_count)
76
+ end
77
+
78
+ it "should create events from gz json object" do
79
+ verify_decoded_events(json_object_data, 1)
80
+ end
81
+ end
82
+
83
+ context "when json_type = json_lines" do
84
+
85
+ subject{LogStash::Codecs::JsonGz.new("json_type" => "json_lines")}
86
+
87
+ it "should create events from gz json lines" do
88
+ verify_decoded_events(json_lines_data, event_count)
89
+ end
90
+
91
+ it "should create events from gz json object" do
92
+ verify_decoded_events(json_object_data, 1)
93
+ end
94
+
95
+ end
96
+
97
+ context "when json_type = auto" do
98
+
99
+ subject{LogStash::Codecs::JsonGz.new("json_type" => "auto")}
100
+
101
+ it "should create events from gz json array" do
102
+ verify_decoded_events(json_array_data, event_count)
103
+ end
104
+
105
+ it "should create events from gz json lines" do
106
+ verify_decoded_events(json_lines_data, event_count)
107
+ end
108
+
109
+ it "should create events from gz json object" do
110
+ verify_decoded_events(json_object_data, 1)
111
+ end
112
+
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-codec-json_gz
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dimo Terziev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-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: '1.60'
19
+ - - "<="
20
+ - !ruby/object:Gem::Version
21
+ version: '2.99'
22
+ name: logstash-core-plugin-api
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.99'
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-devutils
40
+ prerelease: false
41
+ type: :development
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/logstash-plugin install gemname. This
49
+ gem is not a stand-alone program
50
+ email: dimo.terziev@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - CHANGELOG.md
56
+ - CONTRIBUTORS
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - lib/logstash/codecs/json_gz.rb
61
+ - logstash-codec-json_gz.gemspec
62
+ - spec/codecs/json_gz_spec.rb
63
+ homepage: https://github.com/dterziev/logstash-codec-json_gz
64
+ licenses:
65
+ - Apache-2.0
66
+ metadata:
67
+ logstash_plugin: 'true'
68
+ logstash_group: codec
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.6.13
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Reads gzip encoded JSON formatted content, creating one event per element
89
+ in a JSON array or JSON line.
90
+ test_files:
91
+ - spec/codecs/json_gz_spec.rb