Higher resolution forwarding latency histogram. (#4067)

* Higher resolution forwarding latency histogram.

Was using the average latency/jitter of last second to populate
forwarding latency/jitter histogram. But, it is too coarse, i. e. the
average value of latency/jitter is very low and those summarised samples
end up in the lowest bucket always.

A few things to address it
- record per packet forwarding latency in histogram
- adjust histogram bins to include smaller values
- Drop jitter histogram

This is a per packet call, but prometheus histogram is supposedly
fast/light weight. Would be good to get better resolution histograms.
Hence doing this. Please let me know if there are performance concerns.

* typo

* one more typo
This commit is contained in:
Raja Subramanian
2025-11-09 17:29:40 +05:30
committed by GitHub
parent 858db7ab7a
commit 4ce07bedeb
2 changed files with 18 additions and 14 deletions
+1
View File
@@ -49,6 +49,7 @@ func (s *ForwardStats) Update(arrival, left int64) (int64, bool) {
s.lastUpdateAt = arrival s.lastUpdateAt = arrival
s.lock.Unlock() s.lock.Unlock()
prometheus.RecordForwardLatencySample(transit)
return transit, isHighForwardingLatency return transit, isHighForwardingLatency
} }
+17 -14
View File
@@ -68,7 +68,6 @@ var (
promForwardLatency prometheus.Gauge promForwardLatency prometheus.Gauge
promForwardJitter prometheus.Gauge promForwardJitter prometheus.Gauge
promForwardLatencyHist prometheus.Histogram promForwardLatencyHist prometheus.Histogram
promForwardJitterHist prometheus.Histogram
promPacketTotalIncomingInitial prometheus.Counter promPacketTotalIncomingInitial prometheus.Counter
promPacketTotalIncomingRetransmit prometheus.Counter promPacketTotalIncomingRetransmit prometheus.Counter
@@ -181,16 +180,19 @@ func initPacketStats(nodeID string, nodeType livekit.NodeType) {
Subsystem: "forward_latency", Subsystem: "forward_latency",
Name: "ns", Name: "ns",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()}, ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()},
// 1ms, 2ms, 3ms, 4ms, 5ms, 10ms, 20ms // 50us, 100us, 250us, 500us, 1ms, 2ms, 3ms, 5ms, 10ms, 20ms
Buckets: []float64{1000000, 2000000, 3000000, 4000000, 5000000, 10000000, 20000000}, Buckets: []float64{
}) 50 * 1000,
promForwardJitterHist = prometheus.NewHistogram(prometheus.HistogramOpts{ 100 * 1000,
Namespace: livekitNamespace, 250 * 1000,
Subsystem: "forward_jitter", 500 * 1000,
Name: "ns", 1 * 1000 * 1000,
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()}, 2 * 1000 * 1000,
// 1ms, 2ms, 3ms, 4ms, 5ms, 10ms, 20ms 3 * 1000 * 1000,
Buckets: []float64{1000000, 2000000, 3000000, 4000000, 5000000, 10000000, 20000000}, 5 * 1000 * 1000,
10 * 1000 * 1000,
20 * 1000 * 1000,
},
}) })
prometheus.MustRegister(promPacketTotal) prometheus.MustRegister(promPacketTotal)
@@ -209,7 +211,6 @@ func initPacketStats(nodeID string, nodeType livekit.NodeType) {
prometheus.MustRegister(promForwardLatency) prometheus.MustRegister(promForwardLatency)
prometheus.MustRegister(promForwardJitter) prometheus.MustRegister(promForwardJitter)
prometheus.MustRegister(promForwardLatencyHist) prometheus.MustRegister(promForwardLatencyHist)
prometheus.MustRegister(promForwardJitterHist)
} }
func IncrementPackets(country string, direction Direction, count uint64, retransmit bool) { func IncrementPackets(country string, direction Direction, count uint64, retransmit bool) {
@@ -335,14 +336,16 @@ func SubConnection(direction Direction) {
promConnections.WithLabelValues(string(direction)).Sub(1) promConnections.WithLabelValues(string(direction)).Sub(1)
} }
func RecordForwardLatencySample(forwardLatency int64) {
promForwardLatencyHist.Observe(float64(forwardLatency))
}
func RecordForwardLatency(shortTermLatencyAvg, longTermLatencyAvg uint32) { func RecordForwardLatency(shortTermLatencyAvg, longTermLatencyAvg uint32) {
forwardLatency.Store(longTermLatencyAvg) forwardLatency.Store(longTermLatencyAvg)
promForwardLatency.Set(float64(longTermLatencyAvg)) promForwardLatency.Set(float64(longTermLatencyAvg))
promForwardLatencyHist.Observe(float64(shortTermLatencyAvg))
} }
func RecordForwardJitter(shortTermJitterAvg, longTermJitterAvg uint32) { func RecordForwardJitter(shortTermJitterAvg, longTermJitterAvg uint32) {
forwardJitter.Store(longTermJitterAvg) forwardJitter.Store(longTermJitterAvg)
promForwardJitter.Set(float64(longTermJitterAvg)) promForwardJitter.Set(float64(longTermJitterAvg))
promForwardJitterHist.Observe(float64(shortTermJitterAvg))
} }