fix: cap percentile at last finite bucket when target falls in open +Inf bucket

This commit is contained in:
2026-03-18 18:38:03 -04:00
parent c336f1f774
commit 8ea2b09d05
+6 -1
View File
@@ -8,6 +8,7 @@ Traefik exposes Prometheus metrics at /metrics. This module:
and latency percentiles (p50, p95, p99) via linear histogram interpolation
"""
from __future__ import annotations
import math
import re
from collections import defaultdict
@@ -171,10 +172,14 @@ def _percentile_ms(
prev_le, prev_count = router_buckets[i - 1]
if count == prev_count:
return prev_le * 1000.0
if math.isinf(le):
# Percentile falls in the open (last_finite_le, +Inf] bucket;
# cap at the last finite bucket upper bound.
return prev_le * 1000.0
fraction = (target - prev_count) / (count - prev_count)
return (prev_le + fraction * (le - prev_le)) * 1000.0
return router_buckets[-2][0] * 1000.0 # last finite bucket upper bound
return 0.0 # unreachable: +Inf bucket always satisfies count >= target
# ──────────────────────────────────────────────────────────────────────────────