From 8a229fda9da589ab9525745d7b82b71571a6f282 Mon Sep 17 00:00:00 2001 From: Lukas Herman Date: Mon, 17 Jun 2024 17:52:08 -0400 Subject: [PATCH] add participant session duration metric (#2801) --- pkg/rtc/participant.go | 6 ++++++ pkg/telemetry/prometheus/rooms.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index e7e847291..ed64cda3b 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -164,6 +164,7 @@ type ParticipantImpl struct { isPublisher atomic.Bool sessionStartRecorded atomic.Bool + lastActiveAt time.Time // when first connected connectedAt time.Time // timer that's set when disconnect is detected on primary PC @@ -1375,6 +1376,11 @@ func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) { return } + if state == livekit.ParticipantInfo_DISCONNECTED && oldState == livekit.ParticipantInfo_ACTIVE { + prometheus.RecordSessionDuration(int(p.ProtocolVersion()), time.Since(p.lastActiveAt)) + } else if state == livekit.ParticipantInfo_ACTIVE { + p.lastActiveAt = time.Now() + } p.params.Logger.Debugw("updating participant state", "state", state.String()) p.dirty.Store(true) diff --git a/pkg/telemetry/prometheus/rooms.go b/pkg/telemetry/prometheus/rooms.go index 6c7540d78..7e1102026 100644 --- a/pkg/telemetry/prometheus/rooms.go +++ b/pkg/telemetry/prometheus/rooms.go @@ -45,6 +45,7 @@ var ( promTrackPublishCounter *prometheus.CounterVec promTrackSubscribeCounter *prometheus.CounterVec promSessionStartTime *prometheus.HistogramVec + promSessionDuration *prometheus.HistogramVec ) func initRoomStats(nodeID string, nodeType livekit.NodeType) { @@ -100,6 +101,13 @@ func initRoomStats(nodeID string, nodeType livekit.NodeType) { ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()}, Buckets: prometheus.ExponentialBucketsRange(100, 10000, 15), }, []string{"protocol_version"}) + promSessionDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: livekitNamespace, + Subsystem: "session", + Name: "duration_ms", + ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()}, + Buckets: prometheus.ExponentialBucketsRange(100, 4*60*60*1000, 15), + }, []string{"protocol_version"}) prometheus.MustRegister(promRoomCurrent) prometheus.MustRegister(promRoomDuration) @@ -109,6 +117,7 @@ func initRoomStats(nodeID string, nodeType livekit.NodeType) { prometheus.MustRegister(promTrackPublishCounter) prometheus.MustRegister(promTrackSubscribeCounter) prometheus.MustRegister(promSessionStartTime) + prometheus.MustRegister(promSessionDuration) } func RoomStarted() { @@ -186,3 +195,7 @@ func RecordTrackSubscribeFailure(err error, isUserError bool) { func RecordSessionStartTime(protocolVersion int, d time.Duration) { promSessionStartTime.WithLabelValues(strconv.Itoa(protocolVersion)).Observe(float64(d.Milliseconds())) } + +func RecordSessionDuration(protocolVersion int, d time.Duration) { + promSessionDuration.WithLabelValues(strconv.Itoa(protocolVersion)).Observe(float64(d.Milliseconds())) +}