Skip to content

Conversation

@Watson1978
Copy link
Contributor

@Watson1978 Watson1978 commented Nov 25, 2025

Related to #111

Problem

Currently, the code uses JSON.generate(msg) in JSON fallback. This forces a standard JSON serialization, potentially ignoring custom serialization logic defined within the objects themselves.

Solution

Change the implementation to use msg.to_json.
JSON.parse(msg.to_json).to_msgpack

This allows objects to control their own string representation via their to_json method before being parsed back into a Hash for MessagePack conversion.

Context & Benefits

This change ensures that if an object has a custom to_json implementation, it is respected.

Example: Active Support (Rails)
A major beneficiary of this change is environments where Active Support is loaded.
In Active Support, to_json delegates to as_json, which handles:

  • Non-standard objects (like Symbols, specific class instances).
  • Date/Time formatting (e.g., ISO 8601).
  • Custom logic defined in models.

By using msg.to_json, we ensure that logs forwarded by fluent-logger-ruby match the expected JSON format defined by the application/framework, rather than the raw internal representation.

Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
@Watson1978 Watson1978 marked this pull request as ready for review November 25, 2025 08:20
@Watson1978 Watson1978 changed the title Improve ActiveSupport Supports ADd ActiveSupport Supports Nov 27, 2025
@Watson1978 Watson1978 changed the title ADd ActiveSupport Supports Add ActiveSupport Supports Nov 27, 2025
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
Copy link
Contributor

@daipom daipom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Since the Active Support topic suddenly appears, it’s a bit hard to follow, so I’d like to clarify the purpose of this PR.
My understanding is that #111 is not directly related to Active Support itself, but it would be more convenient to align with the JSON format definition provided by Active Support.

This improvement would be useful. Looks good to me.

However, it seems a part of #111 is still unresolved.
I’m particularly concerned about how the timestamps of the Flunetd Event itself are handled.
In particular, when using nanosecond_precision, I’m concerned about what happens to the milliseconds of the Event’s timestamp if a JSON fallback occurs.

def to_json(*args)
@sec.to_s
end

It seems that #111 should remain open.

Comment on lines 52 to 55

def as_json(*args)
@sec
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we should leave a comment on why we need this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.
ec11c6d

@daipom
Copy link
Contributor

daipom commented Nov 27, 2025

Since the Active Support topic suddenly appears, it’s a bit hard to follow, so I’d like to clarify the purpose of this PR.

The title change has made it much clearer. Thanks!

@Watson1978
Copy link
Contributor Author

In particular, when using nanosecond_precision, I’m concerned about what happens to the milliseconds of the Event’s timestamp if a JSON fallback occurs.

Even with nanosecond_precision enabled, EventTime's nsec values seem to be truncated when JSON fallback occurs.

Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
@Watson1978
Copy link
Contributor Author

Since the Active Support topic suddenly appears, it’s a bit hard to follow, so I’d like to clarify the purpose of this PR.
My understanding is that #111 is not directly related to Active Support itself, but it would be more convenient to align with the JSON format definition provided by Active Support.

The primary purpose of this PR is to respect the object's own to_json implementation instead of forcing JSON.generate. This allows any Ruby object (not just in Rails) to control its own JSON serialization format.

Active Support is just the most prominent use case where to_json behavior differs significantly from JSON.generate. I will update the description to clarify that the main goal is generic to_json support, with Active Support cited as a key example.

@daipom
Copy link
Contributor

daipom commented Nov 27, 2025

Thanks!

@daipom daipom merged commit 02a0285 into fluent:master Nov 27, 2025
7 checks passed
@Watson1978 Watson1978 deleted the activesupport branch November 27, 2025 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants