Class: LogStash::Codecs::Msgpack

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/codecs/msgpack.rb

Instance Method Summary collapse

Instance Method Details

#decode(data) {|event| ... } ⇒ Object

Yields:

  • (event)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/logstash/codecs/msgpack.rb', line 18

def decode(data)
  begin
    # Msgpack does not care about UTF-8
    event = LogStash::Event.new(MessagePack.unpack(data))
    
    if event.get("tags").nil?
      event.set("tags", [])
    end
    
    if @format
      if event.get("message").nil?
        event.set("message", event.sprintf(@format))
      end  
    end
  rescue => e
    # Treat as plain text and try to do the best we can with it?
    @logger.warn("Trouble parsing msgpack input, falling back to plain text",
                 :input => data, :exception => e)
    event.set("message", data)
    
    tags = event.get("tags").nil? ? [] : event.get("tags") 
    tags << "_msgpackparsefailure"
    event.set("tags", tags)
  end
  yield event
end

#encode(event) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/logstash/codecs/msgpack.rb', line 46

def encode(event)
  # use normalize to make sure returned Hash is pure Ruby for
  # MessagePack#pack which relies on pure Ruby object recognition
  data = LogStash::Util.normalize(event.to_hash)
  # timestamp is serialized as a iso8601 string
  # merge to avoid modifying data which could have side effects if multiple outputs
  @on_event.call(event, MessagePack.pack(data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601)))
end

#registerObject



13
14
15
# File 'lib/logstash/codecs/msgpack.rb', line 13

def register
  require "msgpack"
end