mirror of
https://github.com/elastic/logstash.git
synced 2025-04-23 06:08:19 -04:00
[oldlogstashjson] simplify decoding from v0 to v1
This commit is contained in:
parent
176bd664f8
commit
25dc988a5d
2 changed files with 9 additions and 11 deletions
|
@ -5,6 +5,12 @@ class LogStash::Codecs::OldLogStashJSON < LogStash::Codecs::Base
|
|||
config_name "oldlogstashjson"
|
||||
milestone 1
|
||||
|
||||
# Map from v0 name to v1 name.
|
||||
# Note: @source is gone and has no similar field.
|
||||
V0_TO_V1 = {"@timestamp" => "@timestamp", "@message" => "message",
|
||||
"@tags" => "tags", "@type" => "type",
|
||||
"@source_host" => "host", "@source_path" => "path"}
|
||||
|
||||
public
|
||||
def decode(data)
|
||||
obj = JSON.parse(data.force_encoding("UTF-8"))
|
||||
|
@ -12,19 +18,10 @@ class LogStash::Codecs::OldLogStashJSON < LogStash::Codecs::Base
|
|||
h = {}
|
||||
|
||||
# Convert the old logstash schema to the new one.
|
||||
basics = %w(@message @tags @type)
|
||||
basics.each do |key|
|
||||
# Convert '@message' to 'message', etc
|
||||
h[key[1..-1]] = obj[key] if obj.include?(key)
|
||||
V0_TO_V1.each do |key, val|
|
||||
h[val] = obj[key] if obj.include?(key)
|
||||
end
|
||||
|
||||
# fix other mappings
|
||||
h["host"] = obj["@source_host"]
|
||||
h["path"] = obj["@source_path"]
|
||||
# Note: @source is gone and has no similar field.
|
||||
|
||||
h["@timestamp"] = obj["@timestamp"] if obj.include?("@timestamp")
|
||||
|
||||
h.merge!(obj["@fields"]) if obj["@fields"].is_a?(Hash)
|
||||
yield LogStash::Event.new(h)
|
||||
end # def decode
|
||||
|
|
|
@ -18,6 +18,7 @@ describe LogStash::Codecs::OldLogStashJSON do
|
|||
insist { event["message"] } == data["@message"]
|
||||
insist { event["host"] } == data["@source_host"]
|
||||
insist { event["tags"] } == data["@tags"]
|
||||
insist { event["path"] } != nil # @source_path not in v0 test data
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue