|
23 | 23 |
|
24 | 24 | module Fluent |
25 | 25 | module Logger |
| 26 | + class EventTime |
| 27 | + TYPE = 0 |
| 28 | + |
| 29 | + def initialize(raw_time) |
| 30 | + @time = raw_time |
| 31 | + end |
| 32 | + |
| 33 | + def to_msgpack(io = nil) |
| 34 | + @time.sec.to_msgpack(io) |
| 35 | + end |
| 36 | + |
| 37 | + def to_msgpack_ext |
| 38 | + [@time.sec, @time.nsec].pack('NN') |
| 39 | + end |
| 40 | + |
| 41 | + def self.from_msgpack_ext(data) |
| 42 | + new(*data.unpack('NN')) |
| 43 | + end |
| 44 | + |
| 45 | + def to_json(*args) |
| 46 | + @time.sec |
| 47 | + end |
| 48 | + end |
| 49 | + |
26 | 50 | class FluentLogger < LoggerBase |
27 | 51 | BUFFER_LIMIT = 8*1024*1024 |
28 | 52 | RECONNECT_WAIT = 0.5 |
@@ -55,6 +79,13 @@ def initialize(tag_prefix = nil, *args) |
55 | 79 | @host = options[:host] |
56 | 80 | @port = options[:port] |
57 | 81 | @socket_path = options[:socket_path] |
| 82 | + @nanosecond_precision = options[:nanosecond_precision] |
| 83 | + |
| 84 | + factory = MessagePack::Factory.new |
| 85 | + if @nanosecond_precision |
| 86 | + factory.register_type(EventTime::TYPE, EventTime) |
| 87 | + end |
| 88 | + @packer = factory.packer |
58 | 89 |
|
59 | 90 | @mon = Monitor.new |
60 | 91 | @pending = nil |
@@ -98,7 +129,11 @@ def last_error |
98 | 129 | def post_with_time(tag, map, time) |
99 | 130 | @logger.debug { "event: #{tag} #{map.to_json}" rescue nil } if @logger.debug? |
100 | 131 | tag = "#{@tag_prefix}.#{tag}" if @tag_prefix |
101 | | - write [tag, time.to_i, map] |
| 132 | + if @nanosecond_precision && time.is_a?(Time) |
| 133 | + write [tag, EventTime.new(time), map] |
| 134 | + else |
| 135 | + write [tag, time.to_i, map] |
| 136 | + end |
102 | 137 | end |
103 | 138 |
|
104 | 139 | def close |
@@ -147,7 +182,9 @@ def pending_bytesize |
147 | 182 |
|
148 | 183 | def to_msgpack(msg) |
149 | 184 | begin |
150 | | - msg.to_msgpack |
| 185 | + res = @packer.pack(msg).to_s |
| 186 | + @packer.clear |
| 187 | + res |
151 | 188 | rescue NoMethodError |
152 | 189 | JSON.parse(JSON.generate(msg)).to_msgpack |
153 | 190 | end |
|
0 commit comments