out_file formatter with time_type float drops the fraction of a time under 0.1 second

未关闭 适合新手
#5,715 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
86/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
ruby
领域
backend, testing

调研方向

阅读 lib/fluent/time.rb 中的 NumericTimeFormatter#format_float,以及 test/test_time_formatter.rb 中现有的格式化器用例。为纳秒位数不足 9 位的分数时间添加测试,例如 1700000000.012345678,并验证输出保留全部 9 位小数。Issue 指出了受影响的方法和测试文件,报告中的预期值则定义了修复完成的标准。

由索引模型根据 Issue 内容生成。

描述

bug
Describe the bug

With time_type float, the out_file formatter drops the fractional part of a time when it is less than 0.1 second. For example, 1700000000.012345678 is written as 1700000000.0, and 1700000000.000005000 as 1700000000.00000.

NumericTimeFormatter#format_float should pad the nanoseconds with leading zeros to 9 digits. Instead, it replaces them with the zeros (time.rb L460):

nsec_s = time.nsec.to_s
nsec_s = '0' * (9 - nsec_s.size) if nsec_s.size < 9

This affects out_file and any output that uses <format> @type out_file with time_type float. time_type float in <inject> uses a different conversion and is not affected. The existing test only uses .123456789 (test_time_formatter.rb L356-L362).

To Reproduce
  1. Create the input file below.
  2. Run Fluentd with the configuration below.
{"time":"1700000000.123456789","case":"control"}
{"time":"1700000000.012345678","case":"under 0.1 s"}
{"time":"1700000000.000005000","case":"5 microseconds"}
{"time":"1700000000.000000000","case":"zero"}

Output of the out_file formatter on master (31157cba) and v1.20.1:

1700000000.123456789	test	{"time":"1700000000.123456789","case":"control"}
1700000000.0	test	{"time":"1700000000.012345678","case":"under 0.1 s"}
1700000000.00000	test	{"time":"1700000000.000005000","case":"5 microseconds"}
1700000000.00000000	test	{"time":"1700000000.000000000","case":"zero"}

With the same configuration, <inject> gives 1700000000.012345 and 1700000000.000005.

Expected behavior

The fractional part is padded to 9 digits:

1700000000.123456789
1700000000.012345678
1700000000.000005000
1700000000.000000000
Your Environment
- Fluentd version: master (31157cba) and v1.20.1
- Package version: -
- Operating system: Ubuntu 24.04.5 LTS
- Kernel version: 6.8.0-151-generic
- Ruby: 4.0.10
Your Configuration
<source>
  @type tail
  tag test
  path /path/to/input.log
  pos_file /path/to/input.pos
  read_from_head true
  <parse>
    @type json
    time_key time
    time_type string
    time_format %s.%N
    keep_time_key true
  </parse>
</source>

<match test>
  @type copy
  <store>
    @type stdout
    <format>
      @type out_file
      time_type float
    </format>
  </store>
  <store>
    @type stdout
    <inject>
      time_key float_time
      time_type float
    </inject>
  </store>
</match>
Your Error Log
No error is logged.
Additional context

A possible fix is nsec_s = nsec_s.rjust(9, '0') and a test with a time whose nanoseconds have fewer than 9 digits. The code has been like this since 3a8038d9 (2016).

主要语言
Ruby
星标
13.6k
派生
1.4k
平均合并
1 天 10 小时
30 天内合并 PR
79

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fluent/fluentd 的其他 Issue

查看 fluent/fluentd 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。