logstash-filter-json 2.0.2 → 2.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f241f08d6c16daca28363d8e7ca71010d58ab19
4
- data.tar.gz: 79e9fd1be3db90f64b5210c1ce58f17335d5be67
3
+ metadata.gz: 40d60f723d2d12a8b176d226b14d27e4f7b59adf
4
+ data.tar.gz: 09b010f8cd92b795c6af10eaf4b07ef908515768
5
5
  SHA512:
6
- metadata.gz: 3e14beb6d325577ff3c5e08e006995b91197730fed4cd071df3dae23a80bae39d5d9d43156c5033dec44cd1b6bae89e48458f4c12e66bc01d9805346c74ec4c3
7
- data.tar.gz: e552125bc5ac7090400171de2eb1aca84db47f104a6900ecd770802738916ece71b5815f9811ad22742afd83b9c4d7e6aa752f5c3cd8bbdbf847b6aece6ca8e9
6
+ metadata.gz: 9c2bc48511c1b4cae1f9c776479cde3b5558b9e7055971938e1329b5028af7ca6f645049216c4fa0dc0f43003f3ef458e79a6c46469ffe419c93a95882c37d3b
7
+ data.tar.gz: 4ad8488fb033dcd70f161e62f3a41f457650fbec31612dc177893af0867431f16936f3d564bf640ecad9e3f7a21f72896331ad7c5426e6862d7e4e4c9eaebc6a
@@ -1,5 +1,8 @@
1
+ ## 2.0.3
2
+ - Refactored field references, better timestamp handling, code & specs cleanups
3
+
1
4
  ## 2.0.0
2
- - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
5
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
3
6
  instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
4
7
  - Dependency on logstash-core update to 2.0
5
8
 
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Logstash Plugin
2
2
 
3
+ [![Build
4
+ Status](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Filters/job/logstash-plugin-filter-json-unit/badge/icon)](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Filters/job/logstash-plugin-filter-json-unit/)
5
+
3
6
  This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
7
 
5
8
  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.
@@ -46,54 +46,64 @@ class LogStash::Filters::Json < LogStash::Filters::Base
46
46
  # NOTE: if the `target` field already exists, it will be overwritten!
47
47
  config :target, :validate => :string
48
48
 
49
- public
49
+ JSONPARSEFAILURE_TAG = "_jsonparsefailure"
50
+
50
51
  def register
51
52
  # Nothing to do here
52
- end # def register
53
+ end
53
54
 
54
- public
55
55
  def filter(event)
56
-
57
-
58
- @logger.debug("Running json filter", :event => event)
59
-
60
- return unless event.include?(@source)
61
-
62
- # TODO(colin) this field merging stuff below should be handled in Event.
56
+ @logger.debug? && @logger.debug("Running json filter", :event => event)
63
57
 
64
58
  source = event[@source]
59
+ return unless source
65
60
 
66
61
  begin
67
62
  parsed = LogStash::Json.load(source)
68
- # If your parsed JSON is an array, we can't merge, so you must specify a
69
- # destination to store the JSON, so you will get an exception about
70
- if parsed.kind_of?(Array) && @target.nil?
71
- raise('Parsed JSON arrays must have a destination in the configuration')
72
- elsif @target.nil?
73
- event.to_hash.merge! parsed
74
- else
75
- event[@target] = parsed
63
+ rescue => e
64
+ event.tag(JSONPARSEFAILURE_TAG)
65
+ @logger.warn("Error parsing json", :source => @source, :raw => source, :exception => e)
66
+ return
67
+ end
68
+
69
+ if @target
70
+ event[@target] = parsed
71
+ else
72
+ unless parsed.is_a?(Hash)
73
+ event.tag(JSONPARSEFAILURE_TAG)
74
+ @logger.warn("Parsed JSON object/hash requires a target configuration option", :source => @source, :raw => source)
75
+ return
76
76
  end
77
77
 
78
- # If no target, we target the root of the event object. This can allow
79
- # you to overwrite @timestamp and this will typically happen for json
80
- # LogStash Event deserialized here.
81
- if !@target && event.timestamp.is_a?(String)
82
- event.timestamp = LogStash::Timestamp.parse_iso8601(event.timestamp)
78
+ # TODO: (colin) the timestamp initialization should be DRY'ed but exposing the similar code
79
+ # in the Event#init_timestamp method. See https://github.com/elastic/logstash/issues/4293
80
+
81
+ # a) since the parsed hash will be set in the event root, first extract any @timestamp field to properly initialized it
82
+ parsed_timestamp = parsed.delete(LogStash::Event::TIMESTAMP)
83
+ begin
84
+ timestamp = parsed_timestamp ? LogStash::Timestamp.coerce(parsed_timestamp) : nil
85
+ rescue LogStash::TimestampParserError => e
86
+ timestamp = nil
83
87
  end
84
88
 
85
- filter_matched(event)
86
- rescue => e
87
- tag = "_jsonparsefailure"
88
- event["tags"] ||= []
89
- event["tags"] << tag unless event["tags"].include?(tag)
90
- @logger.warn("Trouble parsing json", :source => @source,
91
- :raw => event[@source], :exception => e)
92
- return
89
+ # b) then set all parsed fields in the event
90
+ parsed.each{|k, v| event[k] = v}
91
+
92
+ # c) finally re-inject proper @timestamp
93
+ if parsed_timestamp
94
+ if timestamp
95
+ event.timestamp = timestamp
96
+ else
97
+ event.timestamp = LogStash::Timestamp.new
98
+ @logger.warn("Unrecognized #{LogStash::Event::TIMESTAMP} value, setting current time to #{LogStash::Event::TIMESTAMP}, original in #{LogStash::Event::TIMESTAMP_FAILURE_FIELD} field", :value => parsed_timestamp.inspect)
99
+ event.tag(LogStash::Event::TIMESTAMP_FAILURE_TAG)
100
+ event[LogStash::Event::TIMESTAMP_FAILURE_FIELD] = parsed_timestamp.to_s
101
+ end
102
+ end
93
103
  end
94
104
 
95
- @logger.debug("Event after json filter", :event => event)
96
-
97
- end # def filter
105
+ filter_matched(event)
98
106
 
99
- end # class LogStash::Filters::Json
107
+ @logger.debug? && @logger.debug("Event after json filter", :event => event)
108
+ end
109
+ end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'logstash-filter-json'
4
- s.version = '2.0.2'
4
+ s.version = '2.0.3'
5
5
  s.licenses = ['Apache License (2.0)']
6
6
  s.summary = "This is a JSON parsing filter. It takes an existing field which contains JSON and expands it into an actual data structure within the Logstash event."
7
7
  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"
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "logstash/devutils/rspec/spec_helper"
2
4
  require "logstash/filters/json"
3
5
  require "logstash/timestamp"
@@ -103,52 +105,72 @@ describe LogStash::Filters::Json do
103
105
  end
104
106
  end
105
107
 
106
- context "when json could not be parsed" do
108
+ context "using message field source" do
107
109
 
108
110
  subject(:filter) { LogStash::Filters::Json.new(config) }
109
111
 
110
- let(:message) { "random_message" }
111
- let(:config) { {"source" => "message"} }
112
- let(:event) { LogStash::Event.new("message" => message) }
112
+ let(:config) { {"source" => "message"} }
113
+ let(:event) { LogStash::Event.new("message" => message) }
113
114
 
114
115
  before(:each) do
115
116
  filter.register
116
117
  filter.filter(event)
117
118
  end
118
119
 
119
- it "add the failure tag" do
120
- expect(event).to include "tags"
121
- end
120
+ context "when json could not be parsed" do
121
+ let(:message) { "random_message" }
122
122
 
123
- it "uses an array to store the tags" do
124
- expect(event['tags']).to be_a Array
125
- end
123
+ it "add the failure tag" do
124
+ expect(event).to include("tags")
125
+ end
126
126
 
127
- it "add a json parser failure tag" do
128
- expect(event['tags']).to include "_jsonparsefailure"
129
- end
127
+ it "uses an array to store the tags" do
128
+ expect(event['tags']).to be_a(Array)
129
+ end
130
+
131
+ it "add a json parser failure tag" do
132
+ expect(event['tags']).to include("_jsonparsefailure")
133
+ end
130
134
 
131
- context "there are two different errors added" do
135
+ context "there are two different errors added" do
132
136
 
133
- let(:event) { LogStash::Event.new("message" => message, "tags" => ["_anotherkinfoffailure"] ) }
137
+ let(:event) { LogStash::Event.new("message" => message, "tags" => ["_anotherkinfoffailure"] ) }
134
138
 
135
- it "pile the different error messages" do
136
- expect(event['tags']).to include "_jsonparsefailure"
137
- end
139
+ it "pile the different error messages" do
140
+ expect(event['tags']).to include("_jsonparsefailure")
141
+ end
138
142
 
139
- it "keep the former error messages on the list" do
140
- expect(event['tags']).to include "_anotherkinfoffailure"
143
+ it "keep the former error messages on the list" do
144
+ expect(event['tags']).to include("_anotherkinfoffailure")
145
+ end
141
146
  end
142
147
  end
143
148
 
144
149
  context "the JSON is an ArrayList" do
145
-
146
- let(:message) { "[1, 2, 3]" }
150
+ let(:message) { "[1, 2, 3]" }
147
151
 
148
152
  it "adds the failure tag" do
149
- expect(event['tags']).to include "_jsonparsefailure"
153
+ expect(event['tags']).to include("_jsonparsefailure")
154
+ end
155
+ end
156
+
157
+ context "json contains valid timestamp" do
158
+ let(:message) { "{\"foo\":\"bar\", \"@timestamp\":\"2015-12-02T17:40:00.666Z\"}" }
159
+
160
+ it "should set json timestamp" do
161
+ expect(event.timestamp).to be_a(LogStash::Timestamp)
162
+ expect(event.timestamp.to_s).to eq("2015-12-02T17:40:00.666Z")
150
163
  end
151
164
  end
152
165
 
166
+ context "json contains invalid timestamp" do
167
+ let(:message) { "{\"foo\":\"bar\", \"@timestamp\":\"foobar\"}" }
168
+
169
+ it "should set timestamp to current time" do
170
+ expect(event.timestamp).to be_a(LogStash::Timestamp)
171
+ expect(event["tags"]).to include(LogStash::Event::TIMESTAMP_FAILURE_TAG)
172
+ expect(event[LogStash::Event::TIMESTAMP_FAILURE_FIELD]).to eq("foobar")
173
+ end
174
+ end
153
175
  end
154
176
  end
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- requirement: !ruby/object:Gem::Requirement
14
+ name: logstash-core
15
+ version_requirements: !ruby/object:Gem::Requirement
15
16
  requirements:
16
17
  - - '>='
17
18
  - !ruby/object:Gem::Version
@@ -19,10 +20,7 @@ dependencies:
19
20
  - - <
20
21
  - !ruby/object:Gem::Version
21
22
  version: 3.0.0
22
- name: logstash-core
23
- prerelease: false
24
- type: :runtime
25
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: !ruby/object:Gem::Requirement
26
24
  requirements:
27
25
  - - '>='
28
26
  - !ruby/object:Gem::Version
@@ -30,20 +28,22 @@ dependencies:
30
28
  - - <
31
29
  - !ruby/object:Gem::Version
32
30
  version: 3.0.0
31
+ prerelease: false
32
+ type: :runtime
33
33
  - !ruby/object:Gem::Dependency
34
- requirement: !ruby/object:Gem::Requirement
34
+ name: logstash-devutils
35
+ version_requirements: !ruby/object:Gem::Requirement
35
36
  requirements:
36
37
  - - '>='
37
38
  - !ruby/object:Gem::Version
38
39
  version: '0'
39
- name: logstash-devutils
40
- prerelease: false
41
- type: :development
42
- version_requirements: !ruby/object:Gem::Requirement
40
+ requirement: !ruby/object:Gem::Requirement
43
41
  requirements:
44
42
  - - '>='
45
43
  - !ruby/object:Gem::Version
46
44
  version: '0'
45
+ prerelease: false
46
+ type: :development
47
47
  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
48
48
  email: info@elastic.co
49
49
  executables: []