From 2fe2a9c9f2110ddff149f58b3b3f1d15b4efb702 Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Thu, 11 Jan 2024 23:23:51 -0800 Subject: [PATCH] add session start time metric (#2377) --- pkg/rtc/participant.go | 2 ++ pkg/rtc/participant_internal_test.go | 1 + pkg/service/roommanager.go | 3 +++ pkg/telemetry/prometheus/rooms.go | 14 ++++++++++++++ 4 files changed, 20 insertions(+) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index b6d63e334..06171a837 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -97,6 +97,7 @@ type ParticipantParams struct { AudioConfig config.AudioConfig VideoConfig config.VideoConfig ProtocolVersion types.ProtocolVersion + SessionStartTime time.Time Telemetry telemetry.TelemetryService Trailer []byte PLIThrottleConfig config.PLIThrottleConfig @@ -1421,6 +1422,7 @@ func (p *ParticipantImpl) onPrimaryTransportInitialConnected() { } func (p *ParticipantImpl) onPrimaryTransportFullyEstablished() { + prometheus.RecordSessionStartTime(int(p.ProtocolVersion()), time.Since(p.params.SessionStartTime)) p.updateState(livekit.ParticipantInfo_ACTIVE) } diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index afd312bc8..a1b758ff8 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -753,6 +753,7 @@ func newParticipantForTestWithOpts(identity livekit.ParticipantIdentity, opts *p Config: rtcConf, Sink: &routingfakes.FakeMessageSink{}, ProtocolVersion: opts.protocolVersion, + SessionStartTime: time.Now(), PLIThrottleConfig: conf.RTC.PLIThrottle, Grants: grants, PublishEnabledCodecs: enabledCodecs, diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 24feab876..6d122fd6e 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -245,6 +245,8 @@ func (r *RoomManager) StartSession( requestSource routing.MessageSource, responseSink routing.MessageSink, ) error { + sessionStartTime := time.Now() + room, err := r.getOrCreateRoom(ctx, roomName) if err != nil { return err @@ -390,6 +392,7 @@ func (r *RoomManager) StartSession( AudioConfig: r.config.Audio, VideoConfig: r.config.Video, ProtocolVersion: pv, + SessionStartTime: sessionStartTime, Telemetry: r.telemetry, Trailer: room.Trailer(), PLIThrottleConfig: r.config.RTC.PLIThrottle, diff --git a/pkg/telemetry/prometheus/rooms.go b/pkg/telemetry/prometheus/rooms.go index ef320ad73..2aaff88e2 100644 --- a/pkg/telemetry/prometheus/rooms.go +++ b/pkg/telemetry/prometheus/rooms.go @@ -15,6 +15,7 @@ package prometheus import ( + "strconv" "time" "github.com/prometheus/client_golang/prometheus" @@ -43,6 +44,7 @@ var ( promTrackSubscribedCurrent *prometheus.GaugeVec promTrackPublishCounter *prometheus.CounterVec promTrackSubscribeCounter *prometheus.CounterVec + promSessionStartTime prometheus.HistogramVec ) func initRoomStats(nodeID string, nodeType livekit.NodeType, env string) { @@ -91,6 +93,13 @@ func initRoomStats(nodeID string, nodeType livekit.NodeType, env string) { Name: "subscribe_counter", ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env}, }, []string{"state", "error"}) + promSessionStartTime = *prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: livekitNamespace, + Subsystem: "session", + Name: "start_time_ms", + ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env}, + Buckets: prometheus.ExponentialBucketsRange(100, 5000, 10), + }, []string{"protocol_version"}) prometheus.MustRegister(promRoomCurrent) prometheus.MustRegister(promRoomDuration) @@ -99,6 +108,7 @@ func initRoomStats(nodeID string, nodeType livekit.NodeType, env string) { prometheus.MustRegister(promTrackSubscribedCurrent) prometheus.MustRegister(promTrackPublishCounter) prometheus.MustRegister(promTrackSubscribeCounter) + prometheus.MustRegister(promSessionStartTime) } func RoomStarted() { @@ -172,3 +182,7 @@ func RecordTrackSubscribeFailure(err error, isUserError bool) { trackSubscribeUserError.Inc() } } + +func RecordSessionStartTime(protocolVersion int, d time.Duration) { + promSessionStartTime.WithLabelValues(strconv.Itoa(protocolVersion)).Observe(float64(d.Milliseconds())) +}