Context

When charting Azure Front Door metrics, stored latency samples all came out as 0 ms, even though the hour had responses that took several seconds.

Insight

For an Average metric such as TotalLatency, Azure Monitor returns average = 0 for every time bucket with no requests. It doesn’t return null and doesn’t leave the point out. The newest minutes of a query window also come back as zeros before their data is ingested.

So taking the latest bucket, or averaging the series naively, reads idle time as perfect latency. With append-only history, the wrong zeros then stay for the whole retention period.

Fix

  • Fetch RequestCount for the same buckets.
  • Count latency only where the bucket has at least one request.
  • Combine buckets, and dimension series such as per-endpoint splits, as a request-weighted average.
  • When the window had no requests, write no sample at all, not a 0.
# per bucket (and per dimension series): (avg_latency_ms, request_count)
pairs = [(lat, req) for lat, req in buckets if req and req > 0]
if pairs:
weighted = sum(lat * req for lat, req in pairs) / sum(req for _, req in pairs)
store(weighted)
# else: store nothing; no requests is not 0 ms latency