logstash-filter-truncate 1.0.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: 68f37db86bda4de74978902b01f9dcd4628d7525
4
+ data.tar.gz: 3ac1b4f08c99a5d167302fa828868c1a524925b2
5
+ SHA512:
6
+ metadata.gz: 5559123d9bcf737271ff90147864bafd0d97568bed99893253400aa219ee43262f204366d7a1e248d2f4c277f6754bd0bda35a0897649cc6fc889199216abef8
7
+ data.tar.gz: b5ef11bb6b6be7aa2a2263b9af4ffe9cf0869fdf001b6581b0a9230d233578b17d6a7c534f33600941d47d62cfca00b0ec80b994ad333a30f08d74d599798d09
@@ -0,0 +1 @@
1
+ ## 0.0.1
@@ -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
+ * ...
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,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2016 Elasticsearch <http://www.elastic.co>
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.
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2016 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
@@ -0,0 +1,98 @@
1
+ # Logstash Plugin
2
+
3
+ [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-filter-truncate.svg)](https://travis-ci.org/logstash-plugins/logstash-filter-truncate)
4
+
5
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
+
7
+ 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.
8
+
9
+ ## Documentation
10
+
11
+ 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/).
12
+
13
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - 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).
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ - Update your dependencies
37
+
38
+ ```sh
39
+ bundle install
40
+ ```
41
+
42
+ - Run tests
43
+
44
+ ```sh
45
+ bundle exec rspec
46
+ ```
47
+
48
+ ### 2. Running your unpublished Plugin in Logstash
49
+
50
+ #### 2.1 Run in a local Logstash clone
51
+
52
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
53
+ ```ruby
54
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
+ ```
56
+ - Install plugin
57
+ ```sh
58
+ # Logstash 2.3 and higher
59
+ bin/logstash-plugin install --no-verify
60
+
61
+ # Prior to Logstash 2.3
62
+ bin/plugin install --no-verify
63
+
64
+ ```
65
+ - Run Logstash with your plugin
66
+ ```sh
67
+ bin/logstash -e 'filter {awesome {}}'
68
+ ```
69
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
70
+
71
+ #### 2.2 Run in an installed Logstash
72
+
73
+ 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:
74
+
75
+ - Build your plugin gem
76
+ ```sh
77
+ gem build logstash-filter-awesome.gemspec
78
+ ```
79
+ - Install the plugin from the Logstash home
80
+ ```sh
81
+ # Logstash 2.3 and higher
82
+ bin/logstash-plugin install --no-verify
83
+
84
+ # Prior to Logstash 2.3
85
+ bin/plugin install --no-verify
86
+
87
+ ```
88
+ - Start Logstash and proceed to test the plugin
89
+
90
+ ## Contributing
91
+
92
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
93
+
94
+ 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.
95
+
96
+ It is more important to the community that you are able to contribute.
97
+
98
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,105 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+ # Allows you to truncate fields longer than a given length.
6
+ #
7
+ # This truncates on bytes values, not character count. In practice, this
8
+ # should mean that the truncated length is somewhere between `length_bytes` and
9
+ # `length_bytes - 6` (UTF-8 supports up to 6-byte characters).
10
+ class LogStash::Filters::Truncate < LogStash::Filters::Base
11
+ config_name "truncate"
12
+
13
+ # A list of fieldrefs to truncate if they are too long.
14
+ #
15
+ # If not specified, the default behavior will be to attempt truncation on all
16
+ # strings in the event. This default behavior could be computationally
17
+ # expensive, so if you know exactly which fields you wish to truncate, it is
18
+ # advised that you be specific and configure the fields you want truncated.
19
+ #
20
+ # Special behaviors for non-string fields:
21
+ #
22
+ # * Numbers: No action
23
+ # * Array: this plugin will attempt truncation on all elements of that array.
24
+ # * Hash: truncate will try all values of the hash (recursively, if this hash
25
+ # contains other hashes).
26
+ config :fields, :validate => :string, :list => true
27
+
28
+ # Fields over this length will be truncated to this length.
29
+ #
30
+ # Truncation happens from the end of the text (the start will be kept).
31
+ #
32
+ # As an example, if you set `length_bytes => 10` and a field contains "hello
33
+ # world, how are you?", then this field will be truncated and have this value:
34
+ # "hello worl"
35
+ config :length_bytes, :validate => :number, :required => true
36
+
37
+ def register
38
+ # nothing
39
+ end
40
+
41
+ def filter(event)
42
+ if @fields
43
+ @fields.each do |field|
44
+ Truncator.truncate(event, field, @length_bytes)
45
+ end
46
+ else
47
+ Truncator.truncate_all(event, @length_bytes)
48
+ end
49
+ end
50
+
51
+ module Truncator
52
+ module_function
53
+
54
+ def trim(value, length)
55
+ return value if value.bytesize <= length
56
+ return value if value.nil?
57
+ return "" if length == 0
58
+ return nil if length < 0
59
+
60
+ # Nothing to truncate if the string is empty.
61
+ return value if value.length == 0
62
+
63
+ # Do the actual truncation.
64
+ v = value.byteslice(0, length)
65
+
66
+ # Verify we didn't break the last multibyte character.
67
+ # If we did, keep backing up until it's a good one.
68
+ # Unpack 'U' here will throw an exception if the last character is not a
69
+ # valid UTF-8 character.
70
+ # Note: I am not certain this is the correct solution in all cases.
71
+ i = 0
72
+ while !(v[-1].valid_encoding? && v.bytesize > 0)
73
+ i += 1
74
+ return "" if length - i == 0
75
+ v = value.byteslice(0, length - i)
76
+ end
77
+
78
+ return v
79
+ end
80
+
81
+ def truncate_all(event, length)
82
+ # TODO(sissel): I couldn't find a better way to get the top level keys for
83
+ # an event. Nor could I find a way to iterate over all the keys in an
84
+ # event, so this may have to suffice.
85
+ fields = event.to_hash.keys.map { |k| "[#{k}]" }
86
+ fields.each do |field|
87
+ truncate(event, field, length)
88
+ end
89
+ end
90
+
91
+ def truncate(event, field, length)
92
+ value = event.get(field)
93
+ if value.is_a?(String)
94
+ event.set(field, trim(value, length))
95
+ elsif value.is_a?(Array)
96
+ new_list = value.map { |v| v.is_a?(String) ? trim(v, length) : v }
97
+ event.set(field, new_list)
98
+ elsif value.is_a?(Hash) || value.is_a?(java.util.Map)
99
+ value.keys.each do |key|
100
+ truncate(event, "#{field}[#{key}]", length)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.name = 'logstash-filter-truncate'
4
+ s.version = '1.0.0'
5
+ s.licenses = ['Apache License (2.0)']
6
+ s.summary = "This filter allows you to truncate fields to a given length."
7
+ 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"
8
+ s.authors = ["Elastic"]
9
+ s.email = 'info@elastic.co'
10
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
11
+ s.require_paths = ["lib"]
12
+
13
+ # Files
14
+ s.files = Dir['lib/**/*','spec/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
15
+
16
+ # Tests
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+
19
+ # Special flag to let us know this is actually a logstash plugin
20
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
21
+
22
+ # Gem dependencies
23
+ s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
24
+ s.add_development_dependency "logstash-devutils"
25
+ s.add_development_dependency "flores", "~> 0.0.7"
26
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ require "logstash/devutils/rspec/spec_helper"
4
+ require "logstash/filters/truncate"
5
+ require "flores/random"
6
+ require "flores/rspec"
7
+
8
+ RSpec.configure do |config|
9
+ Flores::RSpec.configure(config)
10
+ end
11
+
12
+
13
+ describe LogStash::Filters::Truncate do
14
+ analyze_results
15
+
16
+ context "defaults" do
17
+ let(:data) {
18
+ {
19
+ "foo" => { "bar" => Flores::Random.text(0..1000) },
20
+ "one" => { "two" => { "three" => Flores::Random.text(0..1000) } },
21
+ "baz" => Flores::Random.text(0..1000),
22
+ }
23
+ }
24
+ let(:length) { Flores::Random.integer(0..1000) }
25
+ subject { described_class.new("length_bytes" => length) }
26
+ let(:event) { LogStash::Event.new(data) }
27
+
28
+ before { subject.filter(event) }
29
+
30
+ stress_it "should truncate all strings in the hash" do
31
+ expect(event.get("[foo][bar]").bytesize).to be <= length
32
+ expect(event.get("[one][two][three]").bytesize).to be <= length
33
+ expect(event.get("baz").bytesize).to be <= length
34
+ end
35
+ end
36
+
37
+ context "with string fields" do
38
+ let(:text) { Flores::Random.text(0..1000) }
39
+ let(:length) { Flores::Random.integer(0..1000) }
40
+ subject { described_class.new("length_bytes" => length, "fields" => [ "example" ]) }
41
+ let(:event) { LogStash::Event.new("message" => text, "example" => text) }
42
+
43
+ before do
44
+ subject.filter(event)
45
+ end
46
+
47
+ stress_it "should truncate the requested field" do
48
+ expect(event.get("example").bytesize).to be <= length
49
+ end
50
+
51
+ stress_it "should remain valid UTF-8" do
52
+ expect(event.get("example")).to be_valid_encoding
53
+ end
54
+
55
+ it "should not modify `message`" do
56
+ expect(event.get("message")).to be == text
57
+ end
58
+ end
59
+
60
+ context "with non-string fields" do
61
+ let(:number) { Flores::Random.integer(-500.. 500) }
62
+ let(:length) { Flores::Random.integer(0..1000) }
63
+ subject { described_class.new("length_bytes" => length, "fields" => [ "example" ]) }
64
+ let(:event) { LogStash::Event.new("example" => number) }
65
+
66
+
67
+ it "should do nothing" do
68
+ expect(event.get("example")).to be == number
69
+ end
70
+ end
71
+
72
+ context "with array fields" do
73
+ let(:count) { Flores::Random.integer(0..20) }
74
+ let(:list) { count.times.map { Flores::Random.text(0..1000) } }
75
+ let(:length) { Flores::Random.integer(0..1000) }
76
+ subject { described_class.new("length_bytes" => length, "fields" => [ "example" ]) }
77
+ let(:event) { LogStash::Event.new("example" => list) }
78
+
79
+ before { subject.filter(event) }
80
+
81
+ stress_it "should truncate all elements in a list" do
82
+ count.times do |i|
83
+ expect(event.get("[example][#{i}]").bytesize).to be <= length
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-truncate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-29 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
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: 0.0.7
53
+ name: flores
54
+ prerelease: false
55
+ type: :development
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.0.7
61
+ 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
62
+ email: info@elastic.co
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - CHANGELOG.md
68
+ - CONTRIBUTORS
69
+ - Gemfile
70
+ - LICENSE
71
+ - NOTICE.TXT
72
+ - README.md
73
+ - lib/logstash/filters/truncate.rb
74
+ - logstash-filter-truncate.gemspec
75
+ - spec/filters/truncate_spec.rb
76
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
77
+ licenses:
78
+ - Apache License (2.0)
79
+ metadata:
80
+ logstash_plugin: 'true'
81
+ logstash_group: filter
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.8
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: This filter allows you to truncate fields to a given length.
102
+ test_files:
103
+ - spec/filters/truncate_spec.rb