pack: JSON output rounds doubles to 16 significant digits (%.16g) and loses precision

オープン 初心者向け
#12,835 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend

調査の方向性

msgpack2json_scalar() を src/flb_pack.c の 1061 行目付近で読み、%.16g フォーマット文字列がある箇所と、周囲の float/double 分岐がどのようにディスパッチされるかを確認する。編集前に切り捨てを確認するため、fluent-bit を再ビルドし、レポートにある dummy -> stdout json_lines コマンド、または tests/internal/ 以下の pack テストを再実行する。完了とは、1728517471.3315752 と 1728488902.8078704 が保持され、0.1 が引き続き 0.1 として出力されることを意味する。同じフォーマット文字列を変更し、すでにこれをカバーしている可能性があるため、まずオープン PR #12129 を読む。

索引モデルが issue の本文から書いたものです。

説明

Bug Report

Describe the bug

When a record is converted from msgpack to JSON, non-integral doubles are printed with %.16g (msgpack2json_scalar() in src/flb_pack.c):

https://github.com/fluent/fluent-bit/blob/4aa872100d58f0a531a7193b73c0b603c7fa444c/src/flb_pack.c#L1061

A double can need 17 significant digits to round-trip. Any value that needs the 17th digit is silently rounded, and the number in the JSON output parses back to a different double than the one in the record.

For example, 1728517471.3315752 is emitted as 1728517471.331575.

This affects every output that builds JSON through flb_msgpack_raw_to_json_sds() / flb_pack_msgpack_to_json_format(), including stdout (json, json_lines) and out_stackdriver.

To Reproduce

  • Example log message if applicable:
{"ts":1728517471.3315752,"a":0.1,"b":1728488902.8078704}
  • Steps to reproduce the problem:
docker run --rm fluent/fluent-bit:5.2.0 /fluent-bit/bin/fluent-bit -q -f 1 \
  -i dummy -p 'dummy={"ts":1728517471.3315752,"a":0.1,"b":1728488902.8078704}' -p samples=1 \
  -o stdout -p format=json_lines

Output:

{"date":1795462835.007555,"ts":1728517471.331575,"a":0.1,"b":1728488902.80787}
Field Input Output
ts 1728517471.3315752 1728517471.331575
b 1728488902.8078704 1728488902.80787
a 0.1 0.1

Expected behavior

A double keeps its value through the msgpack to JSON conversion, so the output contains "ts":1728517471.3315752 and "b":1728488902.8078704.

Your Environment

  • Version used: 5.2.0 (official container image, git commit e0149076aeff009d35fd495e55379cb78f1eb53f). The same line is on master at 4aa872100d58f0a531a7193b73c0b603c7fa444c. We first saw it on 4.2.8.
  • Configuration: see the command above.
  • Environment name and version (e.g. Kubernetes? What version?): Docker for the reproduction. We first saw it on Kubernetes.
  • Server type and version: N/A
  • Operating System and version: Linux
  • Filters and plugins: dummy input and stdout output for the reproduction. We first saw it with tail and out_stackdriver.

Additional context

We hit this with out_stackdriver. Applications that log an epoch-seconds timestamp as a float, such as a ts field of 1728517471.3315752, have 10 digits before the decimal point, so only 6 digits after it survive. The value stored in Cloud Logging then differs from the value the application wrote. Any other float field that needs 17 significant digits is affected in the same way.

%.16g was introduced in #1439, replacing %f, on the assumption that 16 digits are enough. 15 significant digits is the most that always survives decimal to double to decimal. Going from a double to decimal and back to the same double can need 17.

Related:

  • #6447 reports a different symptom from the same format string. It was closed by the stale bot.
  • #12129 (open) replaces %.16g with shortest round-trip formatting. From its description it should fix this case as well. I have not tested it against the values above.

Changing the format string to %.17g always round-trips, but it prints 0.1 as 0.10000000000000001. Shortest round-trip formatting, as in #12129, avoids that.

主要言語
C
スター
8.2k
フォーク
2k
平均マージ
2日 16時間
マージ済み PR(30日)
72

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

fluent/fluent-bit のほかの issue

fluent/fluent-bit の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。