[oldlogstashjson] simplify decoding from v0 to v1

This commit is contained in:
Brad Fritz 2013-11-20 00:06:23 -05:00
parent 176bd664f8
commit 25dc988a5d
2 changed files with 9 additions and 11 deletions

View file

@ -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

View file

@ -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