logstash-filter-json 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/README.md +3 -0
- data/lib/logstash/filters/json.rb +45 -35
- data/logstash-filter-json.gemspec +1 -1
- data/spec/filters/json_spec.rb +45 -23
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40d60f723d2d12a8b176d226b14d27e4f7b59adf
|
4
|
+
data.tar.gz: 09b010f8cd92b795c6af10eaf4b07ef908515768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c2bc48511c1b4cae1f9c776479cde3b5558b9e7055971938e1329b5028af7ca6f645049216c4fa0dc0f43003f3ef458e79a6c46469ffe419c93a95882c37d3b
|
7
|
+
data.tar.gz: 4ad8488fb033dcd70f161e62f3a41f457650fbec31612dc177893af0867431f16936f3d564bf640ecad9e3f7a21f72896331ad7c5426e6862d7e4e4c9eaebc6a
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
49
|
+
JSONPARSEFAILURE_TAG = "_jsonparsefailure"
|
50
|
+
|
50
51
|
def register
|
51
52
|
# Nothing to do here
|
52
|
-
end
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
#
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
96
|
-
|
97
|
-
end # def filter
|
105
|
+
filter_matched(event)
|
98
106
|
|
99
|
-
|
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.
|
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"
|
data/spec/filters/json_spec.rb
CHANGED
@@ -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 "
|
108
|
+
context "using message field source" do
|
107
109
|
|
108
110
|
subject(:filter) { LogStash::Filters::Json.new(config) }
|
109
111
|
|
110
|
-
let(:
|
111
|
-
let(:
|
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
|
-
|
120
|
-
|
121
|
-
end
|
120
|
+
context "when json could not be parsed" do
|
121
|
+
let(:message) { "random_message" }
|
122
122
|
|
123
|
-
|
124
|
-
|
125
|
-
|
123
|
+
it "add the failure tag" do
|
124
|
+
expect(event).to include("tags")
|
125
|
+
end
|
126
126
|
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
-
|
135
|
+
context "there are two different errors added" do
|
132
136
|
|
133
|
-
|
137
|
+
let(:event) { LogStash::Event.new("message" => message, "tags" => ["_anotherkinfoffailure"] ) }
|
134
138
|
|
135
|
-
|
136
|
-
|
137
|
-
|
139
|
+
it "pile the different error messages" do
|
140
|
+
expect(event['tags']).to include("_jsonparsefailure")
|
141
|
+
end
|
138
142
|
|
139
|
-
|
140
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2015-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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: []
|