Grafana 使用 histogram_quantile 和 rate 的精度问题

Grafana 上如果观测离群点,会发现它的值漂移地很厉害。往往一个实际 2min 的指标能显示出是几个 hour。
https://github.com/pingcap/tiflash/issues/8076 所述。这个问题发生需要同时使用 histogram_quantile 和 rate。

其原因是 rate 会将 count 除成小数,因为 IEEE 浮点数不能精确表示,所以引入了噪音数据。
如下所示,8.192 和 67108.864 这两个桶对应的 sum 应该是相等的。但因为浮点数加法的问题,它们不相等了。因此这些立群值就会被放到 bucket 序号更大的桶里面了。

https://github.com/m3db/m3/issues/3706

The problem is that the rate function, while doing its magic, turns counts into fractions. Most fractions can’t be expressed exactly as a floating point number (IEEE754 standard). The resulting number that represents the fraction is just an approximation that uses up all bits of mantissa.

因为精度的问题,导致在某个 edge 上会进一位。所以可以用 histogram_quantile(1.0, sum(round(1000000000*rate(xxx{}[5m]))) by (le) / 1000000000) 这样来规避。

这也体现出浮点数的性质不咋样,连结合律都不满足。在工程上来讲,不满足结合律意味着没法分治。