Skip to main content

Module audit_fmt

Module audit_fmt 

Source
Expand description

v1.4.90 P2-C: audit log Option 序列化助手。

背景:之前 audit log 把 Option<f64>?req.price(tracing 的 Debug shorthand)记录,渲染成 JSON 字符串 "Some(400.0)" / "None",下游 jq / DuckDB 数值聚合炸(aggregator 期望 400.0 number 或 null)。

修法:用 NaN sentinel 把 Option<f64> flatten 成 f64,tracing-subscriber 的 JSON formatter 内部走 serde_json::Value::from(f64::NAN)Number::from_f64(NaN) = NoneValue::Null。 整数 / 字符串同理(i32 → f64 NaN sentinel;&str → “” 哨兵)。

验证依据:

  • tracing_subscriber::fmt::format::json line 501 record_f64 直接调 serde_json::Value::from(value)
  • serde_json::Value::from(f64) impl: Number::from_f64(f).map_or(Value::Null, Value::Number)

Functions§

opt_f64
Option<f64>f64(None → NaN)。tracing JSON 渲染 NaN 为 null
opt_i32
Option<i32>f64(None → NaN,Some(n) → n as f64)。 i32 ≤ 2^31 < 2^52 mantissa,无精度损失。
opt_str
Option<&str>&str(None → “”)。“” 哨兵在 audit 上下文里足以区分 不传 vs 传空(因为 Symbol / owner 等业务字段不会是空字符串)。