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

Abierto Apto para principiantes
#12,835 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c
Área
backend

Línea de trabajo

Lee msgpack2json_scalar() en src/flb_pack.c alrededor de la línea 1061, donde está la cadena de formato %.16g, y comprueba cómo se despachan las ramas float/double circundantes. Recompila fluent-bit y vuelve a ejecutar el comando dummy -> stdout json_lines del informe, o las pruebas de pack bajo tests/internal/, para confirmar el truncamiento antes de editar. Hecho significa que 1728517471.3315752 y 1728488902.8078704 sobreviven mientras que 0.1 sigue imprimiéndose como 0.1. Lee primero el PR abierto #12129, ya que cambia la misma cadena de formato y puede que ya cubra esto.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C
Estrellas
8.2k
Forks
2k
Merge medio
2 d 16 h
PR fusionados (30 d)
72

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de fluent/fluent-bit

Todos los issues de fluent/fluent-bit

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.