解决 Prometheus 报错 duplicate sample for timestamp 异常
解决 Prometheus 写入时报 duplicate sample for timestamp 或 out of order sample 的时间戳倒流排查与乱序写入开启方案。
问题现象
Prometheus 接收 Pushgateway 或 Remote Write 写入指标时抛出错误:
err="duplicate sample for timestamp"
# 或
err="out of order sample"
时序数据被丢弃,监控图表出现断点。
触发根因
- 同一个时间序列(Metric Name + 相同标签集合)在同一个毫秒级时间戳被重复提交了不同的浮点数值。
- 多个客户端实例使用了完全相同的 Label 标识向同一个 Pushgateway 推送数据,导致相互冲突覆盖。
- 采集端系统时钟发生了回退(NTP 跳变)。
解决步骤与操作命令
步骤一:为指标增加唯一性区分标签
在 Pushgateway 推送时,必须通过 URL 路径附加 instance 区分实例:
echo "cpu_usage 42.5" | curl --data-binary @- http://pushgateway:9091/metrics/job/my_job/instance/worker-node-1
步骤二:在 Prometheus 中开启乱序时序写入容忍(TSDB Out-of-order)
Prometheus 2.39+ 原生支持有限窗口内的乱序数据写入。在启动参数或配置中开启:
# 在 prometheus.yml 或启动命令行中开启
storage:
tsdb:
out_of_order_time_window: 10m
启动参数方式:
--storage.tsdb.out-of-order-time-window=10m
步骤三:重载服务生效
curl -X POST http://localhost:9090/-/reload
验证确认
观察指标 prometheus_tsdb_out_of_order_samples_total:
被丢弃的样本数停止增长,数据成功被 TSDB 乱序缓存吸收,监控曲线连续平滑。


