Skip to content

Discard the millisecond of the Time value #111

@Watson1978

Description

@Watson1978

When it fails to serialize data to MsgPack format,
it falls back to part using JSON.parse(JSON.generate(msg)).to_msgpack.

def to_msgpack(msg)
@mon.synchronize {
res = begin
@packer.pack(msg).to_s
rescue NoMethodError
JSON.parse(JSON.generate(msg)).to_msgpack
ensure
@packer.clear
end
res
}
end

The fallback part discard the millisecond of the Time value.
There are cases where people dislike it.

Example:

require "bundler/inline"
gemfile do
  source "https://rubygems.org"
  gem "fluent-logger"
  gem "activesupport"
end

require 'active_support/json'

log = Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port => 24224)

log.post("myapp.access", { user: "foo", created_at: Time.now })
log.post("myapp.access", { user: "foo", created_at: Time.now.as_json })

Beforehand, it preserves milliseconds converting with as_json.
(If active_support is loaded, to_json will invoke as_json automatically.)

This behavior is preferred by default.

2025-09-19 11:54:11.000000000 +0900 incoming: {"user":"foo","created_at":"2025-09-19 11:54:11 +0900"}
2025-09-19 11:54:11.000000000 +0900 incoming: {"user":"foo","created_at":"2025-09-19T11:54:11.828+09:00"}

We need to regist custom MsgPack packer for Time object, or something...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions