use shared psrpc utils (#2506)

* use shared psrpc utils

* fix

* deps
This commit is contained in:
Paul Wells
2024-02-24 00:38:49 -08:00
committed by GitHub
parent 013c933636
commit e5b8e25064
10 changed files with 34 additions and 163 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ func NewSignalClient(nodeID livekit.NodeID, bus psrpc.MessageBus, config config.
c, err := rpc.NewTypedSignalClient(
nodeID,
bus,
middleware.WithClientMetrics(prometheus.PSRPCMetricsObserver{}),
middleware.WithClientMetrics(rpc.PSRPCMetricsObserver{}),
psrpc.WithClientChannelSize(config.StreamBufferSize),
)
if err != nil {
+5 -9
View File
@@ -168,16 +168,12 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
// owner for the data, we'd prefer to use their TimedVersion
// ignore older version
if !timedVersion.After(&u.subscriptionPermissionVersion) {
perms := ""
if u.subscriptionPermission != nil {
perms = u.subscriptionPermission.String()
}
u.params.Logger.Debugw(
"skipping older subscription permission version",
"existingValue", perms,
"existingVersion", u.subscriptionPermissionVersion.String(),
"existingValue", logger.Proto(u.subscriptionPermission),
"existingVersion", &u.subscriptionPermissionVersion,
"requestingValue", logger.Proto(subscriptionPermission),
"requestingVersion", timedVersion.String(),
"requestingVersion", &timedVersion,
)
u.lock.Unlock()
return nil
@@ -193,7 +189,7 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
if subscriptionPermission == nil {
u.params.Logger.Debugw(
"updating subscription permission, setting to nil",
"version", u.subscriptionPermissionVersion.String(),
"version", &u.subscriptionPermissionVersion,
)
// possible to get a nil when migrating
u.lock.Unlock()
@@ -203,7 +199,7 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
u.params.Logger.Debugw(
"updating subscription permission",
"permissions", logger.Proto(u.subscriptionPermission),
"version", u.subscriptionPermissionVersion.String(),
"version", &u.subscriptionPermissionVersion,
)
if err := u.parseSubscriptionPermissionsLocked(subscriptionPermission, func(pID livekit.ParticipantID) types.LocalParticipant {
u.lock.Unlock()
+3 -2
View File
@@ -32,6 +32,7 @@ import (
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/must"
"github.com/livekit/psrpc"
"github.com/livekit/livekit-server/pkg/clientconfiguration"
@@ -455,7 +456,7 @@ func (r *RoomManager) StartSession(
}
participantTopic := rpc.FormatParticipantTopic(roomName, participant.Identity())
participantServer := utils.Must(rpc.NewTypedParticipantServer(r, r.bus))
participantServer := must.Get(rpc.NewTypedParticipantServer(r, r.bus))
killParticipantServer := r.participantServers.Replace(participantTopic, participantServer)
if err := participantServer.RegisterAllParticipantTopics(participantTopic); err != nil {
killParticipantServer()
@@ -547,7 +548,7 @@ func (r *RoomManager) getOrCreateRoom(ctx context.Context, roomName livekit.Room
newRoom := rtc.NewRoom(ri, internal, *r.rtcConfig, &r.config.Audio, r.serverInfo, r.telemetry, r.agentClient, r.egressLauncher)
roomTopic := rpc.FormatRoomTopic(roomName)
roomServer := utils.Must(rpc.NewTypedRoomServer(r, r.bus))
roomServer := must.Get(rpc.NewTypedRoomServer(r, r.bus))
killRoomServer := r.roomServers.Replace(roomTopic, roomServer)
if err := roomServer.RegisterAllRoomTopics(roomTopic); err != nil {
killRoomServer()
+1 -1
View File
@@ -63,7 +63,7 @@ func NewSignalServer(
nodeID,
&signalService{region, sessionHandler, config},
bus,
middleware.WithServerMetrics(prometheus.PSRPCMetricsObserver{}),
middleware.WithServerMetrics(rpc.PSRPCMetricsObserver{}),
psrpc.WithServerChannelSize(config.StreamBufferSize),
)
if err != nil {
+1 -2
View File
@@ -32,7 +32,6 @@ import (
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
@@ -233,7 +232,7 @@ func getPSRPCConfig(config *config.Config) rpc.PSRPCConfig {
}
func getPSRPCClientParams(config rpc.PSRPCConfig, bus psrpc.MessageBus) rpc.ClientParams {
return rpc.NewClientParams(config, bus, logger.GetLogger(), prometheus.PSRPCMetricsObserver{})
return rpc.NewClientParams(config, bus, logger.GetLogger(), rpc.PSRPCMetricsObserver{})
}
func newInProcessTurnServer(conf *config.Config, authHandler turn.AuthHandler) (*turn.Server, error) {
+2 -3
View File
@@ -1,6 +1,6 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
@@ -13,7 +13,6 @@ import (
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
@@ -284,7 +283,7 @@ func getPSRPCConfig(config2 *config.Config) rpc.PSRPCConfig {
}
func getPSRPCClientParams(config2 rpc.PSRPCConfig, bus psrpc.MessageBus) rpc.ClientParams {
return rpc.NewClientParams(config2, bus, logger.GetLogger(), prometheus.PSRPCMetricsObserver{})
return rpc.NewClientParams(config2, bus, logger.GetLogger(), rpc.PSRPCMetricsObserver{})
}
func newInProcessTurnServer(conf *config.Config, authHandler turn.AuthHandler) (*turn.Server, error) {
+2 -1
View File
@@ -23,6 +23,7 @@ import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/rpc"
)
const (
@@ -108,7 +109,7 @@ func Init(nodeID string, nodeType livekit.NodeType, env string) {
initPacketStats(nodeID, nodeType, env)
initRoomStats(nodeID, nodeType, env)
initPSRPCStats(nodeID, nodeType, env)
rpc.InitPSRPCStats(prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env})
initQualityStats(nodeID, nodeType, env)
}
-125
View File
@@ -1,125 +0,0 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheus
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/livekit/protocol/livekit"
"github.com/livekit/psrpc"
"github.com/livekit/psrpc/pkg/middleware"
)
var (
psrpcRequestTime *prometheus.HistogramVec
psrpcStreamSendTime *prometheus.HistogramVec
psrpcStreamReceiveTotal *prometheus.CounterVec
psrpcStreamCurrent *prometheus.GaugeVec
psrpcErrorTotal *prometheus.CounterVec
)
func initPSRPCStats(nodeID string, nodeType livekit.NodeType, env string) {
labels := []string{"role", "kind", "service", "method"}
streamLabels := []string{"role", "service", "method"}
psrpcRequestTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: livekitNamespace,
Subsystem: "psrpc",
Name: "request_time_ms",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env},
Buckets: []float64{10, 50, 100, 300, 500, 1000, 1500, 2000, 5000, 10000},
}, labels)
psrpcStreamSendTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: livekitNamespace,
Subsystem: "psrpc",
Name: "stream_send_time_ms",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env},
Buckets: []float64{10, 50, 100, 300, 500, 1000, 1500, 2000, 5000, 10000},
}, streamLabels)
psrpcStreamReceiveTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "psrpc",
Name: "stream_receive_total",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env},
}, streamLabels)
psrpcStreamCurrent = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "psrpc",
Name: "stream_count",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env},
}, streamLabels)
psrpcErrorTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "psrpc",
Name: "error_total",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String(), "env": env},
}, labels)
prometheus.MustRegister(psrpcRequestTime)
prometheus.MustRegister(psrpcStreamSendTime)
prometheus.MustRegister(psrpcStreamReceiveTotal)
prometheus.MustRegister(psrpcStreamCurrent)
prometheus.MustRegister(psrpcErrorTotal)
}
var _ middleware.MetricsObserver = PSRPCMetricsObserver{}
type PSRPCMetricsObserver struct{}
func (o PSRPCMetricsObserver) OnUnaryRequest(role middleware.MetricRole, info psrpc.RPCInfo, duration time.Duration, err error) {
if err != nil {
psrpcErrorTotal.WithLabelValues(role.String(), "rpc", info.Service, info.Method).Inc()
} else if role == middleware.ClientRole {
psrpcRequestTime.WithLabelValues(role.String(), "rpc", info.Service, info.Method).Observe(float64(duration.Milliseconds()))
} else {
psrpcRequestTime.WithLabelValues(role.String(), "rpc", info.Service, info.Method).Observe(float64(duration.Milliseconds()))
}
}
func (o PSRPCMetricsObserver) OnMultiRequest(role middleware.MetricRole, info psrpc.RPCInfo, duration time.Duration, responseCount int, errorCount int) {
if responseCount == 0 {
psrpcErrorTotal.WithLabelValues(role.String(), "multirpc", info.Service, info.Method).Inc()
} else if role == middleware.ClientRole {
psrpcRequestTime.WithLabelValues(role.String(), "multirpc", info.Service, info.Method).Observe(float64(duration.Milliseconds()))
} else {
psrpcRequestTime.WithLabelValues(role.String(), "multirpc", info.Service, info.Method).Observe(float64(duration.Milliseconds()))
}
}
func (o PSRPCMetricsObserver) OnStreamSend(role middleware.MetricRole, info psrpc.RPCInfo, duration time.Duration, err error) {
if err != nil {
psrpcErrorTotal.WithLabelValues(role.String(), "stream", info.Service, info.Method).Inc()
} else {
psrpcStreamSendTime.WithLabelValues(role.String(), info.Service, info.Method).Observe(float64(duration.Milliseconds()))
}
}
func (o PSRPCMetricsObserver) OnStreamRecv(role middleware.MetricRole, info psrpc.RPCInfo, err error) {
if err != nil {
psrpcErrorTotal.WithLabelValues(role.String(), "stream", info.Service, info.Method).Inc()
} else {
psrpcStreamReceiveTotal.WithLabelValues(role.String(), info.Service, info.Method).Inc()
}
}
func (o PSRPCMetricsObserver) OnStreamOpen(role middleware.MetricRole, info psrpc.RPCInfo) {
psrpcStreamCurrent.WithLabelValues(role.String(), info.Service, info.Method).Inc()
}
func (o PSRPCMetricsObserver) OnStreamClose(role middleware.MetricRole, info psrpc.RPCInfo) {
psrpcStreamCurrent.WithLabelValues(role.String(), info.Service, info.Method).Dec()
}